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 11 #ifndef RTC_BASE_ASYNC_TCP_SOCKET_H_ 12 #define RTC_BASE_ASYNC_TCP_SOCKET_H_ 13 14 #include <stddef.h> 15 16 #include <memory> 17 18 #include "rtc_base/async_packet_socket.h" 19 #include "rtc_base/buffer.h" 20 #include "rtc_base/socket.h" 21 #include "rtc_base/socket_address.h" 22 23 namespace 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. 28 class AsyncTCPSocketBase : public AsyncPacketSocket { 29 public: 30 AsyncTCPSocketBase(Socket* socket, size_t max_packet_size); 31 ~AsyncTCPSocketBase() override; 32 33 AsyncTCPSocketBase(const AsyncTCPSocketBase&) = delete; 34 AsyncTCPSocketBase& operator=(const AsyncTCPSocketBase&) = delete; 35 36 // Pure virtual methods to send and recv data. 37 int Send(const void* pv, 38 size_t cb, 39 const rtc::PacketOptions& options) override = 0; 40 virtual void ProcessInput(char* data, size_t* len) = 0; 41 42 SocketAddress GetLocalAddress() const override; 43 SocketAddress GetRemoteAddress() const override; 44 int SendTo(const void* pv, 45 size_t cb, 46 const SocketAddress& addr, 47 const rtc::PacketOptions& options) override; 48 int Close() override; 49 50 State GetState() const override; 51 int GetOption(Socket::Option opt, int* value) override; 52 int SetOption(Socket::Option opt, int value) override; 53 int GetError() const override; 54 void SetError(int error) override; 55 56 protected: 57 // Binds and connects `socket` and creates AsyncTCPSocket for 58 // it. Takes ownership of `socket`. Returns null if bind() or 59 // connect() fail (`socket` is destroyed in that case). 60 static Socket* ConnectSocket(Socket* socket, 61 const SocketAddress& bind_address, 62 const SocketAddress& remote_address); 63 int FlushOutBuffer(); 64 // Add data to `outbuf_`. 65 void AppendToOutBuffer(const void* pv, size_t cb); 66 67 // Helper methods for `outpos_`. IsOutBufferEmpty()68 bool IsOutBufferEmpty() const { return outbuf_.size() == 0; } ClearOutBuffer()69 void ClearOutBuffer() { outbuf_.Clear(); } 70 71 private: 72 // Called by the underlying socket 73 void OnConnectEvent(Socket* socket); 74 void OnReadEvent(Socket* socket); 75 void OnWriteEvent(Socket* socket); 76 void OnCloseEvent(Socket* socket, int error); 77 78 std::unique_ptr<Socket> socket_; 79 Buffer inbuf_; 80 Buffer outbuf_; 81 size_t max_insize_; 82 size_t max_outsize_; 83 }; 84 85 class 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(Socket* socket, 91 const SocketAddress& bind_address, 92 const SocketAddress& remote_address); 93 explicit AsyncTCPSocket(Socket* socket); ~AsyncTCPSocket()94 ~AsyncTCPSocket() override {} 95 96 AsyncTCPSocket(const AsyncTCPSocket&) = delete; 97 AsyncTCPSocket& operator=(const AsyncTCPSocket&) = delete; 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 }; 104 105 class AsyncTcpListenSocket : public AsyncListenSocket { 106 public: 107 explicit AsyncTcpListenSocket(std::unique_ptr<Socket> socket); 108 109 State GetState() const override; 110 SocketAddress GetLocalAddress() const override; 111 112 virtual void HandleIncomingConnection(rtc::Socket* socket); 113 114 private: 115 // Called by the underlying socket 116 void OnReadEvent(Socket* socket); 117 118 std::unique_ptr<Socket> socket_; 119 }; 120 121 } // namespace rtc 122 123 #endif // RTC_BASE_ASYNC_TCP_SOCKET_H_ 124