| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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.h" | 5 #include "content/renderer/p2p/socket_client.h" |
| 6 | 6 |
| 7 #include "base/message_loop_proxy.h" | 7 #include "base/message_loop_proxy.h" |
| 8 #include "content/common/p2p_messages.h" | 8 #include "content/common/p2p_messages.h" |
| 9 #include "content/renderer/p2p/socket_dispatcher.h" | 9 #include "content/renderer/p2p/socket_dispatcher.h" |
| 10 | 10 |
| 11 P2PSocketClient::P2PSocketClient(P2PSocketDispatcher* dispatcher) | 11 P2PSocketClient::P2PSocketClient(P2PSocketDispatcher* dispatcher) |
| 12 : dispatcher_(dispatcher), | 12 : dispatcher_(dispatcher), |
| 13 ipc_message_loop_(dispatcher->message_loop()), | 13 ipc_message_loop_(dispatcher->message_loop()), |
| 14 delegate_message_loop_(NULL), | 14 delegate_message_loop_(base::MessageLoopProxy::CreateForCurrentThread()), |
| 15 socket_id_(0), delegate_(NULL), | 15 socket_id_(0), delegate_(NULL), |
| 16 state_(STATE_UNINITIALIZED) { | 16 state_(STATE_UNINITIALIZED) { |
| 17 } | 17 } |
| 18 | 18 |
| 19 P2PSocketClient::~P2PSocketClient() { | 19 P2PSocketClient::~P2PSocketClient() { |
| 20 DCHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED); | 20 DCHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED); |
| 21 } | 21 } |
| 22 | 22 |
| 23 void P2PSocketClient::Init( | 23 void P2PSocketClient::Init( |
| 24 P2PSocketType type, const net::IPEndPoint& local_address, | 24 P2PSocketType type, |
| 25 const net::IPEndPoint& remote_address, P2PSocketClient::Delegate* delegate, | 25 const net::IPEndPoint& local_address, |
| 26 scoped_refptr<base::MessageLoopProxy> delegate_loop) { | 26 const net::IPEndPoint& remote_address, |
| 27 P2PSocketClient::Delegate* delegate) { |
| 27 if (!ipc_message_loop_->BelongsToCurrentThread()) { | 28 if (!ipc_message_loop_->BelongsToCurrentThread()) { |
| 28 ipc_message_loop_->PostTask( | 29 ipc_message_loop_->PostTask( |
| 29 FROM_HERE, NewRunnableMethod( | 30 FROM_HERE, NewRunnableMethod( |
| 30 this, &P2PSocketClient::Init, type, local_address, remote_address, | 31 this, &P2PSocketClient::Init, type, local_address, |
| 31 delegate, delegate_loop)); | 32 remote_address, delegate)); |
| 32 return; | 33 return; |
| 33 } | 34 } |
| 34 | 35 |
| 35 DCHECK_EQ(state_, STATE_UNINITIALIZED); | 36 DCHECK_EQ(state_, STATE_UNINITIALIZED); |
| 36 state_ = STATE_OPENING; | 37 state_ = STATE_OPENING; |
| 37 delegate_ = delegate; | 38 delegate_ = delegate; |
| 38 delegate_message_loop_ = delegate_loop; | |
| 39 socket_id_ = dispatcher_->RegisterClient(this); | 39 socket_id_ = dispatcher_->RegisterClient(this); |
| 40 dispatcher_->SendP2PMessage(new P2PHostMsg_CreateSocket( | 40 dispatcher_->SendP2PMessage(new P2PHostMsg_CreateSocket( |
| 41 0, type, socket_id_, local_address, remote_address)); | 41 0, type, socket_id_, local_address, remote_address)); |
| 42 } | 42 } |
| 43 | 43 |
| 44 void P2PSocketClient::Send(const net::IPEndPoint& address, | 44 void P2PSocketClient::Send(const net::IPEndPoint& address, |
| 45 const std::vector<char>& data) { | 45 const std::vector<char>& data) { |
| 46 if (!ipc_message_loop_->BelongsToCurrentThread()) { | 46 if (!ipc_message_loop_->BelongsToCurrentThread()) { |
| 47 ipc_message_loop_->PostTask( | 47 ipc_message_loop_->PostTask( |
| 48 FROM_HERE, NewRunnableMethod(this, &P2PSocketClient::Send, address, | 48 FROM_HERE, NewRunnableMethod(this, &P2PSocketClient::Send, address, |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 const std::vector<char>& data) { | 146 const std::vector<char>& data) { |
| 147 if (delegate_) | 147 if (delegate_) |
| 148 delegate_->OnDataReceived(address, data); | 148 delegate_->OnDataReceived(address, data); |
| 149 } | 149 } |
| 150 | 150 |
| 151 void P2PSocketClient::Detach() { | 151 void P2PSocketClient::Detach() { |
| 152 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 152 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 153 dispatcher_ = NULL; | 153 dispatcher_ = NULL; |
| 154 OnError(); | 154 OnError(); |
| 155 } | 155 } |
| OLD | NEW |