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 #include "content/renderer/p2p/socket_client.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/message_loop/message_loop_proxy.h" | |
9 #include "content/common/p2p_messages.h" | |
10 #include "content/renderer/p2p/socket_dispatcher.h" | |
11 #include "crypto/random.h" | |
12 | |
13 namespace { | |
14 | |
15 uint64 GetUniqueId(uint32 random_socket_id, uint32 packet_id) { | |
16 uint64 uid = random_socket_id; | |
17 uid <<= 32; | |
18 uid |= packet_id; | |
19 return uid; | |
20 } | |
21 | |
22 } // namespace | |
23 | |
24 namespace content { | |
25 | |
26 P2PSocketClient::P2PSocketClient(P2PSocketDispatcher* dispatcher) | |
27 : dispatcher_(dispatcher), | |
28 ipc_message_loop_(dispatcher->message_loop()), | |
29 delegate_message_loop_(base::MessageLoopProxy::current()), | |
30 socket_id_(0), delegate_(NULL), | |
31 state_(STATE_UNINITIALIZED), | |
32 random_socket_id_(0), | |
33 next_packet_id_(0) { | |
34 crypto::RandBytes(&random_socket_id_, sizeof(random_socket_id_)); | |
35 } | |
36 | |
37 P2PSocketClient::~P2PSocketClient() { | |
38 CHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED); | |
39 } | |
40 | |
41 void P2PSocketClient::Init( | |
42 P2PSocketType type, | |
43 const net::IPEndPoint& local_address, | |
44 const net::IPEndPoint& remote_address, | |
45 P2PSocketClient::Delegate* delegate) { | |
46 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | |
47 // |delegate_| is only accessesed on |delegate_message_loop_|. | |
48 delegate_ = delegate; | |
49 | |
50 ipc_message_loop_->PostTask( | |
51 FROM_HERE, base::Bind(&P2PSocketClient::DoInit, this, type, local_address, | |
52 remote_address)); | |
53 } | |
54 | |
55 void P2PSocketClient::DoInit(P2PSocketType type, | |
56 const net::IPEndPoint& local_address, | |
57 const net::IPEndPoint& remote_address) { | |
58 DCHECK_EQ(state_, STATE_UNINITIALIZED); | |
59 DCHECK(delegate_); | |
60 state_ = STATE_OPENING; | |
61 socket_id_ = dispatcher_->RegisterClient(this); | |
62 dispatcher_->SendP2PMessage(new P2PHostMsg_CreateSocket( | |
63 type, socket_id_, local_address, remote_address)); | |
64 } | |
65 | |
66 void P2PSocketClient::SendWithDscp( | |
67 const net::IPEndPoint& address, | |
68 const std::vector<char>& data, | |
69 net::DiffServCodePoint dscp) { | |
70 if (!ipc_message_loop_->BelongsToCurrentThread()) { | |
71 ipc_message_loop_->PostTask( | |
72 FROM_HERE, base::Bind( | |
73 &P2PSocketClient::SendWithDscp, this, address, data, dscp)); | |
74 return; | |
75 } | |
76 | |
77 // Can send data only when the socket is open. | |
78 DCHECK(state_ == STATE_OPEN || state_ == STATE_ERROR); | |
79 if (state_ == STATE_OPEN) { | |
80 uint64 unique_id = GetUniqueId(random_socket_id_, ++next_packet_id_); | |
81 TRACE_EVENT_ASYNC_BEGIN0("p2p", "Send", unique_id); | |
82 dispatcher_->SendP2PMessage(new P2PHostMsg_Send(socket_id_, address, data, | |
83 dscp, unique_id)); | |
84 } | |
85 } | |
86 | |
87 void P2PSocketClient::Send(const net::IPEndPoint& address, | |
88 const std::vector<char>& data) { | |
89 SendWithDscp(address, data, net::DSCP_DEFAULT); | |
90 } | |
91 | |
92 void P2PSocketClient::Close() { | |
93 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | |
94 | |
95 delegate_ = NULL; | |
96 | |
97 ipc_message_loop_->PostTask( | |
98 FROM_HERE, base::Bind(&P2PSocketClient::DoClose, this)); | |
99 } | |
100 | |
101 void P2PSocketClient::DoClose() { | |
102 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | |
103 if (dispatcher_) { | |
104 if (state_ == STATE_OPEN || state_ == STATE_OPENING || | |
105 state_ == STATE_ERROR) { | |
106 dispatcher_->SendP2PMessage(new P2PHostMsg_DestroySocket(socket_id_)); | |
107 } | |
108 dispatcher_->UnregisterClient(socket_id_); | |
109 } | |
110 | |
111 state_ = STATE_CLOSED; | |
112 } | |
113 | |
114 void P2PSocketClient::set_delegate(Delegate* delegate) { | |
115 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | |
116 delegate_ = delegate; | |
117 } | |
118 | |
119 void P2PSocketClient::OnSocketCreated(const net::IPEndPoint& address) { | |
120 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | |
121 DCHECK_EQ(state_, STATE_OPENING); | |
122 state_ = STATE_OPEN; | |
123 | |
124 delegate_message_loop_->PostTask( | |
125 FROM_HERE, | |
126 base::Bind(&P2PSocketClient::DeliverOnSocketCreated, this, address)); | |
127 } | |
128 | |
129 void P2PSocketClient::DeliverOnSocketCreated(const net::IPEndPoint& address) { | |
130 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | |
131 if (delegate_) | |
132 delegate_->OnOpen(address); | |
133 } | |
134 | |
135 void P2PSocketClient::OnIncomingTcpConnection(const net::IPEndPoint& address) { | |
136 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | |
137 DCHECK_EQ(state_, STATE_OPEN); | |
138 | |
139 scoped_refptr<P2PSocketClient> new_client = new P2PSocketClient(dispatcher_); | |
140 new_client->socket_id_ = dispatcher_->RegisterClient(new_client.get()); | |
141 new_client->state_ = STATE_OPEN; | |
142 new_client->delegate_message_loop_ = delegate_message_loop_; | |
143 | |
144 dispatcher_->SendP2PMessage(new P2PHostMsg_AcceptIncomingTcpConnection( | |
145 socket_id_, address, new_client->socket_id_)); | |
146 | |
147 delegate_message_loop_->PostTask( | |
148 FROM_HERE, base::Bind(&P2PSocketClient::DeliverOnIncomingTcpConnection, | |
149 this, address, new_client)); | |
150 } | |
151 | |
152 void P2PSocketClient::DeliverOnIncomingTcpConnection( | |
153 const net::IPEndPoint& address, scoped_refptr<P2PSocketClient> new_client) { | |
154 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | |
155 if (delegate_) { | |
156 delegate_->OnIncomingTcpConnection(address, new_client.get()); | |
157 } else { | |
158 // Just close the socket if there is no delegate to accept it. | |
159 new_client->Close(); | |
160 } | |
161 } | |
162 | |
163 void P2PSocketClient::OnSendComplete() { | |
164 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | |
165 | |
166 delegate_message_loop_->PostTask( | |
167 FROM_HERE, base::Bind(&P2PSocketClient::DeliverOnSendComplete, this)); | |
168 } | |
169 | |
170 void P2PSocketClient::DeliverOnSendComplete() { | |
171 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | |
172 if (delegate_) | |
173 delegate_->OnSendComplete(); | |
174 } | |
175 | |
176 void P2PSocketClient::OnError() { | |
177 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | |
178 state_ = STATE_ERROR; | |
179 | |
180 delegate_message_loop_->PostTask( | |
181 FROM_HERE, base::Bind(&P2PSocketClient::DeliverOnError, this)); | |
182 } | |
183 | |
184 void P2PSocketClient::DeliverOnError() { | |
185 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | |
186 if (delegate_) | |
187 delegate_->OnError(); | |
188 } | |
189 | |
190 void P2PSocketClient::OnDataReceived(const net::IPEndPoint& address, | |
191 const std::vector<char>& data) { | |
192 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | |
193 DCHECK_EQ(STATE_OPEN, state_); | |
194 delegate_message_loop_->PostTask( | |
195 FROM_HERE, | |
196 base::Bind(&P2PSocketClient::DeliverOnDataReceived, this, address, data)); | |
197 } | |
198 | |
199 void P2PSocketClient::DeliverOnDataReceived(const net::IPEndPoint& address, | |
200 const std::vector<char>& data) { | |
201 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | |
202 if (delegate_) | |
203 delegate_->OnDataReceived(address, data); | |
204 } | |
205 | |
206 void P2PSocketClient::Detach() { | |
207 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | |
208 dispatcher_ = NULL; | |
209 OnError(); | |
210 } | |
211 | |
212 } // namespace content | |
OLD | NEW |