henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_ASYNCTCPSOCKET_H_ |
| 12 | #define RTC_BASE_ASYNCTCPSOCKET_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 14 | #include <memory> |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/asyncpacketsocket.h" |
| 17 | #include "rtc_base/buffer.h" |
| 18 | #include "rtc_base/constructormagic.h" |
| 19 | #include "rtc_base/socketfactory.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 20 | |
| 21 | namespace 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. |
| 26 | class 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. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 32 | int Send(const void* pv, |
| 33 | size_t cb, |
| 34 | const rtc::PacketOptions& options) override = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 35 | virtual void ProcessInput(char* data, size_t* len) = 0; |
| 36 | // Signals incoming connection. |
| 37 | virtual void HandleIncomingConnection(AsyncSocket* socket) = 0; |
| 38 | |
| 39 | SocketAddress GetLocalAddress() const override; |
| 40 | SocketAddress GetRemoteAddress() const override; |
| 41 | int SendTo(const void* pv, |
| 42 | size_t cb, |
| 43 | const SocketAddress& addr, |
| 44 | const rtc::PacketOptions& options) override; |
| 45 | int Close() override; |
| 46 | |
| 47 | State GetState() const override; |
| 48 | int GetOption(Socket::Option opt, int* value) override; |
| 49 | int SetOption(Socket::Option opt, int value) override; |
| 50 | int GetError() const override; |
| 51 | void SetError(int error) override; |
| 52 | |
| 53 | protected: |
| 54 | // Binds and connects |socket| and creates AsyncTCPSocket for |
| 55 | // it. Takes ownership of |socket|. Returns null if bind() or |
| 56 | // connect() fail (|socket| is destroyed in that case). |
| 57 | static AsyncSocket* ConnectSocket(AsyncSocket* socket, |
| 58 | const SocketAddress& bind_address, |
| 59 | const SocketAddress& remote_address); |
| 60 | virtual int SendRaw(const void* pv, size_t cb); |
| 61 | int FlushOutBuffer(); |
| 62 | // Add data to |outbuf_|. |
| 63 | void AppendToOutBuffer(const void* pv, size_t cb); |
| 64 | |
| 65 | // Helper methods for |outpos_|. |
| 66 | bool IsOutBufferEmpty() const { return outbuf_.size() == 0; } |
| 67 | void ClearOutBuffer() { outbuf_.Clear(); } |
| 68 | |
| 69 | private: |
| 70 | // Called by the underlying socket |
| 71 | void OnConnectEvent(AsyncSocket* socket); |
| 72 | void OnReadEvent(AsyncSocket* socket); |
| 73 | void OnWriteEvent(AsyncSocket* socket); |
| 74 | void OnCloseEvent(AsyncSocket* socket, int error); |
| 75 | |
| 76 | std::unique_ptr<AsyncSocket> socket_; |
| 77 | bool listen_; |
| 78 | Buffer inbuf_; |
| 79 | Buffer outbuf_; |
| 80 | size_t max_insize_; |
| 81 | size_t max_outsize_; |
| 82 | |
| 83 | RTC_DISALLOW_COPY_AND_ASSIGN(AsyncTCPSocketBase); |
| 84 | }; |
| 85 | |
| 86 | class AsyncTCPSocket : public AsyncTCPSocketBase { |
| 87 | public: |
| 88 | // Binds and connects |socket| and creates AsyncTCPSocket for |
| 89 | // it. Takes ownership of |socket|. Returns null if bind() or |
| 90 | // connect() fail (|socket| is destroyed in that case). |
| 91 | static AsyncTCPSocket* Create(AsyncSocket* socket, |
| 92 | const SocketAddress& bind_address, |
| 93 | const SocketAddress& remote_address); |
| 94 | AsyncTCPSocket(AsyncSocket* socket, bool listen); |
| 95 | ~AsyncTCPSocket() override {} |
| 96 | |
| 97 | int Send(const void* pv, |
| 98 | size_t cb, |
| 99 | const rtc::PacketOptions& options) override; |
| 100 | void ProcessInput(char* data, size_t* len) override; |
| 101 | void HandleIncomingConnection(AsyncSocket* socket) override; |
| 102 | |
| 103 | private: |
| 104 | RTC_DISALLOW_COPY_AND_ASSIGN(AsyncTCPSocket); |
| 105 | }; |
| 106 | |
| 107 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 108 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 109 | #endif // RTC_BASE_ASYNCTCPSOCKET_H_ |