blob: ae12a94a932465d92e72459e20b5454337d5e1b4 [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>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020015#include <memory>
jbauch555604a2016-04-26 03:13:22 -070016
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/async_packet_socket.h"
18#include "rtc_base/async_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:
31 AsyncTCPSocketBase(AsyncSocket* socket, bool listen, size_t max_packet_size);
32 ~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;
39 // Signals incoming connection.
40 virtual void HandleIncomingConnection(AsyncSocket* socket) = 0;
41
42 SocketAddress GetLocalAddress() const override;
43 SocketAddress GetRemoteAddress() const override;
44 int SendTo(const void* pv,
45 size_t cb,
46 const SocketAddress& addr,
47 const rtc::PacketOptions& options) override;
48 int Close() override;
49
50 State GetState() const override;
51 int GetOption(Socket::Option opt, int* value) override;
52 int SetOption(Socket::Option opt, int value) override;
53 int GetError() const override;
54 void SetError(int error) override;
55
56 protected:
57 // Binds and connects |socket| and creates AsyncTCPSocket for
58 // it. Takes ownership of |socket|. Returns null if bind() or
59 // connect() fail (|socket| is destroyed in that case).
60 static AsyncSocket* ConnectSocket(AsyncSocket* socket,
61 const SocketAddress& bind_address,
62 const SocketAddress& remote_address);
63 virtual int SendRaw(const void* pv, size_t cb);
64 int FlushOutBuffer();
65 // Add data to |outbuf_|.
66 void AppendToOutBuffer(const void* pv, size_t cb);
67
68 // Helper methods for |outpos_|.
69 bool IsOutBufferEmpty() const { return outbuf_.size() == 0; }
70 void ClearOutBuffer() { outbuf_.Clear(); }
71
72 private:
73 // Called by the underlying socket
74 void OnConnectEvent(AsyncSocket* socket);
75 void OnReadEvent(AsyncSocket* socket);
76 void OnWriteEvent(AsyncSocket* socket);
77 void OnCloseEvent(AsyncSocket* socket, int error);
78
79 std::unique_ptr<AsyncSocket> socket_;
80 bool listen_;
81 Buffer inbuf_;
82 Buffer outbuf_;
83 size_t max_insize_;
84 size_t max_outsize_;
85
86 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncTCPSocketBase);
87};
88
89class AsyncTCPSocket : public AsyncTCPSocketBase {
90 public:
91 // Binds and connects |socket| and creates AsyncTCPSocket for
92 // it. Takes ownership of |socket|. Returns null if bind() or
93 // connect() fail (|socket| is destroyed in that case).
94 static AsyncTCPSocket* Create(AsyncSocket* socket,
95 const SocketAddress& bind_address,
96 const SocketAddress& remote_address);
97 AsyncTCPSocket(AsyncSocket* socket, bool listen);
98 ~AsyncTCPSocket() override {}
99
100 int Send(const void* pv,
101 size_t cb,
102 const rtc::PacketOptions& options) override;
103 void ProcessInput(char* data, size_t* len) override;
104 void HandleIncomingConnection(AsyncSocket* socket) override;
105
106 private:
107 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncTCPSocket);
108};
109
110} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000111
Steve Anton10542f22019-01-11 09:11:00 -0800112#endif // RTC_BASE_ASYNC_TCP_SOCKET_H_