blob: af0410f8f2b1d553b9ed253c6da9c19e744ca35e [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
11#ifndef WEBRTC_BASE_ASYNCTCPSOCKET_H_
12#define WEBRTC_BASE_ASYNCTCPSOCKET_H_
13
jbauch555604a2016-04-26 03:13:22 -070014#include <memory>
15
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016#include "webrtc/base/asyncpacketsocket.h"
jbauch250fc652016-02-28 15:06:40 -080017#include "webrtc/base/buffer.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018#include "webrtc/base/socketfactory.h"
19
20namespace rtc {
21
22// Simulates UDP semantics over TCP. Send and Recv packet sizes
23// are preserved, and drops packets silently on Send, rather than
24// buffer them in user space.
25class AsyncTCPSocketBase : public AsyncPacketSocket {
26 public:
27 AsyncTCPSocketBase(AsyncSocket* socket, bool listen, size_t max_packet_size);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000028 ~AsyncTCPSocketBase() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000029
30 // Pure virtual methods to send and recv data.
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000031 int Send(const void *pv, size_t cb,
32 const rtc::PacketOptions& options) override = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033 virtual void ProcessInput(char* data, size_t* len) = 0;
34 // Signals incoming connection.
35 virtual void HandleIncomingConnection(AsyncSocket* socket) = 0;
36
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000037 SocketAddress GetLocalAddress() const override;
38 SocketAddress GetRemoteAddress() const override;
39 int SendTo(const void* pv,
40 size_t cb,
41 const SocketAddress& addr,
42 const rtc::PacketOptions& options) override;
43 int Close() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000044
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000045 State GetState() const override;
46 int GetOption(Socket::Option opt, int* value) override;
47 int SetOption(Socket::Option opt, int value) override;
48 int GetError() const override;
49 void SetError(int error) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000050
51 protected:
52 // Binds and connects |socket| and creates AsyncTCPSocket for
53 // it. Takes ownership of |socket|. Returns NULL if bind() or
54 // connect() fail (|socket| is destroyed in that case).
55 static AsyncSocket* ConnectSocket(AsyncSocket* socket,
56 const SocketAddress& bind_address,
57 const SocketAddress& remote_address);
58 virtual int SendRaw(const void* pv, size_t cb);
59 int FlushOutBuffer();
60 // Add data to |outbuf_|.
61 void AppendToOutBuffer(const void* pv, size_t cb);
62
63 // Helper methods for |outpos_|.
jbauch250fc652016-02-28 15:06:40 -080064 bool IsOutBufferEmpty() const { return outbuf_.size() == 0; }
65 void ClearOutBuffer() { outbuf_.Clear(); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066
67 private:
68 // Called by the underlying socket
69 void OnConnectEvent(AsyncSocket* socket);
70 void OnReadEvent(AsyncSocket* socket);
71 void OnWriteEvent(AsyncSocket* socket);
72 void OnCloseEvent(AsyncSocket* socket, int error);
73
jbauch555604a2016-04-26 03:13:22 -070074 std::unique_ptr<AsyncSocket> socket_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000075 bool listen_;
jbauch313afba2016-03-03 03:41:05 -080076 Buffer inbuf_;
jbauch250fc652016-02-28 15:06:40 -080077 Buffer outbuf_;
jbauch313afba2016-03-03 03:41:05 -080078 size_t max_insize_;
jbauch250fc652016-02-28 15:06:40 -080079 size_t max_outsize_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000080
henrikg3c089d72015-09-16 05:37:44 -070081 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncTCPSocketBase);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000082};
83
84class AsyncTCPSocket : public AsyncTCPSocketBase {
85 public:
86 // Binds and connects |socket| and creates AsyncTCPSocket for
87 // it. Takes ownership of |socket|. Returns NULL if bind() or
88 // connect() fail (|socket| is destroyed in that case).
89 static AsyncTCPSocket* Create(AsyncSocket* socket,
90 const SocketAddress& bind_address,
91 const SocketAddress& remote_address);
92 AsyncTCPSocket(AsyncSocket* socket, bool listen);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000093 ~AsyncTCPSocket() override {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000094
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000095 int Send(const void* pv,
96 size_t cb,
97 const rtc::PacketOptions& options) override;
98 void ProcessInput(char* data, size_t* len) override;
99 void HandleIncomingConnection(AsyncSocket* socket) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000100
101 private:
henrikg3c089d72015-09-16 05:37:44 -0700102 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncTCPSocket);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000103};
104
105} // namespace rtc
106
107#endif // WEBRTC_BASE_ASYNCTCPSOCKET_H_