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