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