OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "remoting/protocol/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/time.h" | 9 #include "base/time/time.h" |
10 #include "base/timer/timer.h" | 10 #include "base/timer/timer.h" |
11 #include "net/base/address_list.h" | 11 #include "net/base/address_list.h" |
12 #include "net/base/completion_callback.h" | 12 #include "net/base/completion_callback.h" |
13 #include "net/base/io_buffer.h" | 13 #include "net/base/io_buffer.h" |
14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
15 #include "net/base/net_util.h" | 15 #include "net/base/net_util.h" |
16 | 16 |
17 using cricket::PseudoTcp; | 17 using cricket::PseudoTcp; |
18 | 18 |
19 namespace { | 19 namespace { |
20 const int kReadBufferSize = 65536; // Maximum size of a packet. | 20 const int kReadBufferSize = 65536; // Maximum size of a packet. |
21 const uint16 kDefaultMtu = 1280; | 21 const uint16 kDefaultMtu = 1280; |
22 } // namespace | 22 } // namespace |
23 | 23 |
24 namespace jingle_glue { | 24 namespace remoting { |
| 25 namespace protocol { |
25 | 26 |
26 class PseudoTcpAdapter::Core : public cricket::IPseudoTcpNotify, | 27 class PseudoTcpAdapter::Core : public cricket::IPseudoTcpNotify, |
27 public base::RefCounted<Core> { | 28 public base::RefCounted<Core> { |
28 public: | 29 public: |
29 explicit Core(net::Socket* socket); | 30 explicit Core(scoped_ptr<net::Socket> socket); |
30 | 31 |
31 // Functions used to implement net::StreamSocket. | 32 // Functions used to implement net::StreamSocket. |
32 int Read(net::IOBuffer* buffer, int buffer_size, | 33 int Read(net::IOBuffer* buffer, int buffer_size, |
33 const net::CompletionCallback& callback); | 34 const net::CompletionCallback& callback); |
34 int Write(net::IOBuffer* buffer, int buffer_size, | 35 int Write(net::IOBuffer* buffer, int buffer_size, |
35 const net::CompletionCallback& callback); | 36 const net::CompletionCallback& callback); |
36 int Connect(const net::CompletionCallback& callback); | 37 int Connect(const net::CompletionCallback& callback); |
37 void Disconnect(); | 38 void Disconnect(); |
38 bool IsConnected() const; | 39 bool IsConnected() const; |
39 | 40 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 | 106 |
106 bool socket_write_pending_; | 107 bool socket_write_pending_; |
107 scoped_refptr<net::IOBuffer> socket_read_buffer_; | 108 scoped_refptr<net::IOBuffer> socket_read_buffer_; |
108 | 109 |
109 base::OneShotTimer<Core> timer_; | 110 base::OneShotTimer<Core> timer_; |
110 | 111 |
111 DISALLOW_COPY_AND_ASSIGN(Core); | 112 DISALLOW_COPY_AND_ASSIGN(Core); |
112 }; | 113 }; |
113 | 114 |
114 | 115 |
115 PseudoTcpAdapter::Core::Core(net::Socket* socket) | 116 PseudoTcpAdapter::Core::Core(scoped_ptr<net::Socket> socket) |
116 : pseudo_tcp_(this, 0), | 117 : pseudo_tcp_(this, 0), |
117 socket_(socket), | 118 socket_(socket.Pass()), |
118 write_waits_for_send_(false), | 119 write_waits_for_send_(false), |
119 waiting_write_position_(false), | 120 waiting_write_position_(false), |
120 socket_write_pending_(false) { | 121 socket_write_pending_(false) { |
121 // Doesn't trigger callbacks. | 122 // Doesn't trigger callbacks. |
122 pseudo_tcp_.NotifyMTU(kDefaultMtu); | 123 pseudo_tcp_.NotifyMTU(kDefaultMtu); |
123 } | 124 } |
124 | 125 |
125 PseudoTcpAdapter::Core::~Core() { | 126 PseudoTcpAdapter::Core::~Core() { |
126 } | 127 } |
127 | 128 |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 return IPseudoTcpNotify::WR_SUCCESS; | 357 return IPseudoTcpNotify::WR_SUCCESS; |
357 | 358 |
358 scoped_refptr<net::IOBuffer> write_buffer = new net::IOBuffer(len); | 359 scoped_refptr<net::IOBuffer> write_buffer = new net::IOBuffer(len); |
359 memcpy(write_buffer->data(), buffer, len); | 360 memcpy(write_buffer->data(), buffer, len); |
360 | 361 |
361 // Our underlying socket is datagram-oriented, which means it should either | 362 // Our underlying socket is datagram-oriented, which means it should either |
362 // send exactly as many bytes as we requested, or fail. | 363 // send exactly as many bytes as we requested, or fail. |
363 int result; | 364 int result; |
364 if (socket_.get()) { | 365 if (socket_.get()) { |
365 result = socket_->Write( | 366 result = socket_->Write( |
366 write_buffer.get(), | 367 write_buffer.get(), len, |
367 len, | |
368 base::Bind(&PseudoTcpAdapter::Core::OnWritten, base::Unretained(this))); | 368 base::Bind(&PseudoTcpAdapter::Core::OnWritten, base::Unretained(this))); |
369 } else { | 369 } else { |
370 result = net::ERR_CONNECTION_CLOSED; | 370 result = net::ERR_CONNECTION_CLOSED; |
371 } | 371 } |
372 if (result == net::ERR_IO_PENDING) { | 372 if (result == net::ERR_IO_PENDING) { |
373 socket_write_pending_ = true; | 373 socket_write_pending_ = true; |
374 return IPseudoTcpNotify::WR_SUCCESS; | 374 return IPseudoTcpNotify::WR_SUCCESS; |
375 } else if (result == net::ERR_MSG_TOO_BIG) { | 375 } else if (result == net::ERR_MSG_TOO_BIG) { |
376 return IPseudoTcpNotify::WR_TOO_LARGE; | 376 return IPseudoTcpNotify::WR_TOO_LARGE; |
377 } else if (result < 0) { | 377 } else if (result < 0) { |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 net::CompletionCallback callback = write_callback_; | 456 net::CompletionCallback callback = write_callback_; |
457 write_callback_.Reset(); | 457 write_callback_.Reset(); |
458 write_buffer_ = NULL; | 458 write_buffer_ = NULL; |
459 callback.Run(last_write_result_); | 459 callback.Run(last_write_result_); |
460 } | 460 } |
461 } | 461 } |
462 } | 462 } |
463 | 463 |
464 // Public interface implemention. | 464 // Public interface implemention. |
465 | 465 |
466 PseudoTcpAdapter::PseudoTcpAdapter(net::Socket* socket) | 466 PseudoTcpAdapter::PseudoTcpAdapter(scoped_ptr<net::Socket> socket) |
467 : core_(new Core(socket)) { | 467 : core_(new Core(socket.Pass())) { |
468 } | 468 } |
469 | 469 |
470 PseudoTcpAdapter::~PseudoTcpAdapter() { | 470 PseudoTcpAdapter::~PseudoTcpAdapter() { |
471 Disconnect(); | 471 Disconnect(); |
472 | 472 |
473 // Make sure that the underlying socket is destroyed before PseudoTcp. | 473 // Make sure that the underlying socket is destroyed before PseudoTcp. |
474 core_->DeleteSocket(); | 474 core_->DeleteSocket(); |
475 } | 475 } |
476 | 476 |
477 int PseudoTcpAdapter::Read(net::IOBuffer* buffer, int buffer_size, | 477 int PseudoTcpAdapter::Read(net::IOBuffer* buffer, int buffer_size, |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
596 void PseudoTcpAdapter::SetNoDelay(bool no_delay) { | 596 void PseudoTcpAdapter::SetNoDelay(bool no_delay) { |
597 DCHECK(CalledOnValidThread()); | 597 DCHECK(CalledOnValidThread()); |
598 core_->SetNoDelay(no_delay); | 598 core_->SetNoDelay(no_delay); |
599 } | 599 } |
600 | 600 |
601 void PseudoTcpAdapter::SetWriteWaitsForSend(bool write_waits_for_send) { | 601 void PseudoTcpAdapter::SetWriteWaitsForSend(bool write_waits_for_send) { |
602 DCHECK(CalledOnValidThread()); | 602 DCHECK(CalledOnValidThread()); |
603 core_->SetWriteWaitsForSend(write_waits_for_send); | 603 core_->SetWriteWaitsForSend(write_waits_for_send); |
604 } | 604 } |
605 | 605 |
606 } // namespace jingle_glue | 606 } // namespace protocol |
| 607 } // namespace remoting |
OLD | NEW |