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