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

Side by Side Diff: jingle/glue/pseudotcp_adapter.cc

Issue 8801005: base::Bind: Convert Socket::Read. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review fixes. Created 9 years 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
« no previous file with comments | « jingle/glue/pseudotcp_adapter.h ('k') | jingle/glue/pseudotcp_adapter_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "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 13 matching lines...) Expand all
24 24
25 class PseudoTcpAdapter::Core : public cricket::IPseudoTcpNotify, 25 class PseudoTcpAdapter::Core : public cricket::IPseudoTcpNotify,
26 public base::RefCounted<Core> { 26 public base::RefCounted<Core> {
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 Read(net::IOBuffer* buffer, int buffer_size,
35 const net::CompletionCallback& callback);
34 int Write(net::IOBuffer* buffer, int buffer_size, 36 int Write(net::IOBuffer* buffer, int buffer_size,
35 net::OldCompletionCallback* callback); 37 net::OldCompletionCallback* callback);
36 int Connect(net::OldCompletionCallback* callback); 38 int Connect(net::OldCompletionCallback* callback);
37 int Connect(const net::CompletionCallback& callback); 39 int Connect(const net::CompletionCallback& callback);
38 void Disconnect(); 40 void Disconnect();
39 bool IsConnected() const; 41 bool IsConnected() const;
40 42
41 // cricket::IPseudoTcpNotify interface. 43 // cricket::IPseudoTcpNotify interface.
42 // These notifications are triggered from NotifyPacket. 44 // These notifications are triggered from NotifyPacket.
43 virtual void OnTcpOpen(cricket::PseudoTcp* tcp) OVERRIDE; 45 virtual void OnTcpOpen(cricket::PseudoTcp* tcp) OVERRIDE;
(...skipping 20 matching lines...) Expand all
64 // the stack while calling them. 66 // the stack while calling them.
65 void DoReadFromSocket(); 67 void DoReadFromSocket();
66 void HandleReadResults(int result); 68 void HandleReadResults(int result);
67 void HandleTcpClock(); 69 void HandleTcpClock();
68 70
69 // This re-sets |timer| without triggering callbacks. 71 // This re-sets |timer| without triggering callbacks.
70 void AdjustClock(); 72 void AdjustClock();
71 73
72 net::OldCompletionCallback* old_connect_callback_; 74 net::OldCompletionCallback* old_connect_callback_;
73 net::CompletionCallback connect_callback_; 75 net::CompletionCallback connect_callback_;
74 net::OldCompletionCallback* read_callback_; 76 net::OldCompletionCallback* old_read_callback_;
77 net::CompletionCallback read_callback_;
75 net::OldCompletionCallback* write_callback_; 78 net::OldCompletionCallback* write_callback_;
76 79
77 cricket::PseudoTcp pseudo_tcp_; 80 cricket::PseudoTcp pseudo_tcp_;
78 scoped_ptr<net::Socket> socket_; 81 scoped_ptr<net::Socket> socket_;
79 82
80 scoped_refptr<net::IOBuffer> read_buffer_; 83 scoped_refptr<net::IOBuffer> read_buffer_;
81 int read_buffer_size_; 84 int read_buffer_size_;
82 scoped_refptr<net::IOBuffer> write_buffer_; 85 scoped_refptr<net::IOBuffer> write_buffer_;
83 int write_buffer_size_; 86 int write_buffer_size_;
84 87
85 bool socket_write_pending_; 88 bool socket_write_pending_;
86 scoped_refptr<net::IOBuffer> socket_read_buffer_; 89 scoped_refptr<net::IOBuffer> socket_read_buffer_;
87 90
88 net::OldCompletionCallbackImpl<Core> socket_read_callback_; 91 net::OldCompletionCallbackImpl<Core> socket_read_callback_;
89 net::OldCompletionCallbackImpl<Core> socket_write_callback_; 92 net::OldCompletionCallbackImpl<Core> socket_write_callback_;
90 93
91 base::OneShotTimer<Core> timer_; 94 base::OneShotTimer<Core> timer_;
92 95
93 DISALLOW_COPY_AND_ASSIGN(Core); 96 DISALLOW_COPY_AND_ASSIGN(Core);
94 }; 97 };
95 98
96 99
97 PseudoTcpAdapter::Core::Core(net::Socket* socket) 100 PseudoTcpAdapter::Core::Core(net::Socket* socket)
98 : old_connect_callback_(NULL), 101 : old_connect_callback_(NULL),
99 read_callback_(NULL), 102 old_read_callback_(NULL),
100 write_callback_(NULL), 103 write_callback_(NULL),
101 ALLOW_THIS_IN_INITIALIZER_LIST(pseudo_tcp_(this, 0)), 104 ALLOW_THIS_IN_INITIALIZER_LIST(pseudo_tcp_(this, 0)),
102 socket_(socket), 105 socket_(socket),
103 socket_write_pending_(false), 106 socket_write_pending_(false),
104 ALLOW_THIS_IN_INITIALIZER_LIST( 107 ALLOW_THIS_IN_INITIALIZER_LIST(
105 socket_read_callback_(this, &PseudoTcpAdapter::Core::OnRead)), 108 socket_read_callback_(this, &PseudoTcpAdapter::Core::OnRead)),
106 ALLOW_THIS_IN_INITIALIZER_LIST( 109 ALLOW_THIS_IN_INITIALIZER_LIST(
107 socket_write_callback_(this, &PseudoTcpAdapter::Core::OnWritten)) { 110 socket_write_callback_(this, &PseudoTcpAdapter::Core::OnWritten)) {
108 // Doesn't trigger callbacks. 111 // Doesn't trigger callbacks.
109 pseudo_tcp_.NotifyMTU(kDefaultMtu); 112 pseudo_tcp_.NotifyMTU(kDefaultMtu);
110 } 113 }
111 114
112 PseudoTcpAdapter::Core::~Core() { 115 PseudoTcpAdapter::Core::~Core() {
113 } 116 }
114 117
115 int PseudoTcpAdapter::Core::Read(net::IOBuffer* buffer, int buffer_size, 118 int PseudoTcpAdapter::Core::Read(net::IOBuffer* buffer, int buffer_size,
116 net::OldCompletionCallback* callback) { 119 net::OldCompletionCallback* callback) {
117 DCHECK(!read_callback_); 120 DCHECK(!old_read_callback_ && read_callback_.is_null());
118 121
119 // Reference the Core in case a callback deletes the adapter. 122 // Reference the Core in case a callback deletes the adapter.
120 scoped_refptr<Core> core(this); 123 scoped_refptr<Core> core(this);
124
125 int result = pseudo_tcp_.Recv(buffer->data(), buffer_size);
126 if (result < 0) {
127 result = net::MapSystemError(pseudo_tcp_.GetError());
128 DCHECK(result < 0);
129 }
130
131 if (result == net::ERR_IO_PENDING) {
132 read_buffer_ = buffer;
133 read_buffer_size_ = buffer_size;
134 old_read_callback_ = callback;
135 }
136
137 AdjustClock();
138
139 return result;
140 }
141 int PseudoTcpAdapter::Core::Read(net::IOBuffer* buffer, int buffer_size,
142 const net::CompletionCallback& callback) {
143 DCHECK(!old_read_callback_ && read_callback_.is_null());
144
145 // Reference the Core in case a callback deletes the adapter.
146 scoped_refptr<Core> core(this);
121 147
122 int result = pseudo_tcp_.Recv(buffer->data(), buffer_size); 148 int result = pseudo_tcp_.Recv(buffer->data(), buffer_size);
123 if (result < 0) { 149 if (result < 0) {
124 result = net::MapSystemError(pseudo_tcp_.GetError()); 150 result = net::MapSystemError(pseudo_tcp_.GetError());
125 DCHECK(result < 0); 151 DCHECK(result < 0);
126 } 152 }
127 153
128 if (result == net::ERR_IO_PENDING) { 154 if (result == net::ERR_IO_PENDING) {
129 read_buffer_ = buffer; 155 read_buffer_ = buffer;
130 read_buffer_size_ = buffer_size; 156 read_buffer_size_ = buffer_size;
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 221
196 old_connect_callback_ = NULL; 222 old_connect_callback_ = NULL;
197 connect_callback_ = callback; 223 connect_callback_ = callback;
198 DoReadFromSocket(); 224 DoReadFromSocket();
199 225
200 return net::ERR_IO_PENDING; 226 return net::ERR_IO_PENDING;
201 } 227 }
202 228
203 void PseudoTcpAdapter::Core::Disconnect() { 229 void PseudoTcpAdapter::Core::Disconnect() {
204 // Don't dispatch outstanding callbacks, as mandated by net::StreamSocket. 230 // Don't dispatch outstanding callbacks, as mandated by net::StreamSocket.
205 read_callback_ = NULL; 231 old_read_callback_ = NULL;
232 read_callback_.Reset();
206 read_buffer_ = NULL; 233 read_buffer_ = NULL;
207 write_callback_ = NULL; 234 write_callback_ = NULL;
208 write_buffer_ = NULL; 235 write_buffer_ = NULL;
209 old_connect_callback_ = NULL; 236 old_connect_callback_ = NULL;
210 connect_callback_.Reset(); 237 connect_callback_.Reset();
211 238
212 // TODO(wez): Connect should succeed if called after Disconnect, which 239 // TODO(wez): Connect should succeed if called after Disconnect, which
213 // PseudoTcp doesn't support, so we need to teardown the internal PseudoTcp 240 // PseudoTcp doesn't support, so we need to teardown the internal PseudoTcp
214 // and create a new one in Connect. 241 // and create a new one in Connect.
215 // TODO(wez): Close sets a shutdown flag inside PseudoTcp but has no other 242 // TODO(wez): Close sets a shutdown flag inside PseudoTcp but has no other
(...skipping 19 matching lines...) Expand all
235 connect_callback_.Reset(); 262 connect_callback_.Reset();
236 callback.Run(net::OK); 263 callback.Run(net::OK);
237 } 264 }
238 265
239 OnTcpReadable(tcp); 266 OnTcpReadable(tcp);
240 OnTcpWriteable(tcp); 267 OnTcpWriteable(tcp);
241 } 268 }
242 269
243 void PseudoTcpAdapter::Core::OnTcpReadable(PseudoTcp* tcp) { 270 void PseudoTcpAdapter::Core::OnTcpReadable(PseudoTcp* tcp) {
244 DCHECK_EQ(tcp, &pseudo_tcp_); 271 DCHECK_EQ(tcp, &pseudo_tcp_);
245 if (!read_callback_) 272 if (!old_read_callback_ && read_callback_.is_null())
246 return; 273 return;
247 274
248 int result = pseudo_tcp_.Recv(read_buffer_->data(), read_buffer_size_); 275 int result = pseudo_tcp_.Recv(read_buffer_->data(), read_buffer_size_);
249 if (result < 0) { 276 if (result < 0) {
250 result = net::MapSystemError(pseudo_tcp_.GetError()); 277 result = net::MapSystemError(pseudo_tcp_.GetError());
251 DCHECK(result < 0); 278 DCHECK(result < 0);
252 if (result == net::ERR_IO_PENDING) 279 if (result == net::ERR_IO_PENDING)
253 return; 280 return;
254 } 281 }
255 282
256 AdjustClock(); 283 AdjustClock();
257 284
258 net::OldCompletionCallback* callback = read_callback_; 285 if (old_read_callback_) {
259 read_callback_ = NULL; 286 net::OldCompletionCallback* callback = old_read_callback_;
260 read_buffer_ = NULL; 287 old_read_callback_ = NULL;
261 callback->Run(result); 288 read_buffer_ = NULL;
289 callback->Run(result);
290 } else {
291 net::CompletionCallback callback = read_callback_;
292 read_callback_.Reset();
293 read_buffer_ = NULL;
294 callback.Run(result);
295 }
262 } 296 }
263 297
264 void PseudoTcpAdapter::Core::OnTcpWriteable(PseudoTcp* tcp) { 298 void PseudoTcpAdapter::Core::OnTcpWriteable(PseudoTcp* tcp) {
265 DCHECK_EQ(tcp, &pseudo_tcp_); 299 DCHECK_EQ(tcp, &pseudo_tcp_);
266 if (!write_callback_) 300 if (!write_callback_)
267 return; 301 return;
268 302
269 int result = pseudo_tcp_.Send(write_buffer_->data(), write_buffer_size_); 303 int result = pseudo_tcp_.Send(write_buffer_->data(), write_buffer_size_);
270 if (result < 0) { 304 if (result < 0) {
271 result = net::MapSystemError(pseudo_tcp_.GetError()); 305 result = net::MapSystemError(pseudo_tcp_.GetError());
(...skipping 16 matching lines...) Expand all
288 if (old_connect_callback_) { 322 if (old_connect_callback_) {
289 net::OldCompletionCallback* callback = old_connect_callback_; 323 net::OldCompletionCallback* callback = old_connect_callback_;
290 old_connect_callback_ = NULL; 324 old_connect_callback_ = NULL;
291 callback->Run(net::MapSystemError(error)); 325 callback->Run(net::MapSystemError(error));
292 } else if (!connect_callback_.is_null()) { 326 } else if (!connect_callback_.is_null()) {
293 net::CompletionCallback callback = connect_callback_; 327 net::CompletionCallback callback = connect_callback_;
294 connect_callback_.Reset(); 328 connect_callback_.Reset();
295 callback.Run(net::MapSystemError(error)); 329 callback.Run(net::MapSystemError(error));
296 } 330 }
297 331
298 if (read_callback_) { 332 if (old_read_callback_) {
299 net::OldCompletionCallback* callback = read_callback_; 333 net::OldCompletionCallback* callback = old_read_callback_;
300 read_callback_ = NULL; 334 old_read_callback_ = NULL;
301 callback->Run(net::MapSystemError(error)); 335 callback->Run(net::MapSystemError(error));
336 } else if (!read_callback_.is_null()) {
337 net::CompletionCallback callback = read_callback_;
338 read_callback_.Reset();
339 callback.Run(net::MapSystemError(error));
302 } 340 }
303 341
304 if (write_callback_) { 342 if (write_callback_) {
305 net::OldCompletionCallback* callback = write_callback_; 343 net::OldCompletionCallback* callback = write_callback_;
306 write_callback_ = NULL; 344 write_callback_ = NULL;
307 callback->Run(net::MapSystemError(error)); 345 callback->Run(net::MapSystemError(error));
308 } 346 }
309 } 347 }
310 348
311 void PseudoTcpAdapter::Core::SetAckDelay(int delay_ms) { 349 void PseudoTcpAdapter::Core::SetAckDelay(int delay_ms) {
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 462
425 PseudoTcpAdapter::~PseudoTcpAdapter() { 463 PseudoTcpAdapter::~PseudoTcpAdapter() {
426 Disconnect(); 464 Disconnect();
427 } 465 }
428 466
429 int PseudoTcpAdapter::Read(net::IOBuffer* buffer, int buffer_size, 467 int PseudoTcpAdapter::Read(net::IOBuffer* buffer, int buffer_size,
430 net::OldCompletionCallback* callback) { 468 net::OldCompletionCallback* callback) {
431 DCHECK(CalledOnValidThread()); 469 DCHECK(CalledOnValidThread());
432 return core_->Read(buffer, buffer_size, callback); 470 return core_->Read(buffer, buffer_size, callback);
433 } 471 }
472 int PseudoTcpAdapter::Read(net::IOBuffer* buffer, int buffer_size,
473 const net::CompletionCallback& callback) {
474 DCHECK(CalledOnValidThread());
475 return core_->Read(buffer, buffer_size, callback);
476 }
434 477
435 int PseudoTcpAdapter::Write(net::IOBuffer* buffer, int buffer_size, 478 int PseudoTcpAdapter::Write(net::IOBuffer* buffer, int buffer_size,
436 net::OldCompletionCallback* callback) { 479 net::OldCompletionCallback* callback) {
437 DCHECK(CalledOnValidThread()); 480 DCHECK(CalledOnValidThread());
438 return core_->Write(buffer, buffer_size, callback); 481 return core_->Write(buffer, buffer_size, callback);
439 } 482 }
440 483
441 bool PseudoTcpAdapter::SetReceiveBufferSize(int32 size) { 484 bool PseudoTcpAdapter::SetReceiveBufferSize(int32 size) {
442 DCHECK(CalledOnValidThread()); 485 DCHECK(CalledOnValidThread());
443 486
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
543 DCHECK(CalledOnValidThread()); 586 DCHECK(CalledOnValidThread());
544 core_->SetAckDelay(delay_ms); 587 core_->SetAckDelay(delay_ms);
545 } 588 }
546 589
547 void PseudoTcpAdapter::SetNoDelay(bool no_delay) { 590 void PseudoTcpAdapter::SetNoDelay(bool no_delay) {
548 DCHECK(CalledOnValidThread()); 591 DCHECK(CalledOnValidThread());
549 core_->SetNoDelay(no_delay); 592 core_->SetNoDelay(no_delay);
550 } 593 }
551 594
552 } // namespace jingle_glue 595 } // namespace jingle_glue
OLDNEW
« no previous file with comments | « jingle/glue/pseudotcp_adapter.h ('k') | jingle/glue/pseudotcp_adapter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698