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