blob: c145d6f08fd1f656af197e145e69b424d4193f68 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_ASYNCTCPSOCKET_H_
12#define RTC_BASE_ASYNCTCPSOCKET_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <memory>
jbauch555604a2016-04-26 03:13:22 -070015
Yves Gerey2e00abc2018-10-05 15:39:24 +020016#include "rtc_base/asyncpacketsocket.h" // for PacketOptions, AsyncPacketSo...
17#include "rtc_base/asyncsocket.h" // for AsyncSocket
18#include "rtc_base/buffer.h" // for Buffer
19#include "rtc_base/constructormagic.h" // for RTC_DISALLOW_COPY_AND_ASSIGN
20#include "rtc_base/socket.h" // for Socket, Socket::Option
21#include "rtc_base/socketaddress.h" // for SocketAddress
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020022
23namespace rtc {
24
25// Simulates UDP semantics over TCP. Send and Recv packet sizes
26// are preserved, and drops packets silently on Send, rather than
27// buffer them in user space.
28class AsyncTCPSocketBase : public AsyncPacketSocket {
29 public:
30 AsyncTCPSocketBase(AsyncSocket* socket, bool listen, size_t max_packet_size);
31 ~AsyncTCPSocketBase() override;
32
33 // Pure virtual methods to send and recv data.
Yves Gerey665174f2018-06-19 15:03:05 +020034 int Send(const void* pv,
35 size_t cb,
36 const rtc::PacketOptions& options) override = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020037 virtual void ProcessInput(char* data, size_t* len) = 0;
38 // Signals incoming connection.
39 virtual void HandleIncomingConnection(AsyncSocket* socket) = 0;
40
41 SocketAddress GetLocalAddress() const override;
42 SocketAddress GetRemoteAddress() const override;
43 int SendTo(const void* pv,
44 size_t cb,
45 const SocketAddress& addr,
46 const rtc::PacketOptions& options) override;
47 int Close() override;
48
49 State GetState() const override;
50 int GetOption(Socket::Option opt, int* value) override;
51 int SetOption(Socket::Option opt, int value) override;
52 int GetError() const override;
53 void SetError(int error) override;
54
55 protected:
56 // Binds and connects |socket| and creates AsyncTCPSocket for
57 // it. Takes ownership of |socket|. Returns null if bind() or
58 // connect() fail (|socket| is destroyed in that case).
59 static AsyncSocket* ConnectSocket(AsyncSocket* socket,
60 const SocketAddress& bind_address,
61 const SocketAddress& remote_address);
62 virtual int SendRaw(const void* pv, size_t cb);
63 int FlushOutBuffer();
64 // Add data to |outbuf_|.
65 void AppendToOutBuffer(const void* pv, size_t cb);
66
67 // Helper methods for |outpos_|.
68 bool IsOutBufferEmpty() const { return outbuf_.size() == 0; }
69 void ClearOutBuffer() { outbuf_.Clear(); }
70
71 private:
72 // Called by the underlying socket
73 void OnConnectEvent(AsyncSocket* socket);
74 void OnReadEvent(AsyncSocket* socket);
75 void OnWriteEvent(AsyncSocket* socket);
76 void OnCloseEvent(AsyncSocket* socket, int error);
77
78 std::unique_ptr<AsyncSocket> socket_;
79 bool listen_;
80 Buffer inbuf_;
81 Buffer outbuf_;
82 size_t max_insize_;
83 size_t max_outsize_;
84
85 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncTCPSocketBase);
86};
87
88class AsyncTCPSocket : public AsyncTCPSocketBase {
89 public:
90 // Binds and connects |socket| and creates AsyncTCPSocket for
91 // it. Takes ownership of |socket|. Returns null if bind() or
92 // connect() fail (|socket| is destroyed in that case).
93 static AsyncTCPSocket* Create(AsyncSocket* socket,
94 const SocketAddress& bind_address,
95 const SocketAddress& remote_address);
96 AsyncTCPSocket(AsyncSocket* socket, bool listen);
97 ~AsyncTCPSocket() override {}
98
99 int Send(const void* pv,
100 size_t cb,
101 const rtc::PacketOptions& options) override;
102 void ProcessInput(char* data, size_t* len) override;
103 void HandleIncomingConnection(AsyncSocket* socket) override;
104
105 private:
106 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncTCPSocket);
107};
108
109} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000110
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200111#endif // RTC_BASE_ASYNCTCPSOCKET_H_