[email protected] | ad48b7f | 2012-02-21 21:20:02 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
[email protected] | f6da0b25 | 2011-03-31 00:26:03 | [diff] [blame] | 5 | #include "jingle/glue/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" |
| 11 | #include "base/message_loop.h" |
| 12 | #include "net/base/io_buffer.h" |
| 13 | #include "net/base/net_errors.h" |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 14 | #include "third_party/libjingle/source/talk/p2p/base/transportchannel.h" |
| 15 | |
[email protected] | f6da0b25 | 2011-03-31 00:26:03 | [diff] [blame] | 16 | namespace jingle_glue { |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 17 | |
| 18 | TransportChannelSocketAdapter::TransportChannelSocketAdapter( |
| 19 | cricket::TransportChannel* channel) |
[email protected] | b231184d | 2013-04-27 07:51:55 | [diff] [blame] | 20 | : message_loop_(base::MessageLoop::current()), |
[email protected] | 6c87575 | 2011-04-01 01:59:07 | [diff] [blame] | 21 | channel_(channel), |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 22 | closed_error_code_(net::OK) { |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 23 | DCHECK(channel_); |
| 24 | |
| 25 | channel_->SignalReadPacket.connect( |
| 26 | this, &TransportChannelSocketAdapter::OnNewPacket); |
| 27 | channel_->SignalWritableState.connect( |
| 28 | this, &TransportChannelSocketAdapter::OnWritableState); |
| 29 | channel_->SignalDestroyed.connect( |
| 30 | this, &TransportChannelSocketAdapter::OnChannelDestroyed); |
| 31 | } |
| 32 | |
| 33 | TransportChannelSocketAdapter::~TransportChannelSocketAdapter() { |
[email protected] | ad48b7f | 2012-02-21 21:20:02 | [diff] [blame] | 34 | if (!destruction_callback_.is_null()) |
| 35 | destruction_callback_.Run(); |
| 36 | } |
| 37 | |
| 38 | void TransportChannelSocketAdapter::SetOnDestroyedCallback( |
| 39 | const base::Closure& callback) { |
| 40 | destruction_callback_ = callback; |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | int TransportChannelSocketAdapter::Read( |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 44 | net::IOBuffer* buf, |
| 45 | int buffer_size, |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 46 | const net::CompletionCallback& callback) { |
[email protected] | b231184d | 2013-04-27 07:51:55 | [diff] [blame] | 47 | DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 48 | DCHECK(buf); |
| 49 | DCHECK(!callback.is_null()); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 50 | CHECK(read_callback_.is_null()); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 51 | |
| 52 | if (!channel_) { |
| 53 | DCHECK(closed_error_code_ != net::OK); |
| 54 | return closed_error_code_; |
| 55 | } |
| 56 | |
| 57 | read_callback_ = callback; |
| 58 | read_buffer_ = buf; |
| 59 | read_buffer_size_ = buffer_size; |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 60 | |
| 61 | return net::ERR_IO_PENDING; |
| 62 | } |
| 63 | |
| 64 | int TransportChannelSocketAdapter::Write( |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 65 | net::IOBuffer* buffer, |
| 66 | int buffer_size, |
| 67 | const net::CompletionCallback& callback) { |
[email protected] | b231184d | 2013-04-27 07:51:55 | [diff] [blame] | 68 | DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 69 | DCHECK(buffer); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 70 | DCHECK(!callback.is_null()); |
| 71 | CHECK(write_callback_.is_null()); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 72 | |
| 73 | if (!channel_) { |
| 74 | DCHECK(closed_error_code_ != net::OK); |
| 75 | return closed_error_code_; |
| 76 | } |
| 77 | |
[email protected] | 4d83fbc | 2011-04-22 19:35:49 | [diff] [blame] | 78 | int result; |
| 79 | if (channel_->writable()) { |
| 80 | result = channel_->SendPacket(buffer->data(), buffer_size); |
[email protected] | 87185ec | 2011-07-27 13:25:40 | [diff] [blame] | 81 | if (result < 0) { |
[email protected] | 4d83fbc | 2011-04-22 19:35:49 | [diff] [blame] | 82 | result = net::MapSystemError(channel_->GetError()); |
[email protected] | 87185ec | 2011-07-27 13:25:40 | [diff] [blame] | 83 | |
| 84 | // If the underlying socket returns IO pending where it shouldn't we |
| 85 | // pretend the packet is dropped and return as succeeded because no |
| 86 | // writeable callback will happen. |
| 87 | if (result == net::ERR_IO_PENDING) |
| 88 | result = net::OK; |
| 89 | } |
[email protected] | 4d83fbc | 2011-04-22 19:35:49 | [diff] [blame] | 90 | } else { |
| 91 | // Channel is not writable yet. |
| 92 | result = net::ERR_IO_PENDING; |
[email protected] | 4d83fbc | 2011-04-22 19:35:49 | [diff] [blame] | 93 | write_callback_ = callback; |
| 94 | write_buffer_ = buffer; |
| 95 | write_buffer_size_ = buffer_size; |
| 96 | } |
| 97 | |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 98 | return result; |
| 99 | } |
| 100 | |
| 101 | bool TransportChannelSocketAdapter::SetReceiveBufferSize(int32 size) { |
[email protected] | b231184d | 2013-04-27 07:51:55 | [diff] [blame] | 102 | DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
[email protected] | b8acc61 | 2011-08-04 20:01:42 | [diff] [blame] | 103 | return channel_->SetOption(talk_base::Socket::OPT_RCVBUF, size) == 0; |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | bool TransportChannelSocketAdapter::SetSendBufferSize(int32 size) { |
[email protected] | b231184d | 2013-04-27 07:51:55 | [diff] [blame] | 107 | DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
[email protected] | b8acc61 | 2011-08-04 20:01:42 | [diff] [blame] | 108 | return channel_->SetOption(talk_base::Socket::OPT_SNDBUF, size) == 0; |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void TransportChannelSocketAdapter::Close(int error_code) { |
[email protected] | b231184d | 2013-04-27 07:51:55 | [diff] [blame] | 112 | DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 113 | |
| 114 | if (!channel_) // Already closed. |
| 115 | return; |
| 116 | |
| 117 | DCHECK(error_code != net::OK); |
| 118 | closed_error_code_ = error_code; |
| 119 | channel_->SignalReadPacket.disconnect(this); |
| 120 | channel_->SignalDestroyed.disconnect(this); |
| 121 | channel_ = NULL; |
| 122 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 123 | if (!read_callback_.is_null()) { |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 124 | net::CompletionCallback callback = read_callback_; |
| 125 | read_callback_.Reset(); |
| 126 | read_buffer_ = NULL; |
| 127 | callback.Run(error_code); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 128 | } |
| 129 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 130 | if (!write_callback_.is_null()) { |
| 131 | net::CompletionCallback callback = write_callback_; |
| 132 | write_callback_.Reset(); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 133 | write_buffer_ = NULL; |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 134 | callback.Run(error_code); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | void TransportChannelSocketAdapter::OnNewPacket( |
[email protected] | 8bb74637 | 2012-04-26 04:20:12 | [diff] [blame] | 139 | cricket::TransportChannel* channel, |
| 140 | const char* data, |
| 141 | size_t data_size, |
| 142 | int flags) { |
[email protected] | b231184d | 2013-04-27 07:51:55 | [diff] [blame] | 143 | DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 144 | DCHECK_EQ(channel, channel_); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 145 | if (!read_callback_.is_null()) { |
[email protected] | f9d8a77 | 2013-06-01 04:33:17 | [diff] [blame^] | 146 | DCHECK(read_buffer_.get()); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 147 | CHECK_LT(data_size, static_cast<size_t>(std::numeric_limits<int>::max())); |
| 148 | |
| 149 | if (read_buffer_size_ < static_cast<int>(data_size)) { |
| 150 | LOG(WARNING) << "Data buffer is smaller than the received packet. " |
| 151 | << "Dropping the data that doesn't fit."; |
| 152 | data_size = read_buffer_size_; |
| 153 | } |
| 154 | |
| 155 | memcpy(read_buffer_->data(), data, data_size); |
| 156 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 157 | net::CompletionCallback callback = read_callback_; |
| 158 | read_callback_.Reset(); |
| 159 | read_buffer_ = NULL; |
| 160 | |
| 161 | callback.Run(data_size); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 162 | } else { |
| 163 | LOG(WARNING) |
| 164 | << "Data was received without a callback. Dropping the packet."; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void TransportChannelSocketAdapter::OnWritableState( |
| 169 | cricket::TransportChannel* channel) { |
[email protected] | b231184d | 2013-04-27 07:51:55 | [diff] [blame] | 170 | DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 171 | // Try to send the packet if there is a pending write. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 172 | if (!write_callback_.is_null()) { |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 173 | int result = channel_->SendPacket(write_buffer_->data(), |
| 174 | write_buffer_size_); |
| 175 | if (result < 0) |
[email protected] | f6da0b25 | 2011-03-31 00:26:03 | [diff] [blame] | 176 | result = net::MapSystemError(channel_->GetError()); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 177 | |
| 178 | if (result != net::ERR_IO_PENDING) { |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 179 | net::CompletionCallback callback = write_callback_; |
| 180 | write_callback_.Reset(); |
[email protected] | f749f9c | 2011-12-09 01:06:19 | [diff] [blame] | 181 | write_buffer_ = NULL; |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 182 | callback.Run(result); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void TransportChannelSocketAdapter::OnChannelDestroyed( |
| 188 | cricket::TransportChannel* channel) { |
[email protected] | b231184d | 2013-04-27 07:51:55 | [diff] [blame] | 189 | DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
[email protected] | 2fc3b42 | 2010-09-28 20:59:56 | [diff] [blame] | 190 | DCHECK_EQ(channel, channel_); |
| 191 | Close(net::ERR_CONNECTION_ABORTED); |
| 192 | } |
| 193 | |
[email protected] | f6da0b25 | 2011-03-31 00:26:03 | [diff] [blame] | 194 | } // namespace jingle_glue |