blob: ca61b54d7855f002dc206242a64143b55acdbdd8 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_ASYNC_TCP_SOCKET_H_
12#define RTC_BASE_ASYNC_TCP_SOCKET_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016#include <memory>
jbauch555604a2016-04-26 03:13:22 -070017
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/async_packet_socket.h"
Yves Gerey988cc082018-10-23 12:03:01 +020019#include "rtc_base/buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/constructor_magic.h"
Yves Gerey988cc082018-10-23 12:03:01 +020021#include "rtc_base/socket.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/socket_address.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020023
24namespace rtc {
25
26// Simulates UDP semantics over TCP. Send and Recv packet sizes
27// are preserved, and drops packets silently on Send, rather than
28// buffer them in user space.
29class AsyncTCPSocketBase : public AsyncPacketSocket {
30 public:
Niels Möllerd30ece12021-10-19 10:11:02 +020031 AsyncTCPSocketBase(Socket* socket, size_t max_packet_size);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020032 ~AsyncTCPSocketBase() override;
33
34 // Pure virtual methods to send and recv data.
Yves Gerey665174f2018-06-19 15:03:05 +020035 int Send(const void* pv,
36 size_t cb,
37 const rtc::PacketOptions& options) override = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020038 virtual void ProcessInput(char* data, size_t* len) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020039
40 SocketAddress GetLocalAddress() const override;
41 SocketAddress GetRemoteAddress() const override;
42 int SendTo(const void* pv,
43 size_t cb,
44 const SocketAddress& addr,
45 const rtc::PacketOptions& options) override;
46 int Close() override;
47
48 State GetState() const override;
49 int GetOption(Socket::Option opt, int* value) override;
50 int SetOption(Socket::Option opt, int value) override;
51 int GetError() const override;
52 void SetError(int error) override;
53
54 protected:
Artem Titov96e3b992021-07-26 16:03:14 +020055 // Binds and connects `socket` and creates AsyncTCPSocket for
56 // it. Takes ownership of `socket`. Returns null if bind() or
57 // connect() fail (`socket` is destroyed in that case).
Niels Möllerd0b88792021-08-12 10:32:30 +020058 static Socket* ConnectSocket(Socket* socket,
59 const SocketAddress& bind_address,
60 const SocketAddress& remote_address);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020061 int FlushOutBuffer();
Artem Titov96e3b992021-07-26 16:03:14 +020062 // Add data to `outbuf_`.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020063 void AppendToOutBuffer(const void* pv, size_t cb);
64
Artem Titov96e3b992021-07-26 16:03:14 +020065 // Helper methods for `outpos_`.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020066 bool IsOutBufferEmpty() const { return outbuf_.size() == 0; }
67 void ClearOutBuffer() { outbuf_.Clear(); }
68
69 private:
70 // Called by the underlying socket
Niels Möllerd0b88792021-08-12 10:32:30 +020071 void OnConnectEvent(Socket* socket);
72 void OnReadEvent(Socket* socket);
73 void OnWriteEvent(Socket* socket);
74 void OnCloseEvent(Socket* socket, int error);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020075
Niels Möllerd0b88792021-08-12 10:32:30 +020076 std::unique_ptr<Socket> socket_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020077 Buffer inbuf_;
78 Buffer outbuf_;
79 size_t max_insize_;
80 size_t max_outsize_;
81
82 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncTCPSocketBase);
83};
84
85class AsyncTCPSocket : public AsyncTCPSocketBase {
86 public:
Artem Titov96e3b992021-07-26 16:03:14 +020087 // Binds and connects `socket` and creates AsyncTCPSocket for
88 // it. Takes ownership of `socket`. Returns null if bind() or
89 // connect() fail (`socket` is destroyed in that case).
Niels Möllerd0b88792021-08-12 10:32:30 +020090 static AsyncTCPSocket* Create(Socket* socket,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020091 const SocketAddress& bind_address,
92 const SocketAddress& remote_address);
Niels Möllerd30ece12021-10-19 10:11:02 +020093 explicit AsyncTCPSocket(Socket* socket);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020094 ~AsyncTCPSocket() override {}
95
96 int Send(const void* pv,
97 size_t cb,
98 const rtc::PacketOptions& options) override;
99 void ProcessInput(char* data, size_t* len) override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200100
101 private:
102 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncTCPSocket);
103};
104
Niels Möllerd30ece12021-10-19 10:11:02 +0200105class AsyncTcpListenSocket : public AsyncListenSocket {
106 public:
107 explicit AsyncTcpListenSocket(std::unique_ptr<Socket> socket);
108
109 State GetState() const override;
110 SocketAddress GetLocalAddress() const override;
111
Niels Möllerd30ece12021-10-19 10:11:02 +0200112 virtual void HandleIncomingConnection(rtc::Socket* socket);
113
114 private:
115 // Called by the underlying socket
116 void OnReadEvent(Socket* socket);
117
118 std::unique_ptr<Socket> socket_;
119};
120
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200121} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000122
Steve Anton10542f22019-01-11 09:11:00 -0800123#endif // RTC_BASE_ASYNC_TCP_SOCKET_H_