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 "jingle/glue/pseudotcp_adapter.h" | 5 #include "jingle/glue/pseudotcp_adapter.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/time.h" | 9 #include "base/time.h" |
10 #include "net/base/address_list.h" | 10 #include "net/base/address_list.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 public: | 27 public: |
28 Core(net::Socket* socket); | 28 Core(net::Socket* socket); |
29 virtual ~Core(); | 29 virtual ~Core(); |
30 | 30 |
31 // Functions used to implement net::StreamSocket. | 31 // Functions used to implement net::StreamSocket. |
32 int Read(net::IOBuffer* buffer, int buffer_size, | 32 int Read(net::IOBuffer* buffer, int buffer_size, |
33 net::OldCompletionCallback* callback); | 33 net::OldCompletionCallback* callback); |
34 int Write(net::IOBuffer* buffer, int buffer_size, | 34 int Write(net::IOBuffer* buffer, int buffer_size, |
35 net::OldCompletionCallback* callback); | 35 net::OldCompletionCallback* callback); |
36 int Connect(net::OldCompletionCallback* callback); | 36 int Connect(net::OldCompletionCallback* callback); |
| 37 int Connect(const net::CompletionCallback& callback); |
37 void Disconnect(); | 38 void Disconnect(); |
38 bool IsConnected() const; | 39 bool IsConnected() const; |
39 | 40 |
40 // cricket::IPseudoTcpNotify interface. | 41 // cricket::IPseudoTcpNotify interface. |
41 // These notifications are triggered from NotifyPacket. | 42 // These notifications are triggered from NotifyPacket. |
42 virtual void OnTcpOpen(cricket::PseudoTcp* tcp) OVERRIDE; | 43 virtual void OnTcpOpen(cricket::PseudoTcp* tcp) OVERRIDE; |
43 virtual void OnTcpReadable(cricket::PseudoTcp* tcp) OVERRIDE; | 44 virtual void OnTcpReadable(cricket::PseudoTcp* tcp) OVERRIDE; |
44 virtual void OnTcpWriteable(cricket::PseudoTcp* tcp) OVERRIDE; | 45 virtual void OnTcpWriteable(cricket::PseudoTcp* tcp) OVERRIDE; |
45 // This is triggered by NotifyClock or NotifyPacket. | 46 // This is triggered by NotifyClock or NotifyPacket. |
46 virtual void OnTcpClosed(cricket::PseudoTcp* tcp, uint32 error) OVERRIDE; | 47 virtual void OnTcpClosed(cricket::PseudoTcp* tcp, uint32 error) OVERRIDE; |
(...skipping 14 matching lines...) Expand all Loading... |
61 | 62 |
62 // These may trigger callbacks, so the holder must hold a reference on | 63 // These may trigger callbacks, so the holder must hold a reference on |
63 // the stack while calling them. | 64 // the stack while calling them. |
64 void DoReadFromSocket(); | 65 void DoReadFromSocket(); |
65 void HandleReadResults(int result); | 66 void HandleReadResults(int result); |
66 void HandleTcpClock(); | 67 void HandleTcpClock(); |
67 | 68 |
68 // This re-sets |timer| without triggering callbacks. | 69 // This re-sets |timer| without triggering callbacks. |
69 void AdjustClock(); | 70 void AdjustClock(); |
70 | 71 |
71 net::OldCompletionCallback* connect_callback_; | 72 net::OldCompletionCallback* old_connect_callback_; |
| 73 net::CompletionCallback connect_callback_; |
72 net::OldCompletionCallback* read_callback_; | 74 net::OldCompletionCallback* read_callback_; |
73 net::OldCompletionCallback* write_callback_; | 75 net::OldCompletionCallback* write_callback_; |
74 | 76 |
75 cricket::PseudoTcp pseudo_tcp_; | 77 cricket::PseudoTcp pseudo_tcp_; |
76 scoped_ptr<net::Socket> socket_; | 78 scoped_ptr<net::Socket> socket_; |
77 | 79 |
78 scoped_refptr<net::IOBuffer> read_buffer_; | 80 scoped_refptr<net::IOBuffer> read_buffer_; |
79 int read_buffer_size_; | 81 int read_buffer_size_; |
80 scoped_refptr<net::IOBuffer> write_buffer_; | 82 scoped_refptr<net::IOBuffer> write_buffer_; |
81 int write_buffer_size_; | 83 int write_buffer_size_; |
82 | 84 |
83 bool socket_write_pending_; | 85 bool socket_write_pending_; |
84 scoped_refptr<net::IOBuffer> socket_read_buffer_; | 86 scoped_refptr<net::IOBuffer> socket_read_buffer_; |
85 | 87 |
86 net::OldCompletionCallbackImpl<Core> socket_read_callback_; | 88 net::OldCompletionCallbackImpl<Core> socket_read_callback_; |
87 net::OldCompletionCallbackImpl<Core> socket_write_callback_; | 89 net::OldCompletionCallbackImpl<Core> socket_write_callback_; |
88 | 90 |
89 base::OneShotTimer<Core> timer_; | 91 base::OneShotTimer<Core> timer_; |
90 | 92 |
91 DISALLOW_COPY_AND_ASSIGN(Core); | 93 DISALLOW_COPY_AND_ASSIGN(Core); |
92 }; | 94 }; |
93 | 95 |
94 | 96 |
95 PseudoTcpAdapter::Core::Core(net::Socket* socket) | 97 PseudoTcpAdapter::Core::Core(net::Socket* socket) |
96 : connect_callback_(NULL), | 98 : old_connect_callback_(NULL), |
97 read_callback_(NULL), | 99 read_callback_(NULL), |
98 write_callback_(NULL), | 100 write_callback_(NULL), |
99 ALLOW_THIS_IN_INITIALIZER_LIST(pseudo_tcp_(this, 0)), | 101 ALLOW_THIS_IN_INITIALIZER_LIST(pseudo_tcp_(this, 0)), |
100 socket_(socket), | 102 socket_(socket), |
101 socket_write_pending_(false), | 103 socket_write_pending_(false), |
102 ALLOW_THIS_IN_INITIALIZER_LIST( | 104 ALLOW_THIS_IN_INITIALIZER_LIST( |
103 socket_read_callback_(this, &PseudoTcpAdapter::Core::OnRead)), | 105 socket_read_callback_(this, &PseudoTcpAdapter::Core::OnRead)), |
104 ALLOW_THIS_IN_INITIALIZER_LIST( | 106 ALLOW_THIS_IN_INITIALIZER_LIST( |
105 socket_write_callback_(this, &PseudoTcpAdapter::Core::OnWritten)) { | 107 socket_write_callback_(this, &PseudoTcpAdapter::Core::OnWritten)) { |
106 // Doesn't trigger callbacks. | 108 // Doesn't trigger callbacks. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 // Reference the Core in case a callback deletes the adapter. | 166 // Reference the Core in case a callback deletes the adapter. |
165 scoped_refptr<Core> core(this); | 167 scoped_refptr<Core> core(this); |
166 | 168 |
167 // Start the connection attempt. | 169 // Start the connection attempt. |
168 int result = pseudo_tcp_.Connect(); | 170 int result = pseudo_tcp_.Connect(); |
169 if (result < 0) | 171 if (result < 0) |
170 return net::ERR_FAILED; | 172 return net::ERR_FAILED; |
171 | 173 |
172 AdjustClock(); | 174 AdjustClock(); |
173 | 175 |
| 176 old_connect_callback_ = callback; |
| 177 connect_callback_.Reset(); |
| 178 DoReadFromSocket(); |
| 179 |
| 180 return net::ERR_IO_PENDING; |
| 181 } |
| 182 |
| 183 int PseudoTcpAdapter::Core::Connect(const net::CompletionCallback& callback) { |
| 184 DCHECK_EQ(pseudo_tcp_.State(), cricket::PseudoTcp::TCP_LISTEN); |
| 185 |
| 186 // Reference the Core in case a callback deletes the adapter. |
| 187 scoped_refptr<Core> core(this); |
| 188 |
| 189 // Start the connection attempt. |
| 190 int result = pseudo_tcp_.Connect(); |
| 191 if (result < 0) |
| 192 return net::ERR_FAILED; |
| 193 |
| 194 AdjustClock(); |
| 195 |
| 196 old_connect_callback_ = NULL; |
174 connect_callback_ = callback; | 197 connect_callback_ = callback; |
175 DoReadFromSocket(); | 198 DoReadFromSocket(); |
176 | 199 |
177 return net::ERR_IO_PENDING; | 200 return net::ERR_IO_PENDING; |
178 } | 201 } |
179 | 202 |
180 void PseudoTcpAdapter::Core::Disconnect() { | 203 void PseudoTcpAdapter::Core::Disconnect() { |
181 // Don't dispatch outstanding callbacks, as mandated by net::StreamSocket. | 204 // Don't dispatch outstanding callbacks, as mandated by net::StreamSocket. |
182 read_callback_ = NULL; | 205 read_callback_ = NULL; |
183 read_buffer_ = NULL; | 206 read_buffer_ = NULL; |
184 write_callback_ = NULL; | 207 write_callback_ = NULL; |
185 write_buffer_ = NULL; | 208 write_buffer_ = NULL; |
186 connect_callback_ = NULL; | 209 old_connect_callback_ = NULL; |
| 210 connect_callback_.Reset(); |
187 | 211 |
188 // TODO(wez): Connect should succeed if called after Disconnect, which | 212 // TODO(wez): Connect should succeed if called after Disconnect, which |
189 // PseudoTcp doesn't support, so we need to teardown the internal PseudoTcp | 213 // PseudoTcp doesn't support, so we need to teardown the internal PseudoTcp |
190 // and create a new one in Connect. | 214 // and create a new one in Connect. |
191 // TODO(wez): Close sets a shutdown flag inside PseudoTcp but has no other | 215 // TODO(wez): Close sets a shutdown flag inside PseudoTcp but has no other |
192 // effect. This should be addressed in PseudoTcp, really. | 216 // effect. This should be addressed in PseudoTcp, really. |
193 // In the meantime we can fake OnTcpClosed notification and tear down the | 217 // In the meantime we can fake OnTcpClosed notification and tear down the |
194 // PseudoTcp. | 218 // PseudoTcp. |
195 pseudo_tcp_.Close(true); | 219 pseudo_tcp_.Close(true); |
196 } | 220 } |
197 | 221 |
198 bool PseudoTcpAdapter::Core::IsConnected() const { | 222 bool PseudoTcpAdapter::Core::IsConnected() const { |
199 return pseudo_tcp_.State() == PseudoTcp::TCP_ESTABLISHED; | 223 return pseudo_tcp_.State() == PseudoTcp::TCP_ESTABLISHED; |
200 } | 224 } |
201 | 225 |
202 void PseudoTcpAdapter::Core::OnTcpOpen(PseudoTcp* tcp) { | 226 void PseudoTcpAdapter::Core::OnTcpOpen(PseudoTcp* tcp) { |
203 DCHECK(tcp == &pseudo_tcp_); | 227 DCHECK(tcp == &pseudo_tcp_); |
204 | 228 |
205 if (connect_callback_) { | 229 if (old_connect_callback_) { |
206 net::OldCompletionCallback* callback = connect_callback_; | 230 net::OldCompletionCallback* callback = old_connect_callback_; |
207 connect_callback_ = NULL; | 231 old_connect_callback_ = NULL; |
208 callback->Run(net::OK); | 232 callback->Run(net::OK); |
| 233 } else if (!connect_callback_.is_null()) { |
| 234 net::CompletionCallback callback = connect_callback_; |
| 235 connect_callback_.Reset(); |
| 236 callback.Run(net::OK); |
209 } | 237 } |
210 | 238 |
211 OnTcpReadable(tcp); | 239 OnTcpReadable(tcp); |
212 OnTcpWriteable(tcp); | 240 OnTcpWriteable(tcp); |
213 } | 241 } |
214 | 242 |
215 void PseudoTcpAdapter::Core::OnTcpReadable(PseudoTcp* tcp) { | 243 void PseudoTcpAdapter::Core::OnTcpReadable(PseudoTcp* tcp) { |
216 DCHECK_EQ(tcp, &pseudo_tcp_); | 244 DCHECK_EQ(tcp, &pseudo_tcp_); |
217 if (!read_callback_) | 245 if (!read_callback_) |
218 return; | 246 return; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 | 278 |
251 net::OldCompletionCallback* callback = write_callback_; | 279 net::OldCompletionCallback* callback = write_callback_; |
252 write_callback_ = NULL; | 280 write_callback_ = NULL; |
253 write_buffer_ = NULL; | 281 write_buffer_ = NULL; |
254 callback->Run(result); | 282 callback->Run(result); |
255 } | 283 } |
256 | 284 |
257 void PseudoTcpAdapter::Core::OnTcpClosed(PseudoTcp* tcp, uint32 error) { | 285 void PseudoTcpAdapter::Core::OnTcpClosed(PseudoTcp* tcp, uint32 error) { |
258 DCHECK_EQ(tcp, &pseudo_tcp_); | 286 DCHECK_EQ(tcp, &pseudo_tcp_); |
259 | 287 |
260 if (connect_callback_) { | 288 if (old_connect_callback_) { |
261 net::OldCompletionCallback* callback = connect_callback_; | 289 net::OldCompletionCallback* callback = old_connect_callback_; |
262 connect_callback_ = NULL; | 290 old_connect_callback_ = NULL; |
263 callback->Run(net::MapSystemError(error)); | 291 callback->Run(net::MapSystemError(error)); |
| 292 } else if (!connect_callback_.is_null()) { |
| 293 net::CompletionCallback callback = connect_callback_; |
| 294 connect_callback_.Reset(); |
| 295 callback.Run(net::MapSystemError(error)); |
264 } | 296 } |
265 | 297 |
266 if (read_callback_) { | 298 if (read_callback_) { |
267 net::OldCompletionCallback* callback = read_callback_; | 299 net::OldCompletionCallback* callback = read_callback_; |
268 read_callback_ = NULL; | 300 read_callback_ = NULL; |
269 callback->Run(net::MapSystemError(error)); | 301 callback->Run(net::MapSystemError(error)); |
270 } | 302 } |
271 | 303 |
272 if (write_callback_) { | 304 if (write_callback_) { |
273 net::OldCompletionCallback* callback = write_callback_; | 305 net::OldCompletionCallback* callback = write_callback_; |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 int PseudoTcpAdapter::Connect(net::OldCompletionCallback* callback) { | 455 int PseudoTcpAdapter::Connect(net::OldCompletionCallback* callback) { |
424 DCHECK(CalledOnValidThread()); | 456 DCHECK(CalledOnValidThread()); |
425 | 457 |
426 // net::StreamSocket requires that Connect return OK if already connected. | 458 // net::StreamSocket requires that Connect return OK if already connected. |
427 if (IsConnected()) | 459 if (IsConnected()) |
428 return net::OK; | 460 return net::OK; |
429 | 461 |
430 return core_->Connect(callback); | 462 return core_->Connect(callback); |
431 } | 463 } |
432 | 464 |
| 465 int PseudoTcpAdapter::Connect(const net::CompletionCallback& callback) { |
| 466 DCHECK(CalledOnValidThread()); |
| 467 |
| 468 // net::StreamSocket requires that Connect return OK if already connected. |
| 469 if (IsConnected()) |
| 470 return net::OK; |
| 471 |
| 472 return core_->Connect(callback); |
| 473 } |
| 474 |
433 void PseudoTcpAdapter::Disconnect() { | 475 void PseudoTcpAdapter::Disconnect() { |
434 DCHECK(CalledOnValidThread()); | 476 DCHECK(CalledOnValidThread()); |
435 core_->Disconnect(); | 477 core_->Disconnect(); |
436 } | 478 } |
437 | 479 |
438 bool PseudoTcpAdapter::IsConnected() const { | 480 bool PseudoTcpAdapter::IsConnected() const { |
439 return core_->IsConnected(); | 481 return core_->IsConnected(); |
440 } | 482 } |
441 | 483 |
442 bool PseudoTcpAdapter::IsConnectedAndIdle() const { | 484 bool PseudoTcpAdapter::IsConnectedAndIdle() const { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 DCHECK(CalledOnValidThread()); | 543 DCHECK(CalledOnValidThread()); |
502 core_->SetAckDelay(delay_ms); | 544 core_->SetAckDelay(delay_ms); |
503 } | 545 } |
504 | 546 |
505 void PseudoTcpAdapter::SetNoDelay(bool no_delay) { | 547 void PseudoTcpAdapter::SetNoDelay(bool no_delay) { |
506 DCHECK(CalledOnValidThread()); | 548 DCHECK(CalledOnValidThread()); |
507 core_->SetNoDelay(no_delay); | 549 core_->SetNoDelay(no_delay); |
508 } | 550 } |
509 | 551 |
510 } // namespace jingle_glue | 552 } // namespace jingle_glue |
OLD | NEW |