| 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/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop_proxy.h" | 8 #include "base/message_loop_proxy.h" |
| 9 #include "content/common/p2p_messages.h" | 9 #include "content/common/p2p_messages.h" |
| 10 #include "content/renderer/p2p/socket_dispatcher.h" | 10 #include "content/renderer/p2p/socket_dispatcher.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 FROM_HERE, base::Bind(&P2PSocketClient::Init, this, type, local_address, | 33 FROM_HERE, base::Bind(&P2PSocketClient::Init, this, type, local_address, |
| 34 remote_address, delegate)); | 34 remote_address, delegate)); |
| 35 return; | 35 return; |
| 36 } | 36 } |
| 37 | 37 |
| 38 DCHECK_EQ(state_, STATE_UNINITIALIZED); | 38 DCHECK_EQ(state_, STATE_UNINITIALIZED); |
| 39 state_ = STATE_OPENING; | 39 state_ = STATE_OPENING; |
| 40 delegate_ = delegate; | 40 delegate_ = delegate; |
| 41 socket_id_ = dispatcher_->RegisterClient(this); | 41 socket_id_ = dispatcher_->RegisterClient(this); |
| 42 dispatcher_->SendP2PMessage(new P2PHostMsg_CreateSocket( | 42 dispatcher_->SendP2PMessage(new P2PHostMsg_CreateSocket( |
| 43 0, type, socket_id_, local_address, remote_address)); | 43 type, socket_id_, local_address, remote_address)); |
| 44 } | 44 } |
| 45 | 45 |
| 46 void P2PSocketClient::Send(const net::IPEndPoint& address, | 46 void P2PSocketClient::Send(const net::IPEndPoint& address, |
| 47 const std::vector<char>& data) { | 47 const std::vector<char>& data) { |
| 48 if (!ipc_message_loop_->BelongsToCurrentThread()) { | 48 if (!ipc_message_loop_->BelongsToCurrentThread()) { |
| 49 ipc_message_loop_->PostTask( | 49 ipc_message_loop_->PostTask( |
| 50 FROM_HERE, base::Bind(&P2PSocketClient::Send, this, address, data)); | 50 FROM_HERE, base::Bind(&P2PSocketClient::Send, this, address, data)); |
| 51 return; | 51 return; |
| 52 } | 52 } |
| 53 | 53 |
| 54 // Can send data only when the socket is open. | 54 // Can send data only when the socket is open. |
| 55 DCHECK(state_ == STATE_OPEN || state_ == STATE_ERROR); | 55 DCHECK(state_ == STATE_OPEN || state_ == STATE_ERROR); |
| 56 if (state_ == STATE_OPEN) { | 56 if (state_ == STATE_OPEN) { |
| 57 dispatcher_->SendP2PMessage( | 57 dispatcher_->SendP2PMessage(new P2PHostMsg_Send(socket_id_, address, data)); |
| 58 new P2PHostMsg_Send(0, socket_id_, address, data)); | |
| 59 } | 58 } |
| 60 } | 59 } |
| 61 | 60 |
| 62 void P2PSocketClient::Close() { | 61 void P2PSocketClient::Close() { |
| 63 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 62 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
| 64 | 63 |
| 65 delegate_ = NULL; | 64 delegate_ = NULL; |
| 66 | 65 |
| 67 ipc_message_loop_->PostTask( | 66 ipc_message_loop_->PostTask( |
| 68 FROM_HERE, base::Bind(&P2PSocketClient::DoClose, this)); | 67 FROM_HERE, base::Bind(&P2PSocketClient::DoClose, this)); |
| 69 } | 68 } |
| 70 | 69 |
| 71 void P2PSocketClient::DoClose() { | 70 void P2PSocketClient::DoClose() { |
| 72 if (dispatcher_) { | 71 if (dispatcher_) { |
| 73 if (state_ == STATE_OPEN || state_ == STATE_OPENING || | 72 if (state_ == STATE_OPEN || state_ == STATE_OPENING || |
| 74 state_ == STATE_ERROR) { | 73 state_ == STATE_ERROR) { |
| 75 dispatcher_->SendP2PMessage(new P2PHostMsg_DestroySocket(0, socket_id_)); | 74 dispatcher_->SendP2PMessage(new P2PHostMsg_DestroySocket(socket_id_)); |
| 76 } | 75 } |
| 77 dispatcher_->UnregisterClient(socket_id_); | 76 dispatcher_->UnregisterClient(socket_id_); |
| 78 } | 77 } |
| 79 | 78 |
| 80 state_ = STATE_CLOSED; | 79 state_ = STATE_CLOSED; |
| 81 } | 80 } |
| 82 | 81 |
| 83 void P2PSocketClient::set_delegate(Delegate* delegate) { | 82 void P2PSocketClient::set_delegate(Delegate* delegate) { |
| 84 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 83 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
| 85 delegate_ = delegate; | 84 delegate_ = delegate; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 103 void P2PSocketClient::OnIncomingTcpConnection(const net::IPEndPoint& address) { | 102 void P2PSocketClient::OnIncomingTcpConnection(const net::IPEndPoint& address) { |
| 104 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 103 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 105 DCHECK_EQ(state_, STATE_OPEN); | 104 DCHECK_EQ(state_, STATE_OPEN); |
| 106 | 105 |
| 107 scoped_refptr<P2PSocketClient> new_client = new P2PSocketClient(dispatcher_); | 106 scoped_refptr<P2PSocketClient> new_client = new P2PSocketClient(dispatcher_); |
| 108 new_client->socket_id_ = dispatcher_->RegisterClient(new_client); | 107 new_client->socket_id_ = dispatcher_->RegisterClient(new_client); |
| 109 new_client->state_ = STATE_OPEN; | 108 new_client->state_ = STATE_OPEN; |
| 110 new_client->delegate_message_loop_ = delegate_message_loop_; | 109 new_client->delegate_message_loop_ = delegate_message_loop_; |
| 111 | 110 |
| 112 dispatcher_->SendP2PMessage(new P2PHostMsg_AcceptIncomingTcpConnection( | 111 dispatcher_->SendP2PMessage(new P2PHostMsg_AcceptIncomingTcpConnection( |
| 113 0, socket_id_, address, new_client->socket_id_)); | 112 socket_id_, address, new_client->socket_id_)); |
| 114 | 113 |
| 115 delegate_message_loop_->PostTask( | 114 delegate_message_loop_->PostTask( |
| 116 FROM_HERE, base::Bind(&P2PSocketClient::DeliverOnIncomingTcpConnection, | 115 FROM_HERE, base::Bind(&P2PSocketClient::DeliverOnIncomingTcpConnection, |
| 117 this, address, new_client)); | 116 this, address, new_client)); |
| 118 } | 117 } |
| 119 | 118 |
| 120 void P2PSocketClient::DeliverOnIncomingTcpConnection( | 119 void P2PSocketClient::DeliverOnIncomingTcpConnection( |
| 121 const net::IPEndPoint& address, scoped_refptr<P2PSocketClient> new_client) { | 120 const net::IPEndPoint& address, scoped_refptr<P2PSocketClient> new_client) { |
| 122 if (delegate_) | 121 if (delegate_) |
| 123 delegate_->OnIncomingTcpConnection(address, new_client); | 122 delegate_->OnIncomingTcpConnection(address, new_client); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 151 delegate_->OnDataReceived(address, data); | 150 delegate_->OnDataReceived(address, data); |
| 152 } | 151 } |
| 153 | 152 |
| 154 void P2PSocketClient::Detach() { | 153 void P2PSocketClient::Detach() { |
| 155 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 154 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 156 dispatcher_ = NULL; | 155 dispatcher_ = NULL; |
| 157 OnError(); | 156 OnError(); |
| 158 } | 157 } |
| 159 | 158 |
| 160 } // namespace content | 159 } // namespace content |
| OLD | NEW |