henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "rtc_base/async_tcp_socket.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <stdint.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 14 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 16 | #include <algorithm> |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 17 | #include <memory> |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 18 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "rtc_base/byte_order.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/checks.h" |
| 21 | #include "rtc_base/logging.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 22 | #include "rtc_base/network/sent_packet.h" |
| 23 | #include "rtc_base/third_party/sigslot/sigslot.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 24 | #include "rtc_base/time_utils.h" // for TimeMillis |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 25 | |
| 26 | #if defined(WEBRTC_POSIX) |
| 27 | #include <errno.h> |
| 28 | #endif // WEBRTC_POSIX |
| 29 | |
| 30 | namespace rtc { |
| 31 | |
| 32 | static const size_t kMaxPacketSize = 64 * 1024; |
| 33 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 34 | typedef uint16_t PacketLength; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 35 | static const size_t kPacketLenSize = sizeof(PacketLength); |
| 36 | |
| 37 | static const size_t kBufSize = kMaxPacketSize + kPacketLenSize; |
| 38 | |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 39 | // The input buffer will be resized so that at least kMinimumRecvSize bytes can |
| 40 | // be received (but it will not grow above the maximum size passed to the |
| 41 | // constructor). |
| 42 | static const size_t kMinimumRecvSize = 128; |
| 43 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 44 | static const int kListenBacklog = 5; |
| 45 | |
| 46 | // Binds and connects |socket| |
| 47 | AsyncSocket* AsyncTCPSocketBase::ConnectSocket( |
| 48 | rtc::AsyncSocket* socket, |
| 49 | const rtc::SocketAddress& bind_address, |
| 50 | const rtc::SocketAddress& remote_address) { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 51 | std::unique_ptr<rtc::AsyncSocket> owned_socket(socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 52 | if (socket->Bind(bind_address) < 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 53 | RTC_LOG(LS_ERROR) << "Bind() failed with error " << socket->GetError(); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 54 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 55 | } |
| 56 | if (socket->Connect(remote_address) < 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 57 | RTC_LOG(LS_ERROR) << "Connect() failed with error " << socket->GetError(); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 58 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 59 | } |
| 60 | return owned_socket.release(); |
| 61 | } |
| 62 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 63 | AsyncTCPSocketBase::AsyncTCPSocketBase(AsyncSocket* socket, |
| 64 | bool listen, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 65 | size_t max_packet_size) |
| 66 | : socket_(socket), |
| 67 | listen_(listen), |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 68 | max_insize_(max_packet_size), |
jbauch | 250fc65 | 2016-02-28 15:06:40 -0800 | [diff] [blame] | 69 | max_outsize_(max_packet_size) { |
jbauch | 3c16576 | 2016-02-26 09:31:32 -0800 | [diff] [blame] | 70 | if (!listen_) { |
| 71 | // Listening sockets don't send/receive data, so they don't need buffers. |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 72 | inbuf_.EnsureCapacity(kMinimumRecvSize); |
jbauch | 3c16576 | 2016-02-26 09:31:32 -0800 | [diff] [blame] | 73 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 74 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 75 | RTC_DCHECK(socket_.get() != nullptr); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 76 | socket_->SignalConnectEvent.connect(this, |
| 77 | &AsyncTCPSocketBase::OnConnectEvent); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 78 | socket_->SignalReadEvent.connect(this, &AsyncTCPSocketBase::OnReadEvent); |
| 79 | socket_->SignalWriteEvent.connect(this, &AsyncTCPSocketBase::OnWriteEvent); |
| 80 | socket_->SignalCloseEvent.connect(this, &AsyncTCPSocketBase::OnCloseEvent); |
| 81 | |
| 82 | if (listen_) { |
| 83 | if (socket_->Listen(kListenBacklog) < 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 84 | RTC_LOG(LS_ERROR) << "Listen() failed with error " << socket_->GetError(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
jbauch | 3c16576 | 2016-02-26 09:31:32 -0800 | [diff] [blame] | 89 | AsyncTCPSocketBase::~AsyncTCPSocketBase() {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 90 | |
| 91 | SocketAddress AsyncTCPSocketBase::GetLocalAddress() const { |
| 92 | return socket_->GetLocalAddress(); |
| 93 | } |
| 94 | |
| 95 | SocketAddress AsyncTCPSocketBase::GetRemoteAddress() const { |
| 96 | return socket_->GetRemoteAddress(); |
| 97 | } |
| 98 | |
| 99 | int AsyncTCPSocketBase::Close() { |
| 100 | return socket_->Close(); |
| 101 | } |
| 102 | |
| 103 | AsyncTCPSocket::State AsyncTCPSocketBase::GetState() const { |
| 104 | switch (socket_->GetState()) { |
| 105 | case Socket::CS_CLOSED: |
| 106 | return STATE_CLOSED; |
| 107 | case Socket::CS_CONNECTING: |
| 108 | if (listen_) { |
| 109 | return STATE_BOUND; |
| 110 | } else { |
| 111 | return STATE_CONNECTING; |
| 112 | } |
| 113 | case Socket::CS_CONNECTED: |
| 114 | return STATE_CONNECTED; |
| 115 | default: |
jbauch | 3c16576 | 2016-02-26 09:31:32 -0800 | [diff] [blame] | 116 | RTC_NOTREACHED(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 117 | return STATE_CLOSED; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | int AsyncTCPSocketBase::GetOption(Socket::Option opt, int* value) { |
| 122 | return socket_->GetOption(opt, value); |
| 123 | } |
| 124 | |
| 125 | int AsyncTCPSocketBase::SetOption(Socket::Option opt, int value) { |
| 126 | return socket_->SetOption(opt, value); |
| 127 | } |
| 128 | |
| 129 | int AsyncTCPSocketBase::GetError() const { |
| 130 | return socket_->GetError(); |
| 131 | } |
| 132 | |
| 133 | void AsyncTCPSocketBase::SetError(int error) { |
| 134 | return socket_->SetError(error); |
| 135 | } |
| 136 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 137 | int AsyncTCPSocketBase::SendTo(const void* pv, |
| 138 | size_t cb, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 139 | const SocketAddress& addr, |
| 140 | const rtc::PacketOptions& options) { |
honghaiz | b19eba3 | 2015-08-03 10:23:31 -0700 | [diff] [blame] | 141 | const SocketAddress& remote_address = GetRemoteAddress(); |
| 142 | if (addr == remote_address) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 143 | return Send(pv, cb, options); |
honghaiz | b19eba3 | 2015-08-03 10:23:31 -0700 | [diff] [blame] | 144 | // Remote address may be empty if there is a sudden network change. |
jbauch | 3c16576 | 2016-02-26 09:31:32 -0800 | [diff] [blame] | 145 | RTC_DCHECK(remote_address.IsNil()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 146 | socket_->SetError(ENOTCONN); |
| 147 | return -1; |
| 148 | } |
| 149 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 150 | int AsyncTCPSocketBase::SendRaw(const void* pv, size_t cb) { |
jbauch | 250fc65 | 2016-02-28 15:06:40 -0800 | [diff] [blame] | 151 | if (outbuf_.size() + cb > max_outsize_) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 152 | socket_->SetError(EMSGSIZE); |
| 153 | return -1; |
| 154 | } |
| 155 | |
jbauch | 250fc65 | 2016-02-28 15:06:40 -0800 | [diff] [blame] | 156 | RTC_DCHECK(!listen_); |
| 157 | outbuf_.AppendData(static_cast<const uint8_t*>(pv), cb); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 158 | |
| 159 | return FlushOutBuffer(); |
| 160 | } |
| 161 | |
| 162 | int AsyncTCPSocketBase::FlushOutBuffer() { |
jbauch | 250fc65 | 2016-02-28 15:06:40 -0800 | [diff] [blame] | 163 | RTC_DCHECK(!listen_); |
| 164 | int res = socket_->Send(outbuf_.data(), outbuf_.size()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 165 | if (res <= 0) { |
| 166 | return res; |
| 167 | } |
jbauch | 250fc65 | 2016-02-28 15:06:40 -0800 | [diff] [blame] | 168 | if (static_cast<size_t>(res) > outbuf_.size()) { |
jbauch | 3c16576 | 2016-02-26 09:31:32 -0800 | [diff] [blame] | 169 | RTC_NOTREACHED(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 170 | return -1; |
| 171 | } |
jbauch | 250fc65 | 2016-02-28 15:06:40 -0800 | [diff] [blame] | 172 | size_t new_size = outbuf_.size() - res; |
| 173 | if (new_size > 0) { |
| 174 | memmove(outbuf_.data(), outbuf_.data() + res, new_size); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 175 | } |
jbauch | 250fc65 | 2016-02-28 15:06:40 -0800 | [diff] [blame] | 176 | outbuf_.SetSize(new_size); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 177 | return res; |
| 178 | } |
| 179 | |
| 180 | void AsyncTCPSocketBase::AppendToOutBuffer(const void* pv, size_t cb) { |
jbauch | 250fc65 | 2016-02-28 15:06:40 -0800 | [diff] [blame] | 181 | RTC_DCHECK(outbuf_.size() + cb <= max_outsize_); |
| 182 | RTC_DCHECK(!listen_); |
| 183 | outbuf_.AppendData(static_cast<const uint8_t*>(pv), cb); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void AsyncTCPSocketBase::OnConnectEvent(AsyncSocket* socket) { |
| 187 | SignalConnect(this); |
| 188 | } |
| 189 | |
| 190 | void AsyncTCPSocketBase::OnReadEvent(AsyncSocket* socket) { |
jbauch | 3c16576 | 2016-02-26 09:31:32 -0800 | [diff] [blame] | 191 | RTC_DCHECK(socket_.get() == socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 192 | |
| 193 | if (listen_) { |
| 194 | rtc::SocketAddress address; |
| 195 | rtc::AsyncSocket* new_socket = socket->Accept(&address); |
| 196 | if (!new_socket) { |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 197 | // TODO(stefan): Do something better like forwarding the error |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 198 | // to the user. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 199 | RTC_LOG(LS_ERROR) << "TCP accept failed with error " |
| 200 | << socket_->GetError(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 201 | return; |
| 202 | } |
| 203 | |
| 204 | HandleIncomingConnection(new_socket); |
| 205 | |
| 206 | // Prime a read event in case data is waiting. |
| 207 | new_socket->SignalReadEvent(new_socket); |
| 208 | } else { |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 209 | size_t total_recv = 0; |
| 210 | while (true) { |
| 211 | size_t free_size = inbuf_.capacity() - inbuf_.size(); |
| 212 | if (free_size < kMinimumRecvSize && inbuf_.capacity() < max_insize_) { |
| 213 | inbuf_.EnsureCapacity(std::min(max_insize_, inbuf_.capacity() * 2)); |
| 214 | free_size = inbuf_.capacity() - inbuf_.size(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 215 | } |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 216 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 217 | int len = |
| 218 | socket_->Recv(inbuf_.data() + inbuf_.size(), free_size, nullptr); |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 219 | if (len < 0) { |
| 220 | // TODO(stefan): Do something better like forwarding the error to the |
| 221 | // user. |
| 222 | if (!socket_->IsBlocking()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 223 | RTC_LOG(LS_ERROR) << "Recv() returned error: " << socket_->GetError(); |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 224 | } |
| 225 | break; |
| 226 | } |
| 227 | |
| 228 | total_recv += len; |
| 229 | inbuf_.SetSize(inbuf_.size() + len); |
| 230 | if (!len || static_cast<size_t>(len) < free_size) { |
| 231 | break; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | if (!total_recv) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 236 | return; |
| 237 | } |
| 238 | |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 239 | size_t size = inbuf_.size(); |
| 240 | ProcessInput(inbuf_.data<char>(), &size); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 241 | |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 242 | if (size > inbuf_.size()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 243 | RTC_LOG(LS_ERROR) << "input buffer overflow"; |
jbauch | 3c16576 | 2016-02-26 09:31:32 -0800 | [diff] [blame] | 244 | RTC_NOTREACHED(); |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 245 | inbuf_.Clear(); |
| 246 | } else { |
| 247 | inbuf_.SetSize(size); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | void AsyncTCPSocketBase::OnWriteEvent(AsyncSocket* socket) { |
jbauch | 3c16576 | 2016-02-26 09:31:32 -0800 | [diff] [blame] | 253 | RTC_DCHECK(socket_.get() == socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 254 | |
jbauch | 250fc65 | 2016-02-28 15:06:40 -0800 | [diff] [blame] | 255 | if (outbuf_.size() > 0) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 256 | FlushOutBuffer(); |
| 257 | } |
| 258 | |
jbauch | 250fc65 | 2016-02-28 15:06:40 -0800 | [diff] [blame] | 259 | if (outbuf_.size() == 0) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 260 | SignalReadyToSend(this); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | void AsyncTCPSocketBase::OnCloseEvent(AsyncSocket* socket, int error) { |
| 265 | SignalClose(this, error); |
| 266 | } |
| 267 | |
| 268 | // AsyncTCPSocket |
| 269 | // Binds and connects |socket| and creates AsyncTCPSocket for |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 270 | // it. Takes ownership of |socket|. Returns null if bind() or |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 271 | // connect() fail (|socket| is destroyed in that case). |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 272 | AsyncTCPSocket* AsyncTCPSocket::Create(AsyncSocket* socket, |
| 273 | const SocketAddress& bind_address, |
| 274 | const SocketAddress& remote_address) { |
| 275 | return new AsyncTCPSocket( |
| 276 | AsyncTCPSocketBase::ConnectSocket(socket, bind_address, remote_address), |
| 277 | false); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | AsyncTCPSocket::AsyncTCPSocket(AsyncSocket* socket, bool listen) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 281 | : AsyncTCPSocketBase(socket, listen, kBufSize) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 282 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 283 | int AsyncTCPSocket::Send(const void* pv, |
| 284 | size_t cb, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 285 | const rtc::PacketOptions& options) { |
| 286 | if (cb > kBufSize) { |
| 287 | SetError(EMSGSIZE); |
| 288 | return -1; |
| 289 | } |
| 290 | |
| 291 | // If we are blocking on send, then silently drop this packet |
| 292 | if (!IsOutBufferEmpty()) |
| 293 | return static_cast<int>(cb); |
| 294 | |
| 295 | PacketLength pkt_len = HostToNetwork16(static_cast<PacketLength>(cb)); |
| 296 | AppendToOutBuffer(&pkt_len, kPacketLenSize); |
| 297 | AppendToOutBuffer(pv, cb); |
| 298 | |
| 299 | int res = FlushOutBuffer(); |
| 300 | if (res <= 0) { |
| 301 | // drop packet if we made no progress |
| 302 | ClearOutBuffer(); |
| 303 | return res; |
| 304 | } |
| 305 | |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 306 | rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis(), |
| 307 | options.info_signaled_after_sent); |
Qingsi Wang | 4ea53b3 | 2018-04-16 18:22:31 -0700 | [diff] [blame] | 308 | CopySocketInformationToPacketInfo(cb, *this, false, &sent_packet.info); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 309 | SignalSentPacket(this, sent_packet); |
| 310 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 311 | // We claim to have sent the whole thing, even if we only sent partial |
| 312 | return static_cast<int>(cb); |
| 313 | } |
| 314 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 315 | void AsyncTCPSocket::ProcessInput(char* data, size_t* len) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 316 | SocketAddress remote_addr(GetRemoteAddress()); |
| 317 | |
| 318 | while (true) { |
| 319 | if (*len < kPacketLenSize) |
| 320 | return; |
| 321 | |
| 322 | PacketLength pkt_len = rtc::GetBE16(data); |
| 323 | if (*len < kPacketLenSize + pkt_len) |
| 324 | return; |
| 325 | |
| 326 | SignalReadPacket(this, data + kPacketLenSize, pkt_len, remote_addr, |
Niels Möller | 15ca5a9 | 2018-11-01 14:32:47 +0100 | [diff] [blame] | 327 | TimeMicros()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 328 | |
| 329 | *len -= kPacketLenSize + pkt_len; |
| 330 | if (*len > 0) { |
| 331 | memmove(data, data + kPacketLenSize + pkt_len, *len); |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | void AsyncTCPSocket::HandleIncomingConnection(AsyncSocket* socket) { |
| 337 | SignalNewConnection(this, new AsyncTCPSocket(socket, false)); |
| 338 | } |
| 339 | |
| 340 | } // namespace rtc |