| 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" |
| 11 #include "content/renderer/p2p/socket_client_delegate.h" | 11 #include "content/renderer/p2p/socket_client_delegate.h" |
| 12 #include "content/renderer/p2p/socket_dispatcher.h" | 12 #include "content/renderer/p2p/socket_dispatcher.h" |
| 13 #include "content/renderer/render_thread_impl.h" | 13 #include "content/renderer/render_thread_impl.h" |
| 14 #include "crypto/random.h" | 14 #include "crypto/random.h" |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 uint64 GetUniqueId(uint32 random_socket_id, uint32 packet_id) { | 18 uint64_t GetUniqueId(uint32 random_socket_id, uint32 packet_id) { |
| 19 uint64 uid = random_socket_id; | 19 uint64_t uid = random_socket_id; |
| 20 uid <<= 32; | 20 uid <<= 32; |
| 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) |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 void P2PSocketClientImpl::DoInit(P2PSocketType type, | 62 void P2PSocketClientImpl::DoInit(P2PSocketType type, |
| 63 const net::IPEndPoint& local_address, | 63 const net::IPEndPoint& local_address, |
| 64 const P2PHostAndIPEndPoint& remote_address) { | 64 const P2PHostAndIPEndPoint& remote_address) { |
| 65 DCHECK_EQ(state_, STATE_UNINITIALIZED); | 65 DCHECK_EQ(state_, STATE_UNINITIALIZED); |
| 66 state_ = STATE_OPENING; | 66 state_ = STATE_OPENING; |
| 67 socket_id_ = dispatcher_->RegisterClient(this); | 67 socket_id_ = dispatcher_->RegisterClient(this); |
| 68 dispatcher_->SendP2PMessage(new P2PHostMsg_CreateSocket( | 68 dispatcher_->SendP2PMessage(new P2PHostMsg_CreateSocket( |
| 69 type, socket_id_, local_address, remote_address)); | 69 type, socket_id_, local_address, remote_address)); |
| 70 } | 70 } |
| 71 | 71 |
| 72 void P2PSocketClientImpl::SendWithDscp( | 72 uint64_t P2PSocketClientImpl::SendWithDscp(const net::IPEndPoint& address, |
| 73 const net::IPEndPoint& address, | 73 const std::vector<char>& data, |
| 74 const std::vector<char>& data, | 74 const rtc::PacketOptions& options) { |
| 75 const rtc::PacketOptions& options) { | 75 uint64_t unique_id = GetUniqueId(random_socket_id_, ++next_packet_id_); |
| 76 if (!ipc_message_loop_->BelongsToCurrentThread()) { | 76 if (!ipc_message_loop_->BelongsToCurrentThread()) { |
| 77 ipc_message_loop_->PostTask( | 77 ipc_message_loop_->PostTask( |
| 78 FROM_HERE, base::Bind( | 78 FROM_HERE, base::Bind(&P2PSocketClientImpl::SendWithDscpAndPacketId, |
| 79 &P2PSocketClientImpl::SendWithDscp, this, address, data, options)); | 79 this, address, data, options, unique_id)); |
| 80 return; | 80 return unique_id; |
| 81 } | 81 } |
| 82 | 82 |
| 83 // Can send data only when the socket is open. | 83 // Can send data only when the socket is open. |
| 84 DCHECK(state_ == STATE_OPEN || state_ == STATE_ERROR); | 84 DCHECK(state_ == STATE_OPEN || state_ == STATE_ERROR); |
| 85 if (state_ == STATE_OPEN) { | 85 if (state_ == STATE_OPEN) { |
| 86 uint64 unique_id = GetUniqueId(random_socket_id_, ++next_packet_id_); | 86 SendWithDscpAndPacketId(address, data, options, unique_id); |
| 87 TRACE_EVENT_ASYNC_BEGIN0("p2p", "Send", unique_id); | |
| 88 dispatcher_->SendP2PMessage(new P2PHostMsg_Send(socket_id_, address, data, | |
| 89 options, unique_id)); | |
| 90 } | 87 } |
| 88 |
| 89 return unique_id; |
| 90 } |
| 91 |
| 92 void P2PSocketClientImpl::SendWithDscpAndPacketId( |
| 93 const net::IPEndPoint& address, |
| 94 const std::vector<char>& data, |
| 95 const rtc::PacketOptions& options, |
| 96 uint64_t packet_id) { |
| 97 TRACE_EVENT_ASYNC_BEGIN0("p2p", "Send", packet_id); |
| 98 dispatcher_->SendP2PMessage( |
| 99 new P2PHostMsg_Send(socket_id_, address, data, options, packet_id)); |
| 91 } | 100 } |
| 92 | 101 |
| 93 void P2PSocketClientImpl::Send(const net::IPEndPoint& address, | 102 void P2PSocketClientImpl::Send(const net::IPEndPoint& address, |
| 94 const std::vector<char>& data) { | 103 const std::vector<char>& data) { |
| 95 rtc::PacketOptions options(rtc::DSCP_DEFAULT); | 104 rtc::PacketOptions options(rtc::DSCP_DEFAULT); |
| 96 SendWithDscp(address, data, options); | 105 SendWithDscp(address, data, options); |
| 97 } | 106 } |
| 98 | 107 |
| 99 void P2PSocketClientImpl::SetOption(P2PSocketOption option, | 108 void P2PSocketClientImpl::SetOption(P2PSocketOption option, |
| 100 int value) { | 109 int value) { |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 scoped_refptr<P2PSocketClient> new_client) { | 198 scoped_refptr<P2PSocketClient> new_client) { |
| 190 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 199 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
| 191 if (delegate_) { | 200 if (delegate_) { |
| 192 delegate_->OnIncomingTcpConnection(address, new_client.get()); | 201 delegate_->OnIncomingTcpConnection(address, new_client.get()); |
| 193 } else { | 202 } else { |
| 194 // Just close the socket if there is no delegate to accept it. | 203 // Just close the socket if there is no delegate to accept it. |
| 195 new_client->Close(); | 204 new_client->Close(); |
| 196 } | 205 } |
| 197 } | 206 } |
| 198 | 207 |
| 199 void P2PSocketClientImpl::OnSendComplete() { | 208 void P2PSocketClientImpl::OnSendComplete( |
| 209 const P2PSendPacketMetrics& send_metrics) { |
| 200 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 210 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 201 | 211 |
| 202 delegate_message_loop_->PostTask( | 212 delegate_message_loop_->PostTask( |
| 203 FROM_HERE, base::Bind(&P2PSocketClientImpl::DeliverOnSendComplete, this)); | 213 FROM_HERE, base::Bind(&P2PSocketClientImpl::DeliverOnSendComplete, this, |
| 214 send_metrics)); |
| 204 } | 215 } |
| 205 | 216 |
| 206 void P2PSocketClientImpl::DeliverOnSendComplete() { | 217 void P2PSocketClientImpl::DeliverOnSendComplete( |
| 218 const P2PSendPacketMetrics& send_metrics) { |
| 207 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 219 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
| 208 if (delegate_) | 220 if (delegate_) |
| 209 delegate_->OnSendComplete(); | 221 delegate_->OnSendComplete(send_metrics); |
| 210 } | 222 } |
| 211 | 223 |
| 212 void P2PSocketClientImpl::OnError() { | 224 void P2PSocketClientImpl::OnError() { |
| 213 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 225 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 214 state_ = STATE_ERROR; | 226 state_ = STATE_ERROR; |
| 215 | 227 |
| 216 delegate_message_loop_->PostTask( | 228 delegate_message_loop_->PostTask( |
| 217 FROM_HERE, base::Bind(&P2PSocketClientImpl::DeliverOnError, this)); | 229 FROM_HERE, base::Bind(&P2PSocketClientImpl::DeliverOnError, this)); |
| 218 } | 230 } |
| 219 | 231 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 245 delegate_->OnDataReceived(address, data, timestamp); | 257 delegate_->OnDataReceived(address, data, timestamp); |
| 246 } | 258 } |
| 247 | 259 |
| 248 void P2PSocketClientImpl::Detach() { | 260 void P2PSocketClientImpl::Detach() { |
| 249 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 261 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 250 dispatcher_ = NULL; | 262 dispatcher_ = NULL; |
| 251 OnError(); | 263 OnError(); |
| 252 } | 264 } |
| 253 | 265 |
| 254 } // namespace content | 266 } // namespace content |
| OLD | NEW |