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