Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: content/renderer/p2p/socket_client.cc

Issue 45183002: Expose the p2p client in content/public (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: some renaming Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/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_dispatcher.h" 10 #include "content/renderer/p2p/socket_dispatcher.h"
11 #include "crypto/random.h" 11 #include "crypto/random.h"
12 12
13 namespace { 13 namespace {
14 14
15 uint64 GetUniqueId(uint32 random_socket_id, uint32 packet_id) { 15 uint64 GetUniqueId(uint32 random_socket_id, uint32 packet_id) {
16 uint64 uid = random_socket_id; 16 uint64 uid = random_socket_id;
17 uid <<= 32; 17 uid <<= 32;
18 uid |= packet_id; 18 uid |= packet_id;
19 return uid; 19 return uid;
20 } 20 }
21 21
22 } // namespace 22 } // namespace
23 23
24 namespace content { 24 namespace content {
25 25
26 P2PSocketClient::P2PSocketClient(P2PSocketDispatcher* dispatcher) 26 P2PSocketClientImpl::P2PSocketClientImpl(P2PSocketDispatcher* dispatcher)
27 : dispatcher_(dispatcher), 27 : dispatcher_(dispatcher),
28 ipc_message_loop_(dispatcher->message_loop()), 28 ipc_message_loop_(dispatcher->message_loop()),
29 delegate_message_loop_(base::MessageLoopProxy::current()), 29 delegate_message_loop_(base::MessageLoopProxy::current()),
30 socket_id_(0), delegate_(NULL), 30 socket_id_(0), delegate_(NULL),
31 state_(STATE_UNINITIALIZED), 31 state_(STATE_UNINITIALIZED),
32 random_socket_id_(0), 32 random_socket_id_(0),
33 next_packet_id_(0) { 33 next_packet_id_(0) {
34 crypto::RandBytes(&random_socket_id_, sizeof(random_socket_id_)); 34 crypto::RandBytes(&random_socket_id_, sizeof(random_socket_id_));
35 } 35 }
36 36
37 P2PSocketClient::~P2PSocketClient() { 37 P2PSocketClientImpl::~P2PSocketClientImpl() {
38 CHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED); 38 CHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED);
39 } 39 }
40 40
41 void P2PSocketClient::Init( 41 void P2PSocketClientImpl::Init(
42 P2PSocketType type, 42 P2PSocketType type,
43 const net::IPEndPoint& local_address, 43 const net::IPEndPoint& local_address,
44 const net::IPEndPoint& remote_address, 44 const net::IPEndPoint& remote_address,
45 P2PSocketClient::Delegate* delegate) { 45 P2PSocketClient::Delegate* delegate) {
46 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); 46 DCHECK(delegate_message_loop_->BelongsToCurrentThread());
47 // |delegate_| is only accessesed on |delegate_message_loop_|. 47 // |delegate_| is only accessesed on |delegate_message_loop_|.
48 delegate_ = delegate; 48 delegate_ = delegate;
49 49
50 ipc_message_loop_->PostTask( 50 ipc_message_loop_->PostTask(
51 FROM_HERE, base::Bind(&P2PSocketClient::DoInit, this, type, local_address, 51 FROM_HERE, base::Bind(&P2PSocketClientImpl::DoInit,
52 this,
53 type,
54 local_address,
52 remote_address)); 55 remote_address));
53 } 56 }
54 57
55 void P2PSocketClient::DoInit(P2PSocketType type, 58 void P2PSocketClientImpl::DoInit(P2PSocketType type,
56 const net::IPEndPoint& local_address, 59 const net::IPEndPoint& local_address,
57 const net::IPEndPoint& remote_address) { 60 const net::IPEndPoint& remote_address) {
58 DCHECK_EQ(state_, STATE_UNINITIALIZED); 61 DCHECK_EQ(state_, STATE_UNINITIALIZED);
59 DCHECK(delegate_); 62 DCHECK(delegate_);
60 state_ = STATE_OPENING; 63 state_ = STATE_OPENING;
61 socket_id_ = dispatcher_->RegisterClient(this); 64 socket_id_ = dispatcher_->RegisterClient(this);
62 dispatcher_->SendP2PMessage(new P2PHostMsg_CreateSocket( 65 dispatcher_->SendP2PMessage(new P2PHostMsg_CreateSocket(
63 type, socket_id_, local_address, remote_address)); 66 type, socket_id_, local_address, remote_address));
64 } 67 }
65 68
66 void P2PSocketClient::SendWithDscp( 69 void P2PSocketClientImpl::SendWithDscp(
67 const net::IPEndPoint& address, 70 const net::IPEndPoint& address,
68 const std::vector<char>& data, 71 const std::vector<char>& data,
69 net::DiffServCodePoint dscp) { 72 net::DiffServCodePoint dscp) {
70 if (!ipc_message_loop_->BelongsToCurrentThread()) { 73 if (!ipc_message_loop_->BelongsToCurrentThread()) {
71 ipc_message_loop_->PostTask( 74 ipc_message_loop_->PostTask(
72 FROM_HERE, base::Bind( 75 FROM_HERE, base::Bind(
73 &P2PSocketClient::SendWithDscp, this, address, data, dscp)); 76 &P2PSocketClientImpl::SendWithDscp, this, address, data, dscp));
74 return; 77 return;
75 } 78 }
76 79
77 // Can send data only when the socket is open. 80 // Can send data only when the socket is open.
78 DCHECK(state_ == STATE_OPEN || state_ == STATE_ERROR); 81 DCHECK(state_ == STATE_OPEN || state_ == STATE_ERROR);
79 if (state_ == STATE_OPEN) { 82 if (state_ == STATE_OPEN) {
80 uint64 unique_id = GetUniqueId(random_socket_id_, ++next_packet_id_); 83 uint64 unique_id = GetUniqueId(random_socket_id_, ++next_packet_id_);
81 TRACE_EVENT_ASYNC_BEGIN0("p2p", "Send", unique_id); 84 TRACE_EVENT_ASYNC_BEGIN0("p2p", "Send", unique_id);
82 dispatcher_->SendP2PMessage(new P2PHostMsg_Send(socket_id_, address, data, 85 dispatcher_->SendP2PMessage(new P2PHostMsg_Send(socket_id_, address, data,
83 dscp, unique_id)); 86 dscp, unique_id));
84 } 87 }
85 } 88 }
86 89
87 void P2PSocketClient::Send(const net::IPEndPoint& address, 90 void P2PSocketClientImpl::Send(const net::IPEndPoint& address,
88 const std::vector<char>& data) { 91 const std::vector<char>& data) {
89 SendWithDscp(address, data, net::DSCP_DEFAULT); 92 SendWithDscp(address, data, net::DSCP_DEFAULT);
90 } 93 }
91 94
92 void P2PSocketClient::Close() { 95 void P2PSocketClientImpl::Close() {
93 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); 96 DCHECK(delegate_message_loop_->BelongsToCurrentThread());
94 97
95 delegate_ = NULL; 98 delegate_ = NULL;
96 99
97 ipc_message_loop_->PostTask( 100 ipc_message_loop_->PostTask(
98 FROM_HERE, base::Bind(&P2PSocketClient::DoClose, this)); 101 FROM_HERE, base::Bind(&P2PSocketClientImpl::DoClose, this));
99 } 102 }
100 103
101 void P2PSocketClient::DoClose() { 104 void P2PSocketClientImpl::DoClose() {
102 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); 105 DCHECK(ipc_message_loop_->BelongsToCurrentThread());
103 if (dispatcher_) { 106 if (dispatcher_) {
104 if (state_ == STATE_OPEN || state_ == STATE_OPENING || 107 if (state_ == STATE_OPEN || state_ == STATE_OPENING ||
105 state_ == STATE_ERROR) { 108 state_ == STATE_ERROR) {
106 dispatcher_->SendP2PMessage(new P2PHostMsg_DestroySocket(socket_id_)); 109 dispatcher_->SendP2PMessage(new P2PHostMsg_DestroySocket(socket_id_));
107 } 110 }
108 dispatcher_->UnregisterClient(socket_id_); 111 dispatcher_->UnregisterClient(socket_id_);
109 } 112 }
110 113
111 state_ = STATE_CLOSED; 114 state_ = STATE_CLOSED;
112 } 115 }
113 116
114 void P2PSocketClient::set_delegate(Delegate* delegate) { 117 int P2PSocketClientImpl::socket_id() const {
118 return socket_id_;
119 }
120
121 void P2PSocketClientImpl::set_delegate(Delegate* delegate) {
115 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); 122 DCHECK(delegate_message_loop_->BelongsToCurrentThread());
116 delegate_ = delegate; 123 delegate_ = delegate;
117 } 124 }
118 125
119 void P2PSocketClient::OnSocketCreated(const net::IPEndPoint& address) { 126 void P2PSocketClientImpl::OnSocketCreated(const net::IPEndPoint& address) {
120 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); 127 DCHECK(ipc_message_loop_->BelongsToCurrentThread());
121 DCHECK_EQ(state_, STATE_OPENING); 128 DCHECK_EQ(state_, STATE_OPENING);
122 state_ = STATE_OPEN; 129 state_ = STATE_OPEN;
123 130
124 delegate_message_loop_->PostTask( 131 delegate_message_loop_->PostTask(
125 FROM_HERE, 132 FROM_HERE,
126 base::Bind(&P2PSocketClient::DeliverOnSocketCreated, this, address)); 133 base::Bind(&P2PSocketClientImpl::DeliverOnSocketCreated, this, address));
127 } 134 }
128 135
129 void P2PSocketClient::DeliverOnSocketCreated(const net::IPEndPoint& address) { 136 void P2PSocketClientImpl::DeliverOnSocketCreated(
137 const net::IPEndPoint& address) {
130 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); 138 DCHECK(delegate_message_loop_->BelongsToCurrentThread());
131 if (delegate_) 139 if (delegate_)
132 delegate_->OnOpen(address); 140 delegate_->OnOpen(address);
133 } 141 }
134 142
135 void P2PSocketClient::OnIncomingTcpConnection(const net::IPEndPoint& address) { 143 void P2PSocketClientImpl::OnIncomingTcpConnection(
144 const net::IPEndPoint& address) {
136 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); 145 DCHECK(ipc_message_loop_->BelongsToCurrentThread());
137 DCHECK_EQ(state_, STATE_OPEN); 146 DCHECK_EQ(state_, STATE_OPEN);
138 147
139 scoped_refptr<P2PSocketClient> new_client = new P2PSocketClient(dispatcher_); 148 scoped_refptr<P2PSocketClientImpl> new_client =
149 new P2PSocketClientImpl(dispatcher_);
140 new_client->socket_id_ = dispatcher_->RegisterClient(new_client.get()); 150 new_client->socket_id_ = dispatcher_->RegisterClient(new_client.get());
141 new_client->state_ = STATE_OPEN; 151 new_client->state_ = STATE_OPEN;
142 new_client->delegate_message_loop_ = delegate_message_loop_; 152 new_client->delegate_message_loop_ = delegate_message_loop_;
143 153
144 dispatcher_->SendP2PMessage(new P2PHostMsg_AcceptIncomingTcpConnection( 154 dispatcher_->SendP2PMessage(new P2PHostMsg_AcceptIncomingTcpConnection(
145 socket_id_, address, new_client->socket_id_)); 155 socket_id_, address, new_client->socket_id_));
146 156
147 delegate_message_loop_->PostTask( 157 delegate_message_loop_->PostTask(
148 FROM_HERE, base::Bind(&P2PSocketClient::DeliverOnIncomingTcpConnection, 158 FROM_HERE, base::Bind(
149 this, address, new_client)); 159 &P2PSocketClientImpl::DeliverOnIncomingTcpConnection,
160 this, address, new_client));
150 } 161 }
151 162
152 void P2PSocketClient::DeliverOnIncomingTcpConnection( 163 void P2PSocketClientImpl::DeliverOnIncomingTcpConnection(
153 const net::IPEndPoint& address, scoped_refptr<P2PSocketClient> new_client) { 164 const net::IPEndPoint& address,
165 scoped_refptr<P2PSocketClient> new_client) {
154 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); 166 DCHECK(delegate_message_loop_->BelongsToCurrentThread());
155 if (delegate_) { 167 if (delegate_) {
156 delegate_->OnIncomingTcpConnection(address, new_client.get()); 168 delegate_->OnIncomingTcpConnection(address, new_client.get());
157 } else { 169 } else {
158 // Just close the socket if there is no delegate to accept it. 170 // Just close the socket if there is no delegate to accept it.
159 new_client->Close(); 171 new_client->Close();
160 } 172 }
161 } 173 }
162 174
163 void P2PSocketClient::OnSendComplete() { 175 void P2PSocketClientImpl::OnSendComplete() {
164 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); 176 DCHECK(ipc_message_loop_->BelongsToCurrentThread());
165 177
166 delegate_message_loop_->PostTask( 178 delegate_message_loop_->PostTask(
167 FROM_HERE, base::Bind(&P2PSocketClient::DeliverOnSendComplete, this)); 179 FROM_HERE, base::Bind(&P2PSocketClientImpl::DeliverOnSendComplete, this));
168 } 180 }
169 181
170 void P2PSocketClient::DeliverOnSendComplete() { 182 void P2PSocketClientImpl::DeliverOnSendComplete() {
171 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); 183 DCHECK(delegate_message_loop_->BelongsToCurrentThread());
172 if (delegate_) 184 if (delegate_)
173 delegate_->OnSendComplete(); 185 delegate_->OnSendComplete();
174 } 186 }
175 187
176 void P2PSocketClient::OnError() { 188 void P2PSocketClientImpl::OnError() {
177 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); 189 DCHECK(ipc_message_loop_->BelongsToCurrentThread());
178 state_ = STATE_ERROR; 190 state_ = STATE_ERROR;
179 191
180 delegate_message_loop_->PostTask( 192 delegate_message_loop_->PostTask(
181 FROM_HERE, base::Bind(&P2PSocketClient::DeliverOnError, this)); 193 FROM_HERE, base::Bind(&P2PSocketClientImpl::DeliverOnError, this));
182 } 194 }
183 195
184 void P2PSocketClient::DeliverOnError() { 196 void P2PSocketClientImpl::DeliverOnError() {
185 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); 197 DCHECK(delegate_message_loop_->BelongsToCurrentThread());
186 if (delegate_) 198 if (delegate_)
187 delegate_->OnError(); 199 delegate_->OnError();
188 } 200 }
189 201
190 void P2PSocketClient::OnDataReceived(const net::IPEndPoint& address, 202 void P2PSocketClientImpl::OnDataReceived(const net::IPEndPoint& address,
191 const std::vector<char>& data) { 203 const std::vector<char>& data) {
192 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); 204 DCHECK(ipc_message_loop_->BelongsToCurrentThread());
193 DCHECK_EQ(STATE_OPEN, state_); 205 DCHECK_EQ(STATE_OPEN, state_);
194 delegate_message_loop_->PostTask( 206 delegate_message_loop_->PostTask(
195 FROM_HERE, 207 FROM_HERE,
196 base::Bind(&P2PSocketClient::DeliverOnDataReceived, this, address, data)); 208 base::Bind(&P2PSocketClientImpl::DeliverOnDataReceived,
209 this,
210 address,
211 data));
197 } 212 }
198 213
199 void P2PSocketClient::DeliverOnDataReceived(const net::IPEndPoint& address, 214 void P2PSocketClientImpl::DeliverOnDataReceived(const net::IPEndPoint& address,
200 const std::vector<char>& data) { 215 const std::vector<char>& data) {
201 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); 216 DCHECK(delegate_message_loop_->BelongsToCurrentThread());
202 if (delegate_) 217 if (delegate_)
203 delegate_->OnDataReceived(address, data); 218 delegate_->OnDataReceived(address, data);
204 } 219 }
205 220
206 void P2PSocketClient::Detach() { 221 void P2PSocketClientImpl::Detach() {
207 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); 222 DCHECK(ipc_message_loop_->BelongsToCurrentThread());
208 dispatcher_ = NULL; 223 dispatcher_ = NULL;
209 OnError(); 224 OnError();
210 } 225 }
211 226
212 } // namespace content 227 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698