Chromium Code Reviews| 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 10 matching lines...) Expand all Loading... | |
| 21 | 21 |
| 22 P2PSocketClient::~P2PSocketClient() { | 22 P2PSocketClient::~P2PSocketClient() { |
| 23 DCHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED); | 23 DCHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED); |
| 24 } | 24 } |
| 25 | 25 |
| 26 void P2PSocketClient::Init( | 26 void P2PSocketClient::Init( |
| 27 P2PSocketType type, | 27 P2PSocketType type, |
| 28 const net::IPEndPoint& local_address, | 28 const net::IPEndPoint& local_address, |
| 29 const net::IPEndPoint& remote_address, | 29 const net::IPEndPoint& remote_address, |
| 30 P2PSocketClient::Delegate* delegate) { | 30 P2PSocketClient::Delegate* delegate) { |
| 31 if (!ipc_message_loop_->BelongsToCurrentThread()) { | 31 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
| 32 ipc_message_loop_->PostTask( | 32 // |delegate_| is only accessesed on |delegate_message_loop_|. |
| 33 FROM_HERE, base::Bind(&P2PSocketClient::Init, this, type, local_address, | 33 delegate_ = delegate; |
|
tommi (sloooow) - chröme
2012/09/20 13:47:46
maybe first DCHECK(!delegate_) or is it OK for del
| |
| 34 remote_address, delegate)); | |
| 35 return; | |
| 36 } | |
| 37 | 34 |
| 35 ipc_message_loop_->PostTask( | |
| 36 FROM_HERE, base::Bind(&P2PSocketClient::DoInit, this, type, local_address, | |
| 37 remote_address)); | |
| 38 } | |
| 39 | |
| 40 void P2PSocketClient::DoInit(P2PSocketType type, | |
| 41 const net::IPEndPoint& local_address, | |
| 42 const net::IPEndPoint& remote_address) { | |
| 38 DCHECK_EQ(state_, STATE_UNINITIALIZED); | 43 DCHECK_EQ(state_, STATE_UNINITIALIZED); |
| 44 DCHECK(delegate_); | |
| 39 state_ = STATE_OPENING; | 45 state_ = STATE_OPENING; |
| 40 delegate_ = delegate; | |
| 41 socket_id_ = dispatcher_->RegisterClient(this); | 46 socket_id_ = dispatcher_->RegisterClient(this); |
| 42 dispatcher_->SendP2PMessage(new P2PHostMsg_CreateSocket( | 47 dispatcher_->SendP2PMessage(new P2PHostMsg_CreateSocket( |
| 43 type, socket_id_, local_address, remote_address)); | 48 type, socket_id_, local_address, remote_address)); |
| 44 } | 49 } |
| 45 | 50 |
| 46 void P2PSocketClient::Send(const net::IPEndPoint& address, | 51 void P2PSocketClient::Send(const net::IPEndPoint& address, |
| 47 const std::vector<char>& data) { | 52 const std::vector<char>& data) { |
| 48 if (!ipc_message_loop_->BelongsToCurrentThread()) { | 53 if (!ipc_message_loop_->BelongsToCurrentThread()) { |
| 49 ipc_message_loop_->PostTask( | 54 ipc_message_loop_->PostTask( |
| 50 FROM_HERE, base::Bind(&P2PSocketClient::Send, this, address, data)); | 55 FROM_HERE, base::Bind(&P2PSocketClient::Send, this, address, data)); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 61 void P2PSocketClient::Close() { | 66 void P2PSocketClient::Close() { |
| 62 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 67 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
| 63 | 68 |
| 64 delegate_ = NULL; | 69 delegate_ = NULL; |
| 65 | 70 |
| 66 ipc_message_loop_->PostTask( | 71 ipc_message_loop_->PostTask( |
| 67 FROM_HERE, base::Bind(&P2PSocketClient::DoClose, this)); | 72 FROM_HERE, base::Bind(&P2PSocketClient::DoClose, this)); |
| 68 } | 73 } |
| 69 | 74 |
| 70 void P2PSocketClient::DoClose() { | 75 void P2PSocketClient::DoClose() { |
| 76 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | |
| 71 if (dispatcher_) { | 77 if (dispatcher_) { |
| 72 if (state_ == STATE_OPEN || state_ == STATE_OPENING || | 78 if (state_ == STATE_OPEN || state_ == STATE_OPENING || |
| 73 state_ == STATE_ERROR) { | 79 state_ == STATE_ERROR) { |
| 74 dispatcher_->SendP2PMessage(new P2PHostMsg_DestroySocket(socket_id_)); | 80 dispatcher_->SendP2PMessage(new P2PHostMsg_DestroySocket(socket_id_)); |
| 75 } | 81 } |
| 76 dispatcher_->UnregisterClient(socket_id_); | 82 dispatcher_->UnregisterClient(socket_id_); |
| 77 } | 83 } |
| 78 | 84 |
| 79 state_ = STATE_CLOSED; | 85 state_ = STATE_CLOSED; |
| 80 } | 86 } |
| 81 | 87 |
| 82 void P2PSocketClient::set_delegate(Delegate* delegate) { | 88 void P2PSocketClient::set_delegate(Delegate* delegate) { |
| 83 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 89 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
| 84 delegate_ = delegate; | 90 delegate_ = delegate; |
| 85 } | 91 } |
| 86 | 92 |
| 87 void P2PSocketClient::OnSocketCreated(const net::IPEndPoint& address) { | 93 void P2PSocketClient::OnSocketCreated(const net::IPEndPoint& address) { |
| 88 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 94 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 89 DCHECK_EQ(state_, STATE_OPENING); | 95 DCHECK_EQ(state_, STATE_OPENING); |
| 90 state_ = STATE_OPEN; | 96 state_ = STATE_OPEN; |
| 91 | 97 |
| 92 delegate_message_loop_->PostTask( | 98 delegate_message_loop_->PostTask( |
| 93 FROM_HERE, | 99 FROM_HERE, |
| 94 base::Bind(&P2PSocketClient::DeliverOnSocketCreated, this, address)); | 100 base::Bind(&P2PSocketClient::DeliverOnSocketCreated, this, address)); |
| 95 } | 101 } |
| 96 | 102 |
| 97 void P2PSocketClient::DeliverOnSocketCreated(const net::IPEndPoint& address) { | 103 void P2PSocketClient::DeliverOnSocketCreated(const net::IPEndPoint& address) { |
| 104 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | |
| 98 if (delegate_) | 105 if (delegate_) |
| 99 delegate_->OnOpen(address); | 106 delegate_->OnOpen(address); |
| 100 } | 107 } |
| 101 | 108 |
| 102 void P2PSocketClient::OnIncomingTcpConnection(const net::IPEndPoint& address) { | 109 void P2PSocketClient::OnIncomingTcpConnection(const net::IPEndPoint& address) { |
| 103 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 110 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 104 DCHECK_EQ(state_, STATE_OPEN); | 111 DCHECK_EQ(state_, STATE_OPEN); |
| 105 | 112 |
| 106 scoped_refptr<P2PSocketClient> new_client = new P2PSocketClient(dispatcher_); | 113 scoped_refptr<P2PSocketClient> new_client = new P2PSocketClient(dispatcher_); |
| 107 new_client->socket_id_ = dispatcher_->RegisterClient(new_client); | 114 new_client->socket_id_ = dispatcher_->RegisterClient(new_client); |
| 108 new_client->state_ = STATE_OPEN; | 115 new_client->state_ = STATE_OPEN; |
| 109 new_client->delegate_message_loop_ = delegate_message_loop_; | 116 new_client->delegate_message_loop_ = delegate_message_loop_; |
| 110 | 117 |
| 111 dispatcher_->SendP2PMessage(new P2PHostMsg_AcceptIncomingTcpConnection( | 118 dispatcher_->SendP2PMessage(new P2PHostMsg_AcceptIncomingTcpConnection( |
| 112 socket_id_, address, new_client->socket_id_)); | 119 socket_id_, address, new_client->socket_id_)); |
| 113 | 120 |
| 114 delegate_message_loop_->PostTask( | 121 delegate_message_loop_->PostTask( |
| 115 FROM_HERE, base::Bind(&P2PSocketClient::DeliverOnIncomingTcpConnection, | 122 FROM_HERE, base::Bind(&P2PSocketClient::DeliverOnIncomingTcpConnection, |
| 116 this, address, new_client)); | 123 this, address, new_client)); |
| 117 } | 124 } |
| 118 | 125 |
| 119 void P2PSocketClient::DeliverOnIncomingTcpConnection( | 126 void P2PSocketClient::DeliverOnIncomingTcpConnection( |
| 120 const net::IPEndPoint& address, scoped_refptr<P2PSocketClient> new_client) { | 127 const net::IPEndPoint& address, scoped_refptr<P2PSocketClient> new_client) { |
| 128 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | |
| 121 if (delegate_) | 129 if (delegate_) |
| 122 delegate_->OnIncomingTcpConnection(address, new_client); | 130 delegate_->OnIncomingTcpConnection(address, new_client); |
| 123 } | 131 } |
| 124 | 132 |
| 125 void P2PSocketClient::OnError() { | 133 void P2PSocketClient::OnError() { |
| 126 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 134 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 127 state_ = STATE_ERROR; | 135 state_ = STATE_ERROR; |
| 128 | 136 |
| 129 delegate_message_loop_->PostTask( | 137 delegate_message_loop_->PostTask( |
| 130 FROM_HERE, base::Bind(&P2PSocketClient::DeliverOnError, this)); | 138 FROM_HERE, base::Bind(&P2PSocketClient::DeliverOnError, this)); |
| 131 } | 139 } |
| 132 | 140 |
| 133 void P2PSocketClient::DeliverOnError() { | 141 void P2PSocketClient::DeliverOnError() { |
| 142 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | |
| 134 if (delegate_) | 143 if (delegate_) |
| 135 delegate_->OnError(); | 144 delegate_->OnError(); |
| 136 } | 145 } |
| 137 | 146 |
| 138 void P2PSocketClient::OnDataReceived(const net::IPEndPoint& address, | 147 void P2PSocketClient::OnDataReceived(const net::IPEndPoint& address, |
| 139 const std::vector<char>& data) { | 148 const std::vector<char>& data) { |
| 140 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 149 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 141 DCHECK_EQ(STATE_OPEN, state_); | 150 DCHECK_EQ(STATE_OPEN, state_); |
| 142 delegate_message_loop_->PostTask( | 151 delegate_message_loop_->PostTask( |
| 143 FROM_HERE, | 152 FROM_HERE, |
| 144 base::Bind(&P2PSocketClient::DeliverOnDataReceived, this, address, data)); | 153 base::Bind(&P2PSocketClient::DeliverOnDataReceived, this, address, data)); |
| 145 } | 154 } |
| 146 | 155 |
| 147 void P2PSocketClient::DeliverOnDataReceived(const net::IPEndPoint& address, | 156 void P2PSocketClient::DeliverOnDataReceived(const net::IPEndPoint& address, |
| 148 const std::vector<char>& data) { | 157 const std::vector<char>& data) { |
| 158 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | |
| 149 if (delegate_) | 159 if (delegate_) |
| 150 delegate_->OnDataReceived(address, data); | 160 delegate_->OnDataReceived(address, data); |
| 151 } | 161 } |
| 152 | 162 |
| 153 void P2PSocketClient::Detach() { | 163 void P2PSocketClient::Detach() { |
| 154 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 164 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 155 dispatcher_ = NULL; | 165 dispatcher_ = NULL; |
| 156 OnError(); | 166 OnError(); |
| 157 } | 167 } |
| 158 | 168 |
| 159 } // namespace content | 169 } // namespace content |
| OLD | NEW |