sergeyu | 9cb142f | 2014-09-12 20:43:01 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "remoting/protocol/pseudotcp_channel_factory.h" |
| 6 | |
sergeyu | 89d088b | 2015-12-24 00:22:44 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
sergeyu | 9cb142f | 2014-09-12 20:43:01 | [diff] [blame] | 9 | #include "base/bind.h" |
sergeyu | 9cb142f | 2014-09-12 20:43:01 | [diff] [blame] | 10 | #include "net/base/net_errors.h" |
sergeyu | 9cb142f | 2014-09-12 20:43:01 | [diff] [blame] | 11 | #include "remoting/base/constants.h" |
| 12 | #include "remoting/protocol/datagram_channel_factory.h" |
sergeyu | aa22c08 | 2015-07-20 19:41:13 | [diff] [blame] | 13 | #include "remoting/protocol/p2p_datagram_socket.h" |
sergeyu | 031ce3b3 | 2015-07-06 19:00:46 | [diff] [blame] | 14 | #include "remoting/protocol/pseudotcp_adapter.h" |
sergeyu | 9cb142f | 2014-09-12 20:43:01 | [diff] [blame] | 15 | |
| 16 | namespace remoting { |
| 17 | namespace protocol { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | // Value is chosen to balance the extra latency against the reduced |
| 22 | // load due to ACK traffic. |
| 23 | const int kTcpAckDelayMilliseconds = 10; |
| 24 | |
| 25 | // Values for the TCP send and receive buffer size. This should be tuned to |
| 26 | // accommodate high latency network but not backlog the decoding pipeline. |
| 27 | const int kTcpReceiveBufferSize = 256 * 1024; |
| 28 | const int kTcpSendBufferSize = kTcpReceiveBufferSize + 30 * 1024; |
| 29 | |
| 30 | } // namespace |
| 31 | |
| 32 | PseudoTcpChannelFactory::PseudoTcpChannelFactory( |
| 33 | DatagramChannelFactory* datagram_channel_factory) |
| 34 | : datagram_channel_factory_(datagram_channel_factory) { |
| 35 | } |
| 36 | |
| 37 | PseudoTcpChannelFactory::~PseudoTcpChannelFactory() { |
| 38 | // CancelChannelCreation() is expected to be called before destruction. |
| 39 | DCHECK(pending_sockets_.empty()); |
| 40 | } |
| 41 | |
| 42 | void PseudoTcpChannelFactory::CreateChannel( |
| 43 | const std::string& name, |
| 44 | const ChannelCreatedCallback& callback) { |
| 45 | datagram_channel_factory_->CreateChannel( |
| 46 | name, |
| 47 | base::Bind(&PseudoTcpChannelFactory::OnDatagramChannelCreated, |
| 48 | base::Unretained(this), name, callback)); |
| 49 | } |
| 50 | |
| 51 | void PseudoTcpChannelFactory::CancelChannelCreation(const std::string& name) { |
| 52 | PendingSocketsMap::iterator it = pending_sockets_.find(name); |
| 53 | if (it == pending_sockets_.end()) { |
| 54 | datagram_channel_factory_->CancelChannelCreation(name); |
| 55 | } else { |
| 56 | delete it->second; |
| 57 | pending_sockets_.erase(it); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void PseudoTcpChannelFactory::OnDatagramChannelCreated( |
| 62 | const std::string& name, |
| 63 | const ChannelCreatedCallback& callback, |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame^] | 64 | std::unique_ptr<P2PDatagramSocket> datagram_socket) { |
sergeyu | aa6fa234 | 2015-12-22 23:26:48 | [diff] [blame] | 65 | PseudoTcpAdapter* adapter = new PseudoTcpAdapter(std::move(datagram_socket)); |
sergeyu | 9cb142f | 2014-09-12 20:43:01 | [diff] [blame] | 66 | pending_sockets_[name] = adapter; |
| 67 | |
| 68 | adapter->SetSendBufferSize(kTcpSendBufferSize); |
| 69 | adapter->SetReceiveBufferSize(kTcpReceiveBufferSize); |
| 70 | adapter->SetNoDelay(true); |
| 71 | adapter->SetAckDelay(kTcpAckDelayMilliseconds); |
| 72 | |
| 73 | // TODO(sergeyu): This is a hack to improve latency of the video channel. |
| 74 | // Consider removing it once we have better flow control implemented. |
| 75 | if (name == kVideoChannelName) |
| 76 | adapter->SetWriteWaitsForSend(true); |
| 77 | |
| 78 | int result = adapter->Connect( |
| 79 | base::Bind(&PseudoTcpChannelFactory::OnPseudoTcpConnected, |
| 80 | base::Unretained(this), name, callback)); |
| 81 | if (result != net::ERR_IO_PENDING) |
| 82 | OnPseudoTcpConnected(name, callback, result); |
| 83 | } |
| 84 | |
| 85 | void PseudoTcpChannelFactory::OnPseudoTcpConnected( |
| 86 | const std::string& name, |
| 87 | const ChannelCreatedCallback& callback, |
| 88 | int result) { |
| 89 | PendingSocketsMap::iterator it = pending_sockets_.find(name); |
| 90 | DCHECK(it != pending_sockets_.end()); |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame^] | 91 | std::unique_ptr<P2PStreamSocket> socket(it->second); |
sergeyu | 9cb142f | 2014-09-12 20:43:01 | [diff] [blame] | 92 | pending_sockets_.erase(it); |
| 93 | |
| 94 | if (result != net::OK) |
| 95 | socket.reset(); |
| 96 | |
sergeyu | aa6fa234 | 2015-12-22 23:26:48 | [diff] [blame] | 97 | callback.Run(std::move(socket)); |
sergeyu | 9cb142f | 2014-09-12 20:43:01 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | } // namespace protocol |
| 101 | } // namespace remoting |