| 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::Send(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::SendWithPacketId, this, |
| 79 &P2PSocketClientImpl::SendWithDscp, this, address, data, options)); | 79 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 SendWithPacketId(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; |
| 91 } | 90 } |
| 92 | 91 |
| 93 void P2PSocketClientImpl::Send(const net::IPEndPoint& address, | 92 void P2PSocketClientImpl::SendWithPacketId(const net::IPEndPoint& address, |
| 94 const std::vector<char>& data) { | 93 const std::vector<char>& data, |
| 95 rtc::PacketOptions options(rtc::DSCP_DEFAULT); | 94 const rtc::PacketOptions& options, |
| 96 SendWithDscp(address, data, options); | 95 uint64_t packet_id) { |
| 96 TRACE_EVENT_ASYNC_BEGIN0("p2p", "Send", packet_id); |
| 97 dispatcher_->SendP2PMessage( |
| 98 new P2PHostMsg_Send(socket_id_, address, data, options, packet_id)); |
| 97 } | 99 } |
| 98 | 100 |
| 99 void P2PSocketClientImpl::SetOption(P2PSocketOption option, | 101 void P2PSocketClientImpl::SetOption(P2PSocketOption option, |
| 100 int value) { | 102 int value) { |
| 101 if (!ipc_message_loop_->BelongsToCurrentThread()) { | 103 if (!ipc_message_loop_->BelongsToCurrentThread()) { |
| 102 ipc_message_loop_->PostTask( | 104 ipc_message_loop_->PostTask( |
| 103 FROM_HERE, base::Bind( | 105 FROM_HERE, base::Bind( |
| 104 &P2PSocketClientImpl::SetOption, this, option, value)); | 106 &P2PSocketClientImpl::SetOption, this, option, value)); |
| 105 return; | 107 return; |
| 106 } | 108 } |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 scoped_refptr<P2PSocketClient> new_client) { | 191 scoped_refptr<P2PSocketClient> new_client) { |
| 190 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 192 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
| 191 if (delegate_) { | 193 if (delegate_) { |
| 192 delegate_->OnIncomingTcpConnection(address, new_client.get()); | 194 delegate_->OnIncomingTcpConnection(address, new_client.get()); |
| 193 } else { | 195 } else { |
| 194 // Just close the socket if there is no delegate to accept it. | 196 // Just close the socket if there is no delegate to accept it. |
| 195 new_client->Close(); | 197 new_client->Close(); |
| 196 } | 198 } |
| 197 } | 199 } |
| 198 | 200 |
| 199 void P2PSocketClientImpl::OnSendComplete() { | 201 void P2PSocketClientImpl::OnSendComplete( |
| 202 const P2PSendPacketMetrics& send_metrics) { |
| 200 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 203 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 201 | 204 |
| 202 delegate_message_loop_->PostTask( | 205 delegate_message_loop_->PostTask( |
| 203 FROM_HERE, base::Bind(&P2PSocketClientImpl::DeliverOnSendComplete, this)); | 206 FROM_HERE, base::Bind(&P2PSocketClientImpl::DeliverOnSendComplete, this, |
| 207 send_metrics)); |
| 204 } | 208 } |
| 205 | 209 |
| 206 void P2PSocketClientImpl::DeliverOnSendComplete() { | 210 void P2PSocketClientImpl::DeliverOnSendComplete( |
| 211 const P2PSendPacketMetrics& send_metrics) { |
| 207 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 212 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
| 208 if (delegate_) | 213 if (delegate_) |
| 209 delegate_->OnSendComplete(); | 214 delegate_->OnSendComplete(send_metrics); |
| 210 } | 215 } |
| 211 | 216 |
| 212 void P2PSocketClientImpl::OnError() { | 217 void P2PSocketClientImpl::OnError() { |
| 213 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 218 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 214 state_ = STATE_ERROR; | 219 state_ = STATE_ERROR; |
| 215 | 220 |
| 216 delegate_message_loop_->PostTask( | 221 delegate_message_loop_->PostTask( |
| 217 FROM_HERE, base::Bind(&P2PSocketClientImpl::DeliverOnError, this)); | 222 FROM_HERE, base::Bind(&P2PSocketClientImpl::DeliverOnError, this)); |
| 218 } | 223 } |
| 219 | 224 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 245 delegate_->OnDataReceived(address, data, timestamp); | 250 delegate_->OnDataReceived(address, data, timestamp); |
| 246 } | 251 } |
| 247 | 252 |
| 248 void P2PSocketClientImpl::Detach() { | 253 void P2PSocketClientImpl::Detach() { |
| 249 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 254 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 250 dispatcher_ = NULL; | 255 dispatcher_ = NULL; |
| 251 OnError(); | 256 OnError(); |
| 252 } | 257 } |
| 253 | 258 |
| 254 } // namespace content | 259 } // namespace content |
| OLD | NEW |