OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_RENDERER_P2P_SOCKET_CLIENT_H_ | 5 #ifndef CHROME_RENDERER_P2P_SOCKET_CLIENT_H_ |
6 #define CHROME_RENDERER_P2P_SOCKET_CLIENT_H_ | 6 #define CHROME_RENDERER_P2P_SOCKET_CLIENT_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/ref_counted.h" | 10 #include "base/ref_counted.h" |
11 #include "content/common/p2p_sockets.h" | 11 #include "content/common/p2p_sockets.h" |
| 12 #include "net/base/ip_endpoint.h" |
12 | 13 |
13 namespace base { | 14 namespace base { |
14 class MessageLoopProxy; | 15 class MessageLoopProxy; |
15 } // namespace base | 16 } // namespace base |
16 | 17 |
17 class P2PSocketDispatcher; | 18 class P2PSocketDispatcher; |
18 | 19 |
19 // P2P socket that rountes all calls over IPC. | 20 // P2P socket that rountes all calls over IPC. |
20 // | 21 // |
21 // The object runs on two threads: IPC thread and delegate thread. The | 22 // The object runs on two threads: IPC thread and delegate thread. The |
22 // IPC thread is used to interact with P2PSocketDispatcher. All | 23 // IPC thread is used to interact with P2PSocketDispatcher. All |
23 // callbacks to the user of this class are called on the delegate | 24 // callbacks to the user of this class are called on the delegate |
24 // thread which is specified in Init(). | 25 // thread which is specified in Init(). |
25 class P2PSocketClient : public base::RefCountedThreadSafe<P2PSocketClient> { | 26 class P2PSocketClient : public base::RefCountedThreadSafe<P2PSocketClient> { |
26 public: | 27 public: |
27 // Delegate is called on the the same thread on the delegate thread. | 28 // Delegate is called on the the same thread on the delegate thread. |
28 class Delegate { | 29 class Delegate { |
29 public: | 30 public: |
30 virtual ~Delegate() { } | 31 virtual ~Delegate() { } |
31 | 32 |
32 virtual void OnOpen(const P2PSocketAddress& address) = 0; | 33 virtual void OnOpen(const net::IPEndPoint& address) = 0; |
33 virtual void OnError() = 0; | 34 virtual void OnError() = 0; |
34 virtual void OnDataReceived(const P2PSocketAddress& address, | 35 virtual void OnDataReceived(const net::IPEndPoint& address, |
35 const std::vector<char>& data) = 0; | 36 const std::vector<char>& data) = 0; |
36 }; | 37 }; |
37 | 38 |
38 explicit P2PSocketClient(P2PSocketDispatcher* dispatcher); | 39 explicit P2PSocketClient(P2PSocketDispatcher* dispatcher); |
39 | 40 |
40 // Initialize socket of the specified |type| and connected to the | 41 // Initialize socket of the specified |type| and connected to the |
41 // specified |address|. |address| matters only when |type| is set to | 42 // specified |address|. |address| matters only when |type| is set to |
42 // P2P_SOCKET_TCP_CLIENT. | 43 // P2P_SOCKET_TCP_CLIENT. |
43 void Init(P2PSocketType type, const P2PSocketAddress& address, | 44 void Init(P2PSocketType type, const net::IPEndPoint& address, |
44 Delegate* delegate, | 45 Delegate* delegate, |
45 scoped_refptr<base::MessageLoopProxy> delegate_loop); | 46 scoped_refptr<base::MessageLoopProxy> delegate_loop); |
46 | 47 |
47 // Send the |data| to the |address|. | 48 // Send the |data| to the |address|. |
48 void Send(const P2PSocketAddress& address, const std::vector<char>& data); | 49 void Send(const net::IPEndPoint& address, const std::vector<char>& data); |
49 | 50 |
50 // Must be called before the socket is destroyed. The delegate may | 51 // Must be called before the socket is destroyed. The delegate may |
51 // not be called after |closed_task| is executed. | 52 // not be called after |closed_task| is executed. |
52 void Close(); | 53 void Close(); |
53 | 54 |
54 int socket_id() const { return socket_id_; } | 55 int socket_id() const { return socket_id_; } |
55 | 56 |
56 private: | 57 private: |
57 enum State { | 58 enum State { |
58 STATE_UNINITIALIZED, | 59 STATE_UNINITIALIZED, |
59 STATE_OPENING, | 60 STATE_OPENING, |
60 STATE_OPEN, | 61 STATE_OPEN, |
61 STATE_CLOSED, | 62 STATE_CLOSED, |
62 STATE_ERROR, | 63 STATE_ERROR, |
63 }; | 64 }; |
64 | 65 |
65 friend class P2PSocketDispatcher; | 66 friend class P2PSocketDispatcher; |
66 | 67 |
67 // Calls destructor. | 68 // Calls destructor. |
68 friend class base::RefCountedThreadSafe<P2PSocketClient>; | 69 friend class base::RefCountedThreadSafe<P2PSocketClient>; |
69 | 70 |
70 virtual ~P2PSocketClient(); | 71 virtual ~P2PSocketClient(); |
71 | 72 |
72 // Message handlers that run on IPC thread. | 73 // Message handlers that run on IPC thread. |
73 void OnSocketCreated(const P2PSocketAddress& address); | 74 void OnSocketCreated(const net::IPEndPoint& address); |
74 void OnError(); | 75 void OnError(); |
75 void OnDataReceived(const P2PSocketAddress& address, | 76 void OnDataReceived(const net::IPEndPoint& address, |
76 const std::vector<char>& data); | 77 const std::vector<char>& data); |
77 | 78 |
78 // Proxy methods that deliver messages to the delegate thread. | 79 // Proxy methods that deliver messages to the delegate thread. |
79 void DeliverOnSocketCreated(const P2PSocketAddress& address); | 80 void DeliverOnSocketCreated(const net::IPEndPoint& address); |
80 void DeliverOnError(); | 81 void DeliverOnError(); |
81 void DeliverOnDataReceived(const P2PSocketAddress& address, | 82 void DeliverOnDataReceived(const net::IPEndPoint& address, |
82 const std::vector<char>& data); | 83 const std::vector<char>& data); |
83 | 84 |
84 // Scheduled on the IPC thread to finish closing the connection. | 85 // Scheduled on the IPC thread to finish closing the connection. |
85 void DoClose(); | 86 void DoClose(); |
86 | 87 |
87 | 88 |
88 // Called by the dispatcher when it is destroyed. | 89 // Called by the dispatcher when it is destroyed. |
89 void Detach(); | 90 void Detach(); |
90 | 91 |
91 P2PSocketDispatcher* dispatcher_; | 92 P2PSocketDispatcher* dispatcher_; |
92 scoped_refptr<base::MessageLoopProxy> ipc_message_loop_; | 93 scoped_refptr<base::MessageLoopProxy> ipc_message_loop_; |
93 scoped_refptr<base::MessageLoopProxy> delegate_message_loop_; | 94 scoped_refptr<base::MessageLoopProxy> delegate_message_loop_; |
94 int socket_id_; | 95 int socket_id_; |
95 Delegate* delegate_; | 96 Delegate* delegate_; |
96 State state_; | 97 State state_; |
97 | 98 |
98 DISALLOW_COPY_AND_ASSIGN(P2PSocketClient); | 99 DISALLOW_COPY_AND_ASSIGN(P2PSocketClient); |
99 }; | 100 }; |
100 | 101 |
101 #endif // CHROME_RENDERER_P2P_SOCKET_CLIENT_H_ | 102 #endif // CHROME_RENDERER_P2P_SOCKET_CLIENT_H_ |
OLD | NEW |