| 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 "content/common/p2p_messages.h" | 9 #include "content/common/p2p_messages.h" |
| 10 #include "content/renderer/p2p/socket_client_delegate.h" | 10 #include "content/renderer/p2p/socket_client_delegate.h" |
| 11 #include "content/renderer/p2p/socket_dispatcher.h" | 11 #include "content/renderer/p2p/socket_dispatcher.h" |
| 12 #include "content/renderer/render_thread_impl.h" | 12 #include "content/renderer/render_thread_impl.h" |
| 13 #include "crypto/random.h" | 13 #include "crypto/random.h" |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 uint64 GetUniqueId(uint32 random_socket_id, uint32 packet_id) { | 17 uint64 GetUniqueId(uint32 random_socket_id, uint32 packet_id) { |
| 18 uint64 uid = random_socket_id; | 18 uint64 uid = random_socket_id; |
| 19 uid <<= 32; | 19 uid <<= 32; |
| 20 uid |= packet_id; | 20 uid |= packet_id; |
| 21 return uid; | 21 return uid; |
| 22 } | 22 } |
| 23 | 23 |
| 24 } // namespace | 24 } // namespace |
| 25 | 25 |
| 26 namespace content { | 26 namespace content { |
| 27 | 27 |
| 28 scoped_refptr<P2PSocketClient> P2PSocketClient::Create( | |
| 29 P2PSocketType type, | |
| 30 const net::IPEndPoint& local_address, | |
| 31 const net::IPEndPoint& remote_address, | |
| 32 P2PSocketClientDelegate* delegate) { | |
| 33 P2PSocketClientImpl* impl = new P2PSocketClientImpl( | |
| 34 RenderThreadImpl::current()->p2p_socket_dispatcher()); | |
| 35 impl->Init(type, local_address, remote_address, delegate); | |
| 36 return impl; | |
| 37 } | |
| 38 | |
| 39 P2PSocketClientImpl::P2PSocketClientImpl(P2PSocketDispatcher* dispatcher) | 28 P2PSocketClientImpl::P2PSocketClientImpl(P2PSocketDispatcher* dispatcher) |
| 40 : dispatcher_(dispatcher), | 29 : dispatcher_(dispatcher), |
| 41 ipc_message_loop_(dispatcher->message_loop()), | 30 ipc_message_loop_(dispatcher->message_loop()), |
| 42 delegate_message_loop_(base::MessageLoopProxy::current()), | 31 delegate_message_loop_(base::MessageLoopProxy::current()), |
| 43 socket_id_(0), delegate_(NULL), | 32 socket_id_(0), delegate_(NULL), |
| 44 state_(STATE_UNINITIALIZED), | 33 state_(STATE_UNINITIALIZED), |
| 45 random_socket_id_(0), | 34 random_socket_id_(0), |
| 46 next_packet_id_(0) { | 35 next_packet_id_(0) { |
| 47 crypto::RandBytes(&random_socket_id_, sizeof(random_socket_id_)); | 36 crypto::RandBytes(&random_socket_id_, sizeof(random_socket_id_)); |
| 48 } | 37 } |
| 49 | 38 |
| 50 P2PSocketClientImpl::~P2PSocketClientImpl() { | 39 P2PSocketClientImpl::~P2PSocketClientImpl() { |
| 51 CHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED); | 40 CHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED); |
| 52 } | 41 } |
| 53 | 42 |
| 54 void P2PSocketClientImpl::Init( | 43 void P2PSocketClientImpl::Init( |
| 55 P2PSocketType type, | 44 P2PSocketType type, |
| 56 const net::IPEndPoint& local_address, | 45 const net::IPEndPoint& local_address, |
| 57 const net::IPEndPoint& remote_address, | 46 const P2PHostAndIPEndPoint& remote_address, |
| 58 P2PSocketClientDelegate* delegate) { | 47 P2PSocketClientDelegate* delegate) { |
| 59 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 48 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
| 60 // |delegate_| is only accessesed on |delegate_message_loop_|. | 49 // |delegate_| is only accessesed on |delegate_message_loop_|. |
| 61 delegate_ = delegate; | 50 delegate_ = delegate; |
| 62 | 51 |
| 63 ipc_message_loop_->PostTask( | 52 ipc_message_loop_->PostTask( |
| 64 FROM_HERE, base::Bind(&P2PSocketClientImpl::DoInit, | 53 FROM_HERE, base::Bind(&P2PSocketClientImpl::DoInit, |
| 65 this, | 54 this, |
| 66 type, | 55 type, |
| 67 local_address, | 56 local_address, |
| 68 remote_address)); | 57 remote_address)); |
| 69 } | 58 } |
| 70 | 59 |
| 71 void P2PSocketClientImpl::DoInit(P2PSocketType type, | 60 void P2PSocketClientImpl::DoInit(P2PSocketType type, |
| 72 const net::IPEndPoint& local_address, | 61 const net::IPEndPoint& local_address, |
| 73 const net::IPEndPoint& remote_address) { | 62 const P2PHostAndIPEndPoint& remote_address) { |
| 74 DCHECK_EQ(state_, STATE_UNINITIALIZED); | 63 DCHECK_EQ(state_, STATE_UNINITIALIZED); |
| 75 DCHECK(delegate_); | 64 DCHECK(delegate_); |
| 76 state_ = STATE_OPENING; | 65 state_ = STATE_OPENING; |
| 77 socket_id_ = dispatcher_->RegisterClient(this); | 66 socket_id_ = dispatcher_->RegisterClient(this); |
| 78 dispatcher_->SendP2PMessage(new P2PHostMsg_CreateSocket( | 67 dispatcher_->SendP2PMessage(new P2PHostMsg_CreateSocket( |
| 79 type, socket_id_, local_address, remote_address)); | 68 type, socket_id_, local_address, remote_address)); |
| 80 } | 69 } |
| 81 | 70 |
| 82 void P2PSocketClientImpl::SendWithDscp( | 71 void P2PSocketClientImpl::SendWithDscp( |
| 83 const net::IPEndPoint& address, | 72 const net::IPEndPoint& address, |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 delegate_->OnDataReceived(address, data, timestamp); | 240 delegate_->OnDataReceived(address, data, timestamp); |
| 252 } | 241 } |
| 253 | 242 |
| 254 void P2PSocketClientImpl::Detach() { | 243 void P2PSocketClientImpl::Detach() { |
| 255 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 244 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 256 dispatcher_ = NULL; | 245 dispatcher_ = NULL; |
| 257 OnError(); | 246 OnError(); |
| 258 } | 247 } |
| 259 | 248 |
| 260 } // namespace content | 249 } // namespace content |
| OLD | NEW |