OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CONTENT_RENDERER_P2P_SOCKET_CLIENT_H_ | |
6 #define CONTENT_RENDERER_P2P_SOCKET_CLIENT_H_ | |
7 | |
8 #include <vector> | |
9 | |
10 #include "base/memory/ref_counted.h" | |
11 #include "content/common/p2p_sockets.h" | |
12 #include "net/base/ip_endpoint.h" | |
13 | |
14 namespace base { | |
15 class MessageLoopProxy; | |
16 } // namespace base | |
17 | |
18 namespace content { | |
19 | |
20 class P2PSocketDispatcher; | |
21 | |
22 // P2P socket that rountes all calls over IPC. | |
23 // | |
24 // The object runs on two threads: IPC thread and delegate thread. The | |
25 // IPC thread is used to interact with P2PSocketDispatcher. All | |
26 // callbacks to the user of this class are called on the delegate | |
27 // thread which is specified in Init(). | |
28 class P2PSocketClient : public base::RefCountedThreadSafe<P2PSocketClient> { | |
29 public: | |
30 // Delegate is called on the the same thread on which P2PSocketCLient is | |
31 // created. | |
32 class Delegate { | |
33 public: | |
34 virtual ~Delegate() { } | |
35 | |
36 virtual void OnOpen(const net::IPEndPoint& address) = 0; | |
37 virtual void OnIncomingTcpConnection(const net::IPEndPoint& address, | |
38 P2PSocketClient* client) = 0; | |
39 virtual void OnSendComplete() = 0; | |
40 virtual void OnError() = 0; | |
41 virtual void OnDataReceived(const net::IPEndPoint& address, | |
42 const std::vector<char>& data) = 0; | |
43 }; | |
44 | |
45 explicit P2PSocketClient(P2PSocketDispatcher* dispatcher); | |
46 | |
47 // Initialize socket of the specified |type| and connected to the | |
48 // specified |address|. |address| matters only when |type| is set to | |
49 // P2P_SOCKET_TCP_CLIENT. | |
50 void Init(P2PSocketType type, | |
51 const net::IPEndPoint& local_address, | |
52 const net::IPEndPoint& remote_address, | |
53 Delegate* delegate); | |
54 | |
55 // Send the |data| to the |address|. | |
56 void Send(const net::IPEndPoint& address, const std::vector<char>& data); | |
57 | |
58 // Send the |data| to the |address| using Differentiated Services Code Point | |
59 // |dscp|. | |
60 void SendWithDscp(const net::IPEndPoint& address, | |
61 const std::vector<char>& data, | |
62 net::DiffServCodePoint dscp); | |
63 | |
64 // Must be called before the socket is destroyed. The delegate may | |
65 // not be called after |closed_task| is executed. | |
66 void Close(); | |
67 | |
68 int socket_id() const { return socket_id_; } | |
69 | |
70 void set_delegate(Delegate* delegate); | |
71 | |
72 private: | |
73 enum State { | |
74 STATE_UNINITIALIZED, | |
75 STATE_OPENING, | |
76 STATE_OPEN, | |
77 STATE_CLOSED, | |
78 STATE_ERROR, | |
79 }; | |
80 | |
81 friend class P2PSocketDispatcher; | |
82 | |
83 // Calls destructor. | |
84 friend class base::RefCountedThreadSafe<P2PSocketClient>; | |
85 | |
86 virtual ~P2PSocketClient(); | |
87 | |
88 // Message handlers that run on IPC thread. | |
89 void OnSocketCreated(const net::IPEndPoint& address); | |
90 void OnIncomingTcpConnection(const net::IPEndPoint& address); | |
91 void OnSendComplete(int packet_id); | |
92 void OnSendComplete(); | |
93 void OnError(); | |
94 void OnDataReceived(const net::IPEndPoint& address, | |
95 const std::vector<char>& data); | |
96 | |
97 // Proxy methods that deliver messages to the delegate thread. | |
98 void DeliverOnSocketCreated(const net::IPEndPoint& address); | |
99 void DeliverOnIncomingTcpConnection( | |
100 const net::IPEndPoint& address, | |
101 scoped_refptr<P2PSocketClient> new_client); | |
102 void DeliverOnSendComplete(); | |
103 void DeliverOnError(); | |
104 void DeliverOnDataReceived(const net::IPEndPoint& address, | |
105 const std::vector<char>& data); | |
106 | |
107 // Scheduled on the IPC thread to finish initialization. | |
108 void DoInit(P2PSocketType type, | |
109 const net::IPEndPoint& local_address, | |
110 const net::IPEndPoint& remote_address); | |
111 | |
112 // Scheduled on the IPC thread to finish closing the connection. | |
113 void DoClose(); | |
114 | |
115 // Called by the dispatcher when it is destroyed. | |
116 void Detach(); | |
117 | |
118 P2PSocketDispatcher* dispatcher_; | |
119 scoped_refptr<base::MessageLoopProxy> ipc_message_loop_; | |
120 scoped_refptr<base::MessageLoopProxy> delegate_message_loop_; | |
121 int socket_id_; | |
122 Delegate* delegate_; | |
123 State state_; | |
124 | |
125 // These two fields are used to identify packets for tracing. | |
126 uint32 random_socket_id_; | |
127 uint32 next_packet_id_; | |
128 | |
129 DISALLOW_COPY_AND_ASSIGN(P2PSocketClient); | |
130 }; | |
131 | |
132 } // namespace content | |
133 | |
134 #endif // CONTENT_RENDERER_P2P_SOCKET_CLIENT_H_ | |
OLD | NEW |