OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "content/renderer/p2p/socket_client_impl.h" | 5 #include "content/renderer/p2p/socket_client_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop_proxy.h" | 8 #include "base/message_loop/message_loop_proxy.h" |
9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
10 #include "content/common/p2p_messages.h" | 10 #include "content/common/p2p_messages.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 uid |= packet_id; | 21 uid |= packet_id; |
22 return uid; | 22 return uid; |
23 } | 23 } |
24 | 24 |
25 } // namespace | 25 } // namespace |
26 | 26 |
27 namespace content { | 27 namespace content { |
28 | 28 |
29 P2PSocketClientImpl::P2PSocketClientImpl(P2PSocketDispatcher* dispatcher) | 29 P2PSocketClientImpl::P2PSocketClientImpl(P2PSocketDispatcher* dispatcher) |
30 : dispatcher_(dispatcher), | 30 : dispatcher_(dispatcher), |
31 ipc_message_loop_(dispatcher->message_loop()), | 31 ipc_task_runner_(dispatcher->task_runner()), |
32 delegate_message_loop_(base::MessageLoopProxy::current()), | 32 delegate_message_loop_(base::MessageLoopProxy::current()), |
33 socket_id_(0), delegate_(NULL), | 33 socket_id_(0), |
| 34 delegate_(NULL), |
34 state_(STATE_UNINITIALIZED), | 35 state_(STATE_UNINITIALIZED), |
35 random_socket_id_(0), | 36 random_socket_id_(0), |
36 next_packet_id_(0) { | 37 next_packet_id_(0) { |
37 crypto::RandBytes(&random_socket_id_, sizeof(random_socket_id_)); | 38 crypto::RandBytes(&random_socket_id_, sizeof(random_socket_id_)); |
38 } | 39 } |
39 | 40 |
40 P2PSocketClientImpl::~P2PSocketClientImpl() { | 41 P2PSocketClientImpl::~P2PSocketClientImpl() { |
41 CHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED); | 42 CHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED); |
42 } | 43 } |
43 | 44 |
44 void P2PSocketClientImpl::Init( | 45 void P2PSocketClientImpl::Init( |
45 P2PSocketType type, | 46 P2PSocketType type, |
46 const net::IPEndPoint& local_address, | 47 const net::IPEndPoint& local_address, |
47 const P2PHostAndIPEndPoint& remote_address, | 48 const P2PHostAndIPEndPoint& remote_address, |
48 P2PSocketClientDelegate* delegate) { | 49 P2PSocketClientDelegate* delegate) { |
49 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 50 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
50 DCHECK(delegate); | 51 DCHECK(delegate); |
51 // |delegate_| is only accessesed on |delegate_message_loop_|. | 52 // |delegate_| is only accessesed on |delegate_message_loop_|. |
52 delegate_ = delegate; | 53 delegate_ = delegate; |
53 | 54 |
54 ipc_message_loop_->PostTask( | 55 ipc_task_runner_->PostTask( |
55 FROM_HERE, base::Bind(&P2PSocketClientImpl::DoInit, | 56 FROM_HERE, base::Bind(&P2PSocketClientImpl::DoInit, this, type, |
56 this, | 57 local_address, remote_address)); |
57 type, | |
58 local_address, | |
59 remote_address)); | |
60 } | 58 } |
61 | 59 |
62 void P2PSocketClientImpl::DoInit(P2PSocketType type, | 60 void P2PSocketClientImpl::DoInit(P2PSocketType type, |
63 const net::IPEndPoint& local_address, | 61 const net::IPEndPoint& local_address, |
64 const P2PHostAndIPEndPoint& remote_address) { | 62 const P2PHostAndIPEndPoint& remote_address) { |
65 DCHECK_EQ(state_, STATE_UNINITIALIZED); | 63 DCHECK_EQ(state_, STATE_UNINITIALIZED); |
66 state_ = STATE_OPENING; | 64 state_ = STATE_OPENING; |
67 socket_id_ = dispatcher_->RegisterClient(this); | 65 socket_id_ = dispatcher_->RegisterClient(this); |
68 dispatcher_->SendP2PMessage(new P2PHostMsg_CreateSocket( | 66 dispatcher_->SendP2PMessage(new P2PHostMsg_CreateSocket( |
69 type, socket_id_, local_address, remote_address)); | 67 type, socket_id_, local_address, remote_address)); |
70 } | 68 } |
71 | 69 |
72 uint64_t P2PSocketClientImpl::Send(const net::IPEndPoint& address, | 70 uint64_t P2PSocketClientImpl::Send(const net::IPEndPoint& address, |
73 const std::vector<char>& data, | 71 const std::vector<char>& data, |
74 const rtc::PacketOptions& options) { | 72 const rtc::PacketOptions& options) { |
75 uint64_t unique_id = GetUniqueId(random_socket_id_, ++next_packet_id_); | 73 uint64_t unique_id = GetUniqueId(random_socket_id_, ++next_packet_id_); |
76 if (!ipc_message_loop_->BelongsToCurrentThread()) { | 74 if (!ipc_task_runner_->BelongsToCurrentThread()) { |
77 ipc_message_loop_->PostTask( | 75 ipc_task_runner_->PostTask( |
78 FROM_HERE, base::Bind(&P2PSocketClientImpl::SendWithPacketId, this, | 76 FROM_HERE, base::Bind(&P2PSocketClientImpl::SendWithPacketId, this, |
79 address, data, options, unique_id)); | 77 address, data, options, unique_id)); |
80 return unique_id; | 78 return unique_id; |
81 } | 79 } |
82 | 80 |
83 // Can send data only when the socket is open. | 81 // Can send data only when the socket is open. |
84 DCHECK(state_ == STATE_OPEN || state_ == STATE_ERROR); | 82 DCHECK(state_ == STATE_OPEN || state_ == STATE_ERROR); |
85 if (state_ == STATE_OPEN) { | 83 if (state_ == STATE_OPEN) { |
86 SendWithPacketId(address, data, options, unique_id); | 84 SendWithPacketId(address, data, options, unique_id); |
87 } | 85 } |
88 | 86 |
89 return unique_id; | 87 return unique_id; |
90 } | 88 } |
91 | 89 |
92 void P2PSocketClientImpl::SendWithPacketId(const net::IPEndPoint& address, | 90 void P2PSocketClientImpl::SendWithPacketId(const net::IPEndPoint& address, |
93 const std::vector<char>& data, | 91 const std::vector<char>& data, |
94 const rtc::PacketOptions& options, | 92 const rtc::PacketOptions& options, |
95 uint64_t packet_id) { | 93 uint64_t packet_id) { |
96 TRACE_EVENT_ASYNC_BEGIN0("p2p", "Send", packet_id); | 94 TRACE_EVENT_ASYNC_BEGIN0("p2p", "Send", packet_id); |
97 dispatcher_->SendP2PMessage( | 95 dispatcher_->SendP2PMessage( |
98 new P2PHostMsg_Send(socket_id_, address, data, options, packet_id)); | 96 new P2PHostMsg_Send(socket_id_, address, data, options, packet_id)); |
99 } | 97 } |
100 | 98 |
101 void P2PSocketClientImpl::SetOption(P2PSocketOption option, | 99 void P2PSocketClientImpl::SetOption(P2PSocketOption option, |
102 int value) { | 100 int value) { |
103 if (!ipc_message_loop_->BelongsToCurrentThread()) { | 101 if (!ipc_task_runner_->BelongsToCurrentThread()) { |
104 ipc_message_loop_->PostTask( | 102 ipc_task_runner_->PostTask( |
105 FROM_HERE, base::Bind( | 103 FROM_HERE, |
106 &P2PSocketClientImpl::SetOption, this, option, value)); | 104 base::Bind(&P2PSocketClientImpl::SetOption, this, option, value)); |
107 return; | 105 return; |
108 } | 106 } |
109 | 107 |
110 DCHECK(state_ == STATE_OPEN || state_ == STATE_ERROR); | 108 DCHECK(state_ == STATE_OPEN || state_ == STATE_ERROR); |
111 if (state_ == STATE_OPEN) { | 109 if (state_ == STATE_OPEN) { |
112 dispatcher_->SendP2PMessage(new P2PHostMsg_SetOption(socket_id_, | 110 dispatcher_->SendP2PMessage(new P2PHostMsg_SetOption(socket_id_, |
113 option, value)); | 111 option, value)); |
114 } | 112 } |
115 } | 113 } |
116 | 114 |
117 void P2PSocketClientImpl::Close() { | 115 void P2PSocketClientImpl::Close() { |
118 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 116 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
119 | 117 |
120 delegate_ = NULL; | 118 delegate_ = NULL; |
121 | 119 |
122 ipc_message_loop_->PostTask( | 120 ipc_task_runner_->PostTask(FROM_HERE, |
123 FROM_HERE, base::Bind(&P2PSocketClientImpl::DoClose, this)); | 121 base::Bind(&P2PSocketClientImpl::DoClose, this)); |
124 } | 122 } |
125 | 123 |
126 void P2PSocketClientImpl::DoClose() { | 124 void P2PSocketClientImpl::DoClose() { |
127 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 125 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
128 if (dispatcher_) { | 126 if (dispatcher_) { |
129 if (state_ == STATE_OPEN || state_ == STATE_OPENING || | 127 if (state_ == STATE_OPEN || state_ == STATE_OPENING || |
130 state_ == STATE_ERROR) { | 128 state_ == STATE_ERROR) { |
131 dispatcher_->SendP2PMessage(new P2PHostMsg_DestroySocket(socket_id_)); | 129 dispatcher_->SendP2PMessage(new P2PHostMsg_DestroySocket(socket_id_)); |
132 } | 130 } |
133 dispatcher_->UnregisterClient(socket_id_); | 131 dispatcher_->UnregisterClient(socket_id_); |
134 } | 132 } |
135 | 133 |
136 state_ = STATE_CLOSED; | 134 state_ = STATE_CLOSED; |
137 } | 135 } |
138 | 136 |
139 int P2PSocketClientImpl::GetSocketID() const { | 137 int P2PSocketClientImpl::GetSocketID() const { |
140 return socket_id_; | 138 return socket_id_; |
141 } | 139 } |
142 | 140 |
143 void P2PSocketClientImpl::SetDelegate(P2PSocketClientDelegate* delegate) { | 141 void P2PSocketClientImpl::SetDelegate(P2PSocketClientDelegate* delegate) { |
144 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 142 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
145 delegate_ = delegate; | 143 delegate_ = delegate; |
146 } | 144 } |
147 | 145 |
148 void P2PSocketClientImpl::OnSocketCreated( | 146 void P2PSocketClientImpl::OnSocketCreated( |
149 const net::IPEndPoint& local_address, | 147 const net::IPEndPoint& local_address, |
150 const net::IPEndPoint& remote_address) { | 148 const net::IPEndPoint& remote_address) { |
151 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 149 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
152 DCHECK_EQ(state_, STATE_OPENING); | 150 DCHECK_EQ(state_, STATE_OPENING); |
153 state_ = STATE_OPEN; | 151 state_ = STATE_OPEN; |
154 | 152 |
155 delegate_message_loop_->PostTask( | 153 delegate_message_loop_->PostTask( |
156 FROM_HERE, | 154 FROM_HERE, |
157 base::Bind(&P2PSocketClientImpl::DeliverOnSocketCreated, this, | 155 base::Bind(&P2PSocketClientImpl::DeliverOnSocketCreated, this, |
158 local_address, remote_address)); | 156 local_address, remote_address)); |
159 } | 157 } |
160 | 158 |
161 void P2PSocketClientImpl::DeliverOnSocketCreated( | 159 void P2PSocketClientImpl::DeliverOnSocketCreated( |
162 const net::IPEndPoint& local_address, | 160 const net::IPEndPoint& local_address, |
163 const net::IPEndPoint& remote_address) { | 161 const net::IPEndPoint& remote_address) { |
164 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 162 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
165 if (delegate_) | 163 if (delegate_) |
166 delegate_->OnOpen(local_address, remote_address); | 164 delegate_->OnOpen(local_address, remote_address); |
167 } | 165 } |
168 | 166 |
169 void P2PSocketClientImpl::OnIncomingTcpConnection( | 167 void P2PSocketClientImpl::OnIncomingTcpConnection( |
170 const net::IPEndPoint& address) { | 168 const net::IPEndPoint& address) { |
171 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 169 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
172 DCHECK_EQ(state_, STATE_OPEN); | 170 DCHECK_EQ(state_, STATE_OPEN); |
173 | 171 |
174 scoped_refptr<P2PSocketClientImpl> new_client = | 172 scoped_refptr<P2PSocketClientImpl> new_client = |
175 new P2PSocketClientImpl(dispatcher_); | 173 new P2PSocketClientImpl(dispatcher_); |
176 new_client->socket_id_ = dispatcher_->RegisterClient(new_client.get()); | 174 new_client->socket_id_ = dispatcher_->RegisterClient(new_client.get()); |
177 new_client->state_ = STATE_OPEN; | 175 new_client->state_ = STATE_OPEN; |
178 new_client->delegate_message_loop_ = delegate_message_loop_; | 176 new_client->delegate_message_loop_ = delegate_message_loop_; |
179 | 177 |
180 dispatcher_->SendP2PMessage(new P2PHostMsg_AcceptIncomingTcpConnection( | 178 dispatcher_->SendP2PMessage(new P2PHostMsg_AcceptIncomingTcpConnection( |
181 socket_id_, address, new_client->socket_id_)); | 179 socket_id_, address, new_client->socket_id_)); |
(...skipping 11 matching lines...) Expand all Loading... |
193 if (delegate_) { | 191 if (delegate_) { |
194 delegate_->OnIncomingTcpConnection(address, new_client.get()); | 192 delegate_->OnIncomingTcpConnection(address, new_client.get()); |
195 } else { | 193 } else { |
196 // Just close the socket if there is no delegate to accept it. | 194 // Just close the socket if there is no delegate to accept it. |
197 new_client->Close(); | 195 new_client->Close(); |
198 } | 196 } |
199 } | 197 } |
200 | 198 |
201 void P2PSocketClientImpl::OnSendComplete( | 199 void P2PSocketClientImpl::OnSendComplete( |
202 const P2PSendPacketMetrics& send_metrics) { | 200 const P2PSendPacketMetrics& send_metrics) { |
203 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 201 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
204 | 202 |
205 delegate_message_loop_->PostTask( | 203 delegate_message_loop_->PostTask( |
206 FROM_HERE, base::Bind(&P2PSocketClientImpl::DeliverOnSendComplete, this, | 204 FROM_HERE, base::Bind(&P2PSocketClientImpl::DeliverOnSendComplete, this, |
207 send_metrics)); | 205 send_metrics)); |
208 } | 206 } |
209 | 207 |
210 void P2PSocketClientImpl::DeliverOnSendComplete( | 208 void P2PSocketClientImpl::DeliverOnSendComplete( |
211 const P2PSendPacketMetrics& send_metrics) { | 209 const P2PSendPacketMetrics& send_metrics) { |
212 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 210 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
213 if (delegate_) | 211 if (delegate_) |
214 delegate_->OnSendComplete(send_metrics); | 212 delegate_->OnSendComplete(send_metrics); |
215 } | 213 } |
216 | 214 |
217 void P2PSocketClientImpl::OnError() { | 215 void P2PSocketClientImpl::OnError() { |
218 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 216 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
219 state_ = STATE_ERROR; | 217 state_ = STATE_ERROR; |
220 | 218 |
221 delegate_message_loop_->PostTask( | 219 delegate_message_loop_->PostTask( |
222 FROM_HERE, base::Bind(&P2PSocketClientImpl::DeliverOnError, this)); | 220 FROM_HERE, base::Bind(&P2PSocketClientImpl::DeliverOnError, this)); |
223 } | 221 } |
224 | 222 |
225 void P2PSocketClientImpl::DeliverOnError() { | 223 void P2PSocketClientImpl::DeliverOnError() { |
226 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 224 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
227 if (delegate_) | 225 if (delegate_) |
228 delegate_->OnError(); | 226 delegate_->OnError(); |
229 } | 227 } |
230 | 228 |
231 void P2PSocketClientImpl::OnDataReceived(const net::IPEndPoint& address, | 229 void P2PSocketClientImpl::OnDataReceived(const net::IPEndPoint& address, |
232 const std::vector<char>& data, | 230 const std::vector<char>& data, |
233 const base::TimeTicks& timestamp) { | 231 const base::TimeTicks& timestamp) { |
234 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 232 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
235 DCHECK_EQ(STATE_OPEN, state_); | 233 DCHECK_EQ(STATE_OPEN, state_); |
236 delegate_message_loop_->PostTask( | 234 delegate_message_loop_->PostTask( |
237 FROM_HERE, | 235 FROM_HERE, |
238 base::Bind(&P2PSocketClientImpl::DeliverOnDataReceived, | 236 base::Bind(&P2PSocketClientImpl::DeliverOnDataReceived, |
239 this, | 237 this, |
240 address, | 238 address, |
241 data, | 239 data, |
242 timestamp)); | 240 timestamp)); |
243 } | 241 } |
244 | 242 |
245 void P2PSocketClientImpl::DeliverOnDataReceived( | 243 void P2PSocketClientImpl::DeliverOnDataReceived( |
246 const net::IPEndPoint& address, const std::vector<char>& data, | 244 const net::IPEndPoint& address, const std::vector<char>& data, |
247 const base::TimeTicks& timestamp) { | 245 const base::TimeTicks& timestamp) { |
248 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 246 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
249 if (delegate_) | 247 if (delegate_) |
250 delegate_->OnDataReceived(address, data, timestamp); | 248 delegate_->OnDataReceived(address, data, timestamp); |
251 } | 249 } |
252 | 250 |
253 void P2PSocketClientImpl::Detach() { | 251 void P2PSocketClientImpl::Detach() { |
254 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 252 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
255 dispatcher_ = NULL; | 253 dispatcher_ = NULL; |
256 OnError(); | 254 OnError(); |
257 } | 255 } |
258 | 256 |
259 } // namespace content | 257 } // namespace content |
OLD | NEW |