sergeyu | 031ce3b3 | 2015-07-06 19:00:46 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
sergeyu | 031ce3b3 | 2015-07-06 19:00:46 | [diff] [blame] | 5 | #include "remoting/protocol/channel_socket_adapter.h" |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 6 | |
| 7 | #include <limits> |
| 8 | |
[email protected] | ad48b7f | 2012-02-21 21:20:02 | [diff] [blame] | 9 | #include "base/callback.h" |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 10 | #include "base/logging.h" |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 11 | #include "net/base/io_buffer.h" |
| 12 | #include "net/base/net_errors.h" |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 13 | |
sergeyu | 031ce3b3 | 2015-07-06 19:00:46 | [diff] [blame] | 14 | namespace remoting { |
| 15 | namespace protocol { |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 16 | |
| 17 | TransportChannelSocketAdapter::TransportChannelSocketAdapter( |
zhihuang | 1aa292f3 | 2017-01-03 19:37:51 | [diff] [blame] | 18 | cricket::IceTransportInternal* ice_transport) |
| 19 | : channel_(ice_transport), closed_error_code_(net::OK) { |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 20 | DCHECK(channel_); |
| 21 | |
| 22 | channel_->SignalReadPacket.connect( |
| 23 | this, &TransportChannelSocketAdapter::OnNewPacket); |
| 24 | channel_->SignalWritableState.connect( |
| 25 | this, &TransportChannelSocketAdapter::OnWritableState); |
| 26 | channel_->SignalDestroyed.connect( |
| 27 | this, &TransportChannelSocketAdapter::OnChannelDestroyed); |
| 28 | } |
| 29 | |
| 30 | TransportChannelSocketAdapter::~TransportChannelSocketAdapter() { |
joedow | 18837eb | 2017-05-17 18:28:24 | [diff] [blame] | 31 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | ad48b7f | 2012-02-21 21:20:02 | [diff] [blame] | 32 | if (!destruction_callback_.is_null()) |
| 33 | destruction_callback_.Run(); |
| 34 | } |
| 35 | |
| 36 | void TransportChannelSocketAdapter::SetOnDestroyedCallback( |
| 37 | const base::Closure& callback) { |
| 38 | destruction_callback_ = callback; |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 39 | } |
| 40 | |
sergeyu | aa22c08 | 2015-07-20 19:41:13 | [diff] [blame] | 41 | int TransportChannelSocketAdapter::Recv( |
Mario Sanchez Prada | 670d282 | 2019-03-18 21:52:41 | [diff] [blame] | 42 | const scoped_refptr<net::IOBuffer>& buf, |
| 43 | int buffer_size, |
| 44 | const net::CompletionRepeatingCallback& callback) { |
sergeyu | 031ce3b3 | 2015-07-06 19:00:46 | [diff] [blame] | 45 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 46 | DCHECK(buf); |
| 47 | DCHECK(!callback.is_null()); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 48 | CHECK(read_callback_.is_null()); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 49 | |
| 50 | if (!channel_) { |
| 51 | DCHECK(closed_error_code_ != net::OK); |
| 52 | return closed_error_code_; |
| 53 | } |
| 54 | |
| 55 | read_callback_ = callback; |
| 56 | read_buffer_ = buf; |
| 57 | read_buffer_size_ = buffer_size; |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 58 | |
| 59 | return net::ERR_IO_PENDING; |
| 60 | } |
| 61 | |
sergeyu | aa22c08 | 2015-07-20 19:41:13 | [diff] [blame] | 62 | int TransportChannelSocketAdapter::Send( |
Mario Sanchez Prada | 670d282 | 2019-03-18 21:52:41 | [diff] [blame] | 63 | const scoped_refptr<net::IOBuffer>& buffer, |
| 64 | int buffer_size, |
| 65 | const net::CompletionRepeatingCallback& callback) { |
sergeyu | 031ce3b3 | 2015-07-06 19:00:46 | [diff] [blame] | 66 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 67 | DCHECK(buffer); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 68 | DCHECK(!callback.is_null()); |
| 69 | CHECK(write_callback_.is_null()); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 70 | |
| 71 | if (!channel_) { |
| 72 | DCHECK(closed_error_code_ != net::OK); |
| 73 | return closed_error_code_; |
| 74 | } |
| 75 | |
[email protected] | 4d83fbc | 2011-04-22 19:35:49 | [diff] [blame] | 76 | int result; |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 77 | rtc::PacketOptions options; |
[email protected] | 4d83fbc | 2011-04-22 19:35:49 | [diff] [blame] | 78 | if (channel_->writable()) { |
[email protected] | e6b239a | 2014-02-15 06:09:52 | [diff] [blame] | 79 | result = channel_->SendPacket(buffer->data(), buffer_size, options); |
[email protected] | 87185ec | 2011-07-27 13:25:40 | [diff] [blame] | 80 | if (result < 0) { |
[email protected] | 4d83fbc | 2011-04-22 19:35:49 | [diff] [blame] | 81 | result = net::MapSystemError(channel_->GetError()); |
[email protected] | 87185ec | 2011-07-27 13:25:40 | [diff] [blame] | 82 | |
| 83 | // If the underlying socket returns IO pending where it shouldn't we |
| 84 | // pretend the packet is dropped and return as succeeded because no |
| 85 | // writeable callback will happen. |
| 86 | if (result == net::ERR_IO_PENDING) |
| 87 | result = net::OK; |
| 88 | } |
[email protected] | 4d83fbc | 2011-04-22 19:35:49 | [diff] [blame] | 89 | } else { |
| 90 | // Channel is not writable yet. |
| 91 | result = net::ERR_IO_PENDING; |
[email protected] | 4d83fbc | 2011-04-22 19:35:49 | [diff] [blame] | 92 | write_callback_ = callback; |
| 93 | write_buffer_ = buffer; |
| 94 | write_buffer_size_ = buffer_size; |
| 95 | } |
| 96 | |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 97 | return result; |
| 98 | } |
| 99 | |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 100 | void TransportChannelSocketAdapter::Close(int error_code) { |
sergeyu | 031ce3b3 | 2015-07-06 19:00:46 | [diff] [blame] | 101 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 102 | |
| 103 | if (!channel_) // Already closed. |
| 104 | return; |
| 105 | |
| 106 | DCHECK(error_code != net::OK); |
| 107 | closed_error_code_ = error_code; |
| 108 | channel_->SignalReadPacket.disconnect(this); |
| 109 | channel_->SignalDestroyed.disconnect(this); |
| 110 | channel_ = NULL; |
| 111 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 112 | if (!read_callback_.is_null()) { |
Mario Sanchez Prada | 670d282 | 2019-03-18 21:52:41 | [diff] [blame] | 113 | net::CompletionRepeatingCallback callback = read_callback_; |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 114 | read_callback_.Reset(); |
kylechar | 80b4c9f | 2019-11-14 23:07:09 | [diff] [blame] | 115 | read_buffer_.reset(); |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 116 | callback.Run(error_code); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 117 | } |
| 118 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 119 | if (!write_callback_.is_null()) { |
Mario Sanchez Prada | 670d282 | 2019-03-18 21:52:41 | [diff] [blame] | 120 | net::CompletionRepeatingCallback callback = write_callback_; |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 121 | write_callback_.Reset(); |
kylechar | 80b4c9f | 2019-11-14 23:07:09 | [diff] [blame] | 122 | write_buffer_.reset(); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 123 | callback.Run(error_code); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | void TransportChannelSocketAdapter::OnNewPacket( |
Niels Möller | e5cdbff0 | 2019-12-18 12:29:45 | [diff] [blame^] | 128 | rtc::PacketTransportInternal* transport, |
[email protected] | 8bb74637 | 2012-04-26 04:20:12 | [diff] [blame] | 129 | const char* data, |
| 130 | size_t data_size, |
Niels Möller | 6c41fbf5 | 2018-12-20 10:22:56 | [diff] [blame] | 131 | const int64_t& packet_time, |
[email protected] | 8bb74637 | 2012-04-26 04:20:12 | [diff] [blame] | 132 | int flags) { |
sergeyu | 031ce3b3 | 2015-07-06 19:00:46 | [diff] [blame] | 133 | DCHECK(thread_checker_.CalledOnValidThread()); |
sprang | d72236c | 2016-10-21 08:36:04 | [diff] [blame] | 134 | DCHECK_EQ(transport, channel_); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 135 | if (!read_callback_.is_null()) { |
[email protected] | f9d8a77 | 2013-06-01 04:33:17 | [diff] [blame] | 136 | DCHECK(read_buffer_.get()); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 137 | CHECK_LT(data_size, static_cast<size_t>(std::numeric_limits<int>::max())); |
| 138 | |
| 139 | if (read_buffer_size_ < static_cast<int>(data_size)) { |
| 140 | LOG(WARNING) << "Data buffer is smaller than the received packet. " |
| 141 | << "Dropping the data that doesn't fit."; |
| 142 | data_size = read_buffer_size_; |
| 143 | } |
| 144 | |
| 145 | memcpy(read_buffer_->data(), data, data_size); |
| 146 | |
Mario Sanchez Prada | 670d282 | 2019-03-18 21:52:41 | [diff] [blame] | 147 | net::CompletionRepeatingCallback callback = read_callback_; |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 148 | read_callback_.Reset(); |
kylechar | 80b4c9f | 2019-11-14 23:07:09 | [diff] [blame] | 149 | read_buffer_.reset(); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 150 | callback.Run(data_size); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 151 | } else { |
| 152 | LOG(WARNING) |
| 153 | << "Data was received without a callback. Dropping the packet."; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void TransportChannelSocketAdapter::OnWritableState( |
Niels Möller | e5cdbff0 | 2019-12-18 12:29:45 | [diff] [blame^] | 158 | rtc::PacketTransportInternal* transport) { |
sergeyu | 031ce3b3 | 2015-07-06 19:00:46 | [diff] [blame] | 159 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 160 | // Try to send the packet if there is a pending write. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 161 | if (!write_callback_.is_null()) { |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 162 | rtc::PacketOptions options; |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 163 | int result = channel_->SendPacket(write_buffer_->data(), |
[email protected] | 7791cf4 | 2013-09-23 23:11:57 | [diff] [blame] | 164 | write_buffer_size_, |
[email protected] | e6b239a | 2014-02-15 06:09:52 | [diff] [blame] | 165 | options); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 166 | if (result < 0) |
[email protected] | f6da0b25 | 2011-03-31 00:26:03 | [diff] [blame] | 167 | result = net::MapSystemError(channel_->GetError()); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 168 | |
| 169 | if (result != net::ERR_IO_PENDING) { |
Mario Sanchez Prada | 670d282 | 2019-03-18 21:52:41 | [diff] [blame] | 170 | net::CompletionRepeatingCallback callback = write_callback_; |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 171 | write_callback_.Reset(); |
kylechar | 80b4c9f | 2019-11-14 23:07:09 | [diff] [blame] | 172 | write_buffer_.reset(); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 173 | callback.Run(result); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void TransportChannelSocketAdapter::OnChannelDestroyed( |
zhihuang | 1aa292f3 | 2017-01-03 19:37:51 | [diff] [blame] | 179 | cricket::IceTransportInternal* channel) { |
sergeyu | 031ce3b3 | 2015-07-06 19:00:46 | [diff] [blame] | 180 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 181 | DCHECK_EQ(channel, channel_); |
| 182 | Close(net::ERR_CONNECTION_ABORTED); |
| 183 | } |
| 184 | |
sergeyu | 031ce3b3 | 2015-07-06 19:00:46 | [diff] [blame] | 185 | } // namespace protocol |
| 186 | } // namespace remoting |