| OLD | NEW |
| 1 // Copyright 2015 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 "remoting/protocol/pseudotcp_adapter.h" | 5 #include "remoting/protocol/pseudotcp_adapter.h" |
| 6 | 6 |
| 7 #include <stddef.h> |
| 8 |
| 7 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 8 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/macros.h" |
| 9 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 10 #include "base/timer/timer.h" | 13 #include "base/timer/timer.h" |
| 11 #include "net/base/address_list.h" | 14 #include "net/base/address_list.h" |
| 12 #include "net/base/completion_callback.h" | 15 #include "net/base/completion_callback.h" |
| 13 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
| 14 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 15 #include "net/base/net_util.h" | 18 #include "net/base/net_util.h" |
| 16 #include "remoting/protocol/p2p_datagram_socket.h" | 19 #include "remoting/protocol/p2p_datagram_socket.h" |
| 17 | 20 |
| 18 using cricket::PseudoTcp; | 21 using cricket::PseudoTcp; |
| 19 | 22 |
| 20 namespace { | 23 namespace { |
| 21 const int kReadBufferSize = 65536; // Maximum size of a packet. | 24 const int kReadBufferSize = 65536; // Maximum size of a packet. |
| 22 const uint16 kDefaultMtu = 1280; | 25 const uint16_t kDefaultMtu = 1280; |
| 23 } // namespace | 26 } // namespace |
| 24 | 27 |
| 25 namespace remoting { | 28 namespace remoting { |
| 26 namespace protocol { | 29 namespace protocol { |
| 27 | 30 |
| 28 class PseudoTcpAdapter::Core : public cricket::IPseudoTcpNotify, | 31 class PseudoTcpAdapter::Core : public cricket::IPseudoTcpNotify, |
| 29 public base::RefCounted<Core> { | 32 public base::RefCounted<Core> { |
| 30 public: | 33 public: |
| 31 explicit Core(scoped_ptr<P2PDatagramSocket> socket); | 34 explicit Core(scoped_ptr<P2PDatagramSocket> socket); |
| 32 | 35 |
| 33 // Functions used to implement net::StreamSocket. | 36 // Functions used to implement net::StreamSocket. |
| 34 int Read(const scoped_refptr<net::IOBuffer>& buffer, int buffer_size, | 37 int Read(const scoped_refptr<net::IOBuffer>& buffer, int buffer_size, |
| 35 const net::CompletionCallback& callback); | 38 const net::CompletionCallback& callback); |
| 36 int Write(const scoped_refptr<net::IOBuffer>& buffer, int buffer_size, | 39 int Write(const scoped_refptr<net::IOBuffer>& buffer, int buffer_size, |
| 37 const net::CompletionCallback& callback); | 40 const net::CompletionCallback& callback); |
| 38 int Connect(const net::CompletionCallback& callback); | 41 int Connect(const net::CompletionCallback& callback); |
| 39 | 42 |
| 40 // cricket::IPseudoTcpNotify interface. | 43 // cricket::IPseudoTcpNotify interface. |
| 41 // These notifications are triggered from NotifyPacket. | 44 // These notifications are triggered from NotifyPacket. |
| 42 void OnTcpOpen(cricket::PseudoTcp* tcp) override; | 45 void OnTcpOpen(cricket::PseudoTcp* tcp) override; |
| 43 void OnTcpReadable(cricket::PseudoTcp* tcp) override; | 46 void OnTcpReadable(cricket::PseudoTcp* tcp) override; |
| 44 void OnTcpWriteable(cricket::PseudoTcp* tcp) override; | 47 void OnTcpWriteable(cricket::PseudoTcp* tcp) override; |
| 45 // This is triggered by NotifyClock or NotifyPacket. | 48 // This is triggered by NotifyClock or NotifyPacket. |
| 46 void OnTcpClosed(cricket::PseudoTcp* tcp, uint32 error) override; | 49 void OnTcpClosed(cricket::PseudoTcp* tcp, uint32_t error) override; |
| 47 // This is triggered by NotifyClock, NotifyPacket, Recv and Send. | 50 // This is triggered by NotifyClock, NotifyPacket, Recv and Send. |
| 48 WriteResult TcpWritePacket(cricket::PseudoTcp* tcp, | 51 WriteResult TcpWritePacket(cricket::PseudoTcp* tcp, |
| 49 const char* buffer, | 52 const char* buffer, |
| 50 size_t len) override; | 53 size_t len) override; |
| 51 | 54 |
| 52 void SetAckDelay(int delay_ms); | 55 void SetAckDelay(int delay_ms); |
| 53 void SetNoDelay(bool no_delay); | 56 void SetNoDelay(bool no_delay); |
| 54 void SetReceiveBufferSize(int32 size); | 57 void SetReceiveBufferSize(int32_t size); |
| 55 void SetSendBufferSize(int32 size); | 58 void SetSendBufferSize(int32_t size); |
| 56 void SetWriteWaitsForSend(bool write_waits_for_send); | 59 void SetWriteWaitsForSend(bool write_waits_for_send); |
| 57 | 60 |
| 58 void DeleteSocket(); | 61 void DeleteSocket(); |
| 59 | 62 |
| 60 private: | 63 private: |
| 61 friend class base::RefCounted<Core>; | 64 friend class base::RefCounted<Core>; |
| 62 ~Core() override; | 65 ~Core() override; |
| 63 | 66 |
| 64 // These are invoked by the underlying Socket, and may trigger callbacks. | 67 // These are invoked by the underlying Socket, and may trigger callbacks. |
| 65 // They hold a reference to |this| while running, to protect from deletion. | 68 // They hold a reference to |this| while running, to protect from deletion. |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 last_write_result_ = result; | 273 last_write_result_ = result; |
| 271 return; | 274 return; |
| 272 } | 275 } |
| 273 | 276 |
| 274 net::CompletionCallback callback = write_callback_; | 277 net::CompletionCallback callback = write_callback_; |
| 275 write_callback_.Reset(); | 278 write_callback_.Reset(); |
| 276 write_buffer_ = NULL; | 279 write_buffer_ = NULL; |
| 277 callback.Run(result); | 280 callback.Run(result); |
| 278 } | 281 } |
| 279 | 282 |
| 280 void PseudoTcpAdapter::Core::OnTcpClosed(PseudoTcp* tcp, uint32 error) { | 283 void PseudoTcpAdapter::Core::OnTcpClosed(PseudoTcp* tcp, uint32_t error) { |
| 281 DCHECK_EQ(tcp, &pseudo_tcp_); | 284 DCHECK_EQ(tcp, &pseudo_tcp_); |
| 282 | 285 |
| 283 if (!connect_callback_.is_null()) { | 286 if (!connect_callback_.is_null()) { |
| 284 net::CompletionCallback callback = connect_callback_; | 287 net::CompletionCallback callback = connect_callback_; |
| 285 connect_callback_.Reset(); | 288 connect_callback_.Reset(); |
| 286 callback.Run(net::MapSystemError(error)); | 289 callback.Run(net::MapSystemError(error)); |
| 287 } | 290 } |
| 288 | 291 |
| 289 if (!read_callback_.is_null()) { | 292 if (!read_callback_.is_null()) { |
| 290 net::CompletionCallback callback = read_callback_; | 293 net::CompletionCallback callback = read_callback_; |
| 291 read_callback_.Reset(); | 294 read_callback_.Reset(); |
| 292 callback.Run(net::MapSystemError(error)); | 295 callback.Run(net::MapSystemError(error)); |
| 293 } | 296 } |
| 294 | 297 |
| 295 if (!write_callback_.is_null()) { | 298 if (!write_callback_.is_null()) { |
| 296 net::CompletionCallback callback = write_callback_; | 299 net::CompletionCallback callback = write_callback_; |
| 297 write_callback_.Reset(); | 300 write_callback_.Reset(); |
| 298 callback.Run(net::MapSystemError(error)); | 301 callback.Run(net::MapSystemError(error)); |
| 299 } | 302 } |
| 300 } | 303 } |
| 301 | 304 |
| 302 void PseudoTcpAdapter::Core::SetAckDelay(int delay_ms) { | 305 void PseudoTcpAdapter::Core::SetAckDelay(int delay_ms) { |
| 303 pseudo_tcp_.SetOption(cricket::PseudoTcp::OPT_ACKDELAY, delay_ms); | 306 pseudo_tcp_.SetOption(cricket::PseudoTcp::OPT_ACKDELAY, delay_ms); |
| 304 } | 307 } |
| 305 | 308 |
| 306 void PseudoTcpAdapter::Core::SetNoDelay(bool no_delay) { | 309 void PseudoTcpAdapter::Core::SetNoDelay(bool no_delay) { |
| 307 pseudo_tcp_.SetOption(cricket::PseudoTcp::OPT_NODELAY, no_delay ? 1 : 0); | 310 pseudo_tcp_.SetOption(cricket::PseudoTcp::OPT_NODELAY, no_delay ? 1 : 0); |
| 308 } | 311 } |
| 309 | 312 |
| 310 void PseudoTcpAdapter::Core::SetReceiveBufferSize(int32 size) { | 313 void PseudoTcpAdapter::Core::SetReceiveBufferSize(int32_t size) { |
| 311 pseudo_tcp_.SetOption(cricket::PseudoTcp::OPT_RCVBUF, size); | 314 pseudo_tcp_.SetOption(cricket::PseudoTcp::OPT_RCVBUF, size); |
| 312 } | 315 } |
| 313 | 316 |
| 314 void PseudoTcpAdapter::Core::SetSendBufferSize(int32 size) { | 317 void PseudoTcpAdapter::Core::SetSendBufferSize(int32_t size) { |
| 315 pseudo_tcp_.SetOption(cricket::PseudoTcp::OPT_SNDBUF, size); | 318 pseudo_tcp_.SetOption(cricket::PseudoTcp::OPT_SNDBUF, size); |
| 316 } | 319 } |
| 317 | 320 |
| 318 void PseudoTcpAdapter::Core::SetWriteWaitsForSend(bool write_waits_for_send) { | 321 void PseudoTcpAdapter::Core::SetWriteWaitsForSend(bool write_waits_for_send) { |
| 319 write_waits_for_send_ = write_waits_for_send; | 322 write_waits_for_send_ = write_waits_for_send; |
| 320 } | 323 } |
| 321 | 324 |
| 322 void PseudoTcpAdapter::Core::DeleteSocket() { | 325 void PseudoTcpAdapter::Core::DeleteSocket() { |
| 323 // Don't dispatch outstanding callbacks when the socket is deleted. | 326 // Don't dispatch outstanding callbacks when the socket is deleted. |
| 324 read_callback_.Reset(); | 327 read_callback_.Reset(); |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 return core_->Read(buffer, buffer_size, callback); | 467 return core_->Read(buffer, buffer_size, callback); |
| 465 } | 468 } |
| 466 | 469 |
| 467 int PseudoTcpAdapter::Write(const scoped_refptr<net::IOBuffer>& buffer, | 470 int PseudoTcpAdapter::Write(const scoped_refptr<net::IOBuffer>& buffer, |
| 468 int buffer_size, | 471 int buffer_size, |
| 469 const net::CompletionCallback& callback) { | 472 const net::CompletionCallback& callback) { |
| 470 DCHECK(CalledOnValidThread()); | 473 DCHECK(CalledOnValidThread()); |
| 471 return core_->Write(buffer, buffer_size, callback); | 474 return core_->Write(buffer, buffer_size, callback); |
| 472 } | 475 } |
| 473 | 476 |
| 474 int PseudoTcpAdapter::SetReceiveBufferSize(int32 size) { | 477 int PseudoTcpAdapter::SetReceiveBufferSize(int32_t size) { |
| 475 DCHECK(CalledOnValidThread()); | 478 DCHECK(CalledOnValidThread()); |
| 476 core_->SetReceiveBufferSize(size); | 479 core_->SetReceiveBufferSize(size); |
| 477 return net::OK; | 480 return net::OK; |
| 478 } | 481 } |
| 479 | 482 |
| 480 int PseudoTcpAdapter::SetSendBufferSize(int32 size) { | 483 int PseudoTcpAdapter::SetSendBufferSize(int32_t size) { |
| 481 DCHECK(CalledOnValidThread()); | 484 DCHECK(CalledOnValidThread()); |
| 482 core_->SetSendBufferSize(size); | 485 core_->SetSendBufferSize(size); |
| 483 return net::OK; | 486 return net::OK; |
| 484 } | 487 } |
| 485 | 488 |
| 486 int PseudoTcpAdapter::Connect(const net::CompletionCallback& callback) { | 489 int PseudoTcpAdapter::Connect(const net::CompletionCallback& callback) { |
| 487 DCHECK(CalledOnValidThread()); | 490 DCHECK(CalledOnValidThread()); |
| 488 return core_->Connect(callback); | 491 return core_->Connect(callback); |
| 489 } | 492 } |
| 490 | 493 |
| 491 void PseudoTcpAdapter::SetAckDelay(int delay_ms) { | 494 void PseudoTcpAdapter::SetAckDelay(int delay_ms) { |
| 492 DCHECK(CalledOnValidThread()); | 495 DCHECK(CalledOnValidThread()); |
| 493 core_->SetAckDelay(delay_ms); | 496 core_->SetAckDelay(delay_ms); |
| 494 } | 497 } |
| 495 | 498 |
| 496 void PseudoTcpAdapter::SetNoDelay(bool no_delay) { | 499 void PseudoTcpAdapter::SetNoDelay(bool no_delay) { |
| 497 DCHECK(CalledOnValidThread()); | 500 DCHECK(CalledOnValidThread()); |
| 498 core_->SetNoDelay(no_delay); | 501 core_->SetNoDelay(no_delay); |
| 499 } | 502 } |
| 500 | 503 |
| 501 void PseudoTcpAdapter::SetWriteWaitsForSend(bool write_waits_for_send) { | 504 void PseudoTcpAdapter::SetWriteWaitsForSend(bool write_waits_for_send) { |
| 502 DCHECK(CalledOnValidThread()); | 505 DCHECK(CalledOnValidThread()); |
| 503 core_->SetWriteWaitsForSend(write_waits_for_send); | 506 core_->SetWriteWaitsForSend(write_waits_for_send); |
| 504 } | 507 } |
| 505 | 508 |
| 506 } // namespace protocol | 509 } // namespace protocol |
| 507 } // namespace remoting | 510 } // namespace remoting |
| OLD | NEW |