blob: b0d0399e344798bab8a8866b8b5a3dcb30f97c16 [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
14#include "webrtc/base/asyncpacketsocket.h"
15#include "webrtc/base/scoped_ptr.h"
16#include "webrtc/base/socketfactory.h"
17
18namespace rtc {
19
20// Simulates UDP semantics over TCP. Send and Recv packet sizes
21// are preserved, and drops packets silently on Send, rather than
22// buffer them in user space.
23class AsyncTCPSocketBase : public AsyncPacketSocket {
24 public:
25 AsyncTCPSocketBase(AsyncSocket* socket, bool listen, size_t max_packet_size);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000026 ~AsyncTCPSocketBase() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000027
28 // Pure virtual methods to send and recv data.
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000029 int Send(const void *pv, size_t cb,
30 const rtc::PacketOptions& options) override = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000031 virtual void ProcessInput(char* data, size_t* len) = 0;
32 // Signals incoming connection.
33 virtual void HandleIncomingConnection(AsyncSocket* socket) = 0;
34
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000035 SocketAddress GetLocalAddress() const override;
36 SocketAddress GetRemoteAddress() const override;
37 int SendTo(const void* pv,
38 size_t cb,
39 const SocketAddress& addr,
40 const rtc::PacketOptions& options) override;
41 int Close() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000042
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000043 State GetState() const override;
44 int GetOption(Socket::Option opt, int* value) override;
45 int SetOption(Socket::Option opt, int value) override;
46 int GetError() const override;
47 void SetError(int error) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048
49 protected:
50 // Binds and connects |socket| and creates AsyncTCPSocket for
51 // it. Takes ownership of |socket|. Returns NULL if bind() or
52 // connect() fail (|socket| is destroyed in that case).
53 static AsyncSocket* ConnectSocket(AsyncSocket* socket,
54 const SocketAddress& bind_address,
55 const SocketAddress& remote_address);
56 virtual int SendRaw(const void* pv, size_t cb);
57 int FlushOutBuffer();
58 // Add data to |outbuf_|.
59 void AppendToOutBuffer(const void* pv, size_t cb);
60
61 // Helper methods for |outpos_|.
62 bool IsOutBufferEmpty() const { return outpos_ == 0; }
63 void ClearOutBuffer() { outpos_ = 0; }
64
65 private:
66 // Called by the underlying socket
67 void OnConnectEvent(AsyncSocket* socket);
68 void OnReadEvent(AsyncSocket* socket);
69 void OnWriteEvent(AsyncSocket* socket);
70 void OnCloseEvent(AsyncSocket* socket, int error);
71
72 scoped_ptr<AsyncSocket> socket_;
73 bool listen_;
jbauch3c165762016-02-26 09:31:32 -080074 scoped_ptr<char[]> inbuf_;
75 scoped_ptr<char[]> outbuf_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000076 size_t insize_, inpos_, outsize_, outpos_;
77
henrikg3c089d72015-09-16 05:37:44 -070078 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncTCPSocketBase);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000079};
80
81class AsyncTCPSocket : public AsyncTCPSocketBase {
82 public:
83 // Binds and connects |socket| and creates AsyncTCPSocket for
84 // it. Takes ownership of |socket|. Returns NULL if bind() or
85 // connect() fail (|socket| is destroyed in that case).
86 static AsyncTCPSocket* Create(AsyncSocket* socket,
87 const SocketAddress& bind_address,
88 const SocketAddress& remote_address);
89 AsyncTCPSocket(AsyncSocket* socket, bool listen);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000090 ~AsyncTCPSocket() override {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000091
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000092 int Send(const void* pv,
93 size_t cb,
94 const rtc::PacketOptions& options) override;
95 void ProcessInput(char* data, size_t* len) override;
96 void HandleIncomingConnection(AsyncSocket* socket) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000097
98 private:
henrikg3c089d72015-09-16 05:37:44 -070099 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncTCPSocket);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000100};
101
102} // namespace rtc
103
104#endif // WEBRTC_BASE_ASYNCTCPSOCKET_H_