| 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/message_loop_proxy.h" | 8 #include "base/message_loop_proxy.h" |
| 8 #include "content/common/p2p_messages.h" | 9 #include "content/common/p2p_messages.h" |
| 9 #include "content/renderer/p2p/socket_dispatcher.h" | 10 #include "content/renderer/p2p/socket_dispatcher.h" |
| 10 | 11 |
| 11 namespace content { | 12 namespace content { |
| 12 | 13 |
| 13 P2PSocketClient::P2PSocketClient(P2PSocketDispatcher* dispatcher) | 14 P2PSocketClient::P2PSocketClient(P2PSocketDispatcher* dispatcher) |
| 14 : dispatcher_(dispatcher), | 15 : dispatcher_(dispatcher), |
| 15 ipc_message_loop_(dispatcher->message_loop()), | 16 ipc_message_loop_(dispatcher->message_loop()), |
| 16 delegate_message_loop_(base::MessageLoopProxy::current()), | 17 delegate_message_loop_(base::MessageLoopProxy::current()), |
| 17 socket_id_(0), delegate_(NULL), | 18 socket_id_(0), delegate_(NULL), |
| 18 state_(STATE_UNINITIALIZED) { | 19 state_(STATE_UNINITIALIZED) { |
| 19 } | 20 } |
| 20 | 21 |
| 21 P2PSocketClient::~P2PSocketClient() { | 22 P2PSocketClient::~P2PSocketClient() { |
| 22 DCHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED); | 23 DCHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED); |
| 23 } | 24 } |
| 24 | 25 |
| 25 void P2PSocketClient::Init( | 26 void P2PSocketClient::Init( |
| 26 P2PSocketType type, | 27 P2PSocketType type, |
| 27 const net::IPEndPoint& local_address, | 28 const net::IPEndPoint& local_address, |
| 28 const net::IPEndPoint& remote_address, | 29 const net::IPEndPoint& remote_address, |
| 29 P2PSocketClient::Delegate* delegate) { | 30 P2PSocketClient::Delegate* delegate) { |
| 30 if (!ipc_message_loop_->BelongsToCurrentThread()) { | 31 if (!ipc_message_loop_->BelongsToCurrentThread()) { |
| 31 ipc_message_loop_->PostTask( | 32 ipc_message_loop_->PostTask( |
| 32 FROM_HERE, NewRunnableMethod( | 33 FROM_HERE, base::Bind(&P2PSocketClient::Init, this, type, local_address, |
| 33 this, &P2PSocketClient::Init, 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 0, 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, NewRunnableMethod(this, &P2PSocketClient::Send, address, | 50 FROM_HERE, base::Bind(&P2PSocketClient::Send, this, address, data)); |
| 51 data)); | |
| 52 return; | 51 return; |
| 53 } | 52 } |
| 54 | 53 |
| 55 // Can send data only when the socket is open. | 54 // Can send data only when the socket is open. |
| 56 DCHECK(state_ == STATE_OPEN || state_ == STATE_ERROR); | 55 DCHECK(state_ == STATE_OPEN || state_ == STATE_ERROR); |
| 57 if (state_ == STATE_OPEN) { | 56 if (state_ == STATE_OPEN) { |
| 58 dispatcher_->SendP2PMessage( | 57 dispatcher_->SendP2PMessage( |
| 59 new P2PHostMsg_Send(0, socket_id_, address, data)); | 58 new P2PHostMsg_Send(0, socket_id_, address, data)); |
| 60 } | 59 } |
| 61 } | 60 } |
| 62 | 61 |
| 63 void P2PSocketClient::Close() { | 62 void P2PSocketClient::Close() { |
| 64 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 63 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
| 65 | 64 |
| 66 delegate_ = NULL; | 65 delegate_ = NULL; |
| 67 | 66 |
| 68 ipc_message_loop_->PostTask( | 67 ipc_message_loop_->PostTask( |
| 69 FROM_HERE, NewRunnableMethod(this, &P2PSocketClient::DoClose)); | 68 FROM_HERE, base::Bind(&P2PSocketClient::DoClose, this)); |
| 70 } | 69 } |
| 71 | 70 |
| 72 void P2PSocketClient::DoClose() { | 71 void P2PSocketClient::DoClose() { |
| 73 if (dispatcher_) { | 72 if (dispatcher_) { |
| 74 if (state_ == STATE_OPEN || state_ == STATE_OPENING || | 73 if (state_ == STATE_OPEN || state_ == STATE_OPENING || |
| 75 state_ == STATE_ERROR) { | 74 state_ == STATE_ERROR) { |
| 76 dispatcher_->SendP2PMessage(new P2PHostMsg_DestroySocket(0, socket_id_)); | 75 dispatcher_->SendP2PMessage(new P2PHostMsg_DestroySocket(0, socket_id_)); |
| 77 } | 76 } |
| 78 dispatcher_->UnregisterClient(socket_id_); | 77 dispatcher_->UnregisterClient(socket_id_); |
| 79 } | 78 } |
| 80 | 79 |
| 81 state_ = STATE_CLOSED; | 80 state_ = STATE_CLOSED; |
| 82 } | 81 } |
| 83 | 82 |
| 84 void P2PSocketClient::set_delegate(Delegate* delegate) { | 83 void P2PSocketClient::set_delegate(Delegate* delegate) { |
| 85 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 84 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
| 86 delegate_ = delegate; | 85 delegate_ = delegate; |
| 87 } | 86 } |
| 88 | 87 |
| 89 void P2PSocketClient::OnSocketCreated(const net::IPEndPoint& address) { | 88 void P2PSocketClient::OnSocketCreated(const net::IPEndPoint& address) { |
| 90 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 89 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 91 DCHECK_EQ(state_, STATE_OPENING); | 90 DCHECK_EQ(state_, STATE_OPENING); |
| 92 state_ = STATE_OPEN; | 91 state_ = STATE_OPEN; |
| 93 | 92 |
| 94 delegate_message_loop_->PostTask(FROM_HERE, NewRunnableMethod( | 93 delegate_message_loop_->PostTask( |
| 95 this, &P2PSocketClient::DeliverOnSocketCreated, address)); | 94 FROM_HERE, |
| 95 base::Bind(&P2PSocketClient::DeliverOnSocketCreated, this, address)); |
| 96 } | 96 } |
| 97 | 97 |
| 98 void P2PSocketClient::DeliverOnSocketCreated(const net::IPEndPoint& address) { | 98 void P2PSocketClient::DeliverOnSocketCreated(const net::IPEndPoint& address) { |
| 99 if (delegate_) | 99 if (delegate_) |
| 100 delegate_->OnOpen(address); | 100 delegate_->OnOpen(address); |
| 101 } | 101 } |
| 102 | 102 |
| 103 void P2PSocketClient::OnIncomingTcpConnection(const net::IPEndPoint& address) { | 103 void P2PSocketClient::OnIncomingTcpConnection(const net::IPEndPoint& address) { |
| 104 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 104 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 105 DCHECK_EQ(state_, STATE_OPEN); | 105 DCHECK_EQ(state_, STATE_OPEN); |
| 106 | 106 |
| 107 scoped_refptr<P2PSocketClient> new_client = new P2PSocketClient(dispatcher_); | 107 scoped_refptr<P2PSocketClient> new_client = new P2PSocketClient(dispatcher_); |
| 108 new_client->socket_id_ = dispatcher_->RegisterClient(new_client); | 108 new_client->socket_id_ = dispatcher_->RegisterClient(new_client); |
| 109 new_client->state_ = STATE_OPEN; | 109 new_client->state_ = STATE_OPEN; |
| 110 new_client->delegate_message_loop_ = delegate_message_loop_; | 110 new_client->delegate_message_loop_ = delegate_message_loop_; |
| 111 | 111 |
| 112 dispatcher_->SendP2PMessage(new P2PHostMsg_AcceptIncomingTcpConnection( | 112 dispatcher_->SendP2PMessage(new P2PHostMsg_AcceptIncomingTcpConnection( |
| 113 0, socket_id_, address, new_client->socket_id_)); | 113 0, socket_id_, address, new_client->socket_id_)); |
| 114 | 114 |
| 115 delegate_message_loop_->PostTask(FROM_HERE, NewRunnableMethod( | 115 delegate_message_loop_->PostTask( |
| 116 this, &P2PSocketClient::DeliverOnIncomingTcpConnection, | 116 FROM_HERE, base::Bind(&P2PSocketClient::DeliverOnIncomingTcpConnection, |
| 117 address, new_client)); | 117 this, address, new_client)); |
| 118 } | 118 } |
| 119 | 119 |
| 120 void P2PSocketClient::DeliverOnIncomingTcpConnection( | 120 void P2PSocketClient::DeliverOnIncomingTcpConnection( |
| 121 const net::IPEndPoint& address, scoped_refptr<P2PSocketClient> new_client) { | 121 const net::IPEndPoint& address, scoped_refptr<P2PSocketClient> new_client) { |
| 122 if (delegate_) | 122 if (delegate_) |
| 123 delegate_->OnIncomingTcpConnection(address, new_client); | 123 delegate_->OnIncomingTcpConnection(address, new_client); |
| 124 } | 124 } |
| 125 | 125 |
| 126 void P2PSocketClient::OnError() { | 126 void P2PSocketClient::OnError() { |
| 127 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 127 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 128 state_ = STATE_ERROR; | 128 state_ = STATE_ERROR; |
| 129 | 129 |
| 130 delegate_message_loop_->PostTask(FROM_HERE, NewRunnableMethod( | 130 delegate_message_loop_->PostTask( |
| 131 this, &P2PSocketClient::DeliverOnError)); | 131 FROM_HERE, base::Bind(&P2PSocketClient::DeliverOnError, this)); |
| 132 } | 132 } |
| 133 | 133 |
| 134 void P2PSocketClient::DeliverOnError() { | 134 void P2PSocketClient::DeliverOnError() { |
| 135 if (delegate_) | 135 if (delegate_) |
| 136 delegate_->OnError(); | 136 delegate_->OnError(); |
| 137 } | 137 } |
| 138 | 138 |
| 139 void P2PSocketClient::OnDataReceived(const net::IPEndPoint& address, | 139 void P2PSocketClient::OnDataReceived(const net::IPEndPoint& address, |
| 140 const std::vector<char>& data) { | 140 const std::vector<char>& data) { |
| 141 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 141 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 142 DCHECK_EQ(STATE_OPEN, state_); | 142 DCHECK_EQ(STATE_OPEN, state_); |
| 143 delegate_message_loop_->PostTask(FROM_HERE, NewRunnableMethod( | 143 delegate_message_loop_->PostTask( |
| 144 this, &P2PSocketClient::DeliverOnDataReceived, address, data)); | 144 FROM_HERE, |
| 145 base::Bind(&P2PSocketClient::DeliverOnDataReceived, this, address, data)); |
| 145 } | 146 } |
| 146 | 147 |
| 147 void P2PSocketClient::DeliverOnDataReceived(const net::IPEndPoint& address, | 148 void P2PSocketClient::DeliverOnDataReceived(const net::IPEndPoint& address, |
| 148 const std::vector<char>& data) { | 149 const std::vector<char>& data) { |
| 149 if (delegate_) | 150 if (delegate_) |
| 150 delegate_->OnDataReceived(address, data); | 151 delegate_->OnDataReceived(address, data); |
| 151 } | 152 } |
| 152 | 153 |
| 153 void P2PSocketClient::Detach() { | 154 void P2PSocketClient::Detach() { |
| 154 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 155 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 155 dispatcher_ = NULL; | 156 dispatcher_ = NULL; |
| 156 OnError(); | 157 OnError(); |
| 157 } | 158 } |
| 158 | 159 |
| 159 } // namespace content | 160 } // namespace content |
| OLD | NEW |