• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/async_socket.h"
20 #include "rtc_base/buffer.h"
21 #include "rtc_base/constructor_magic.h"
22 #include "rtc_base/socket.h"
23 #include "rtc_base/socket_address.h"
24 
25 namespace rtc {
26 
27 // Simulates UDP semantics over TCP.  Send and Recv packet sizes
28 // are preserved, and drops packets silently on Send, rather than
29 // buffer them in user space.
30 class AsyncTCPSocketBase : public AsyncPacketSocket {
31  public:
32   AsyncTCPSocketBase(AsyncSocket* socket, bool listen, size_t max_packet_size);
33   ~AsyncTCPSocketBase() override;
34 
35   // Pure virtual methods to send and recv data.
36   int Send(const void* pv,
37            size_t cb,
38            const rtc::PacketOptions& options) override = 0;
39   virtual void ProcessInput(char* data, size_t* len) = 0;
40   // Signals incoming connection.
41   virtual void HandleIncomingConnection(AsyncSocket* socket) = 0;
42 
43   SocketAddress GetLocalAddress() const override;
44   SocketAddress GetRemoteAddress() const override;
45   int SendTo(const void* pv,
46              size_t cb,
47              const SocketAddress& addr,
48              const rtc::PacketOptions& options) override;
49   int Close() override;
50 
51   State GetState() const override;
52   int GetOption(Socket::Option opt, int* value) override;
53   int SetOption(Socket::Option opt, int value) override;
54   int GetError() const override;
55   void SetError(int error) override;
56 
57  protected:
58   // Binds and connects |socket| and creates AsyncTCPSocket for
59   // it. Takes ownership of |socket|. Returns null if bind() or
60   // connect() fail (|socket| is destroyed in that case).
61   static AsyncSocket* ConnectSocket(AsyncSocket* socket,
62                                     const SocketAddress& bind_address,
63                                     const SocketAddress& remote_address);
64   int FlushOutBuffer();
65   // Add data to |outbuf_|.
66   void AppendToOutBuffer(const void* pv, size_t cb);
67 
68   // Helper methods for |outpos_|.
IsOutBufferEmpty()69   bool IsOutBufferEmpty() const { return outbuf_.size() == 0; }
ClearOutBuffer()70   void ClearOutBuffer() { outbuf_.Clear(); }
71 
72  private:
73   // Called by the underlying socket
74   void OnConnectEvent(AsyncSocket* socket);
75   void OnReadEvent(AsyncSocket* socket);
76   void OnWriteEvent(AsyncSocket* socket);
77   void OnCloseEvent(AsyncSocket* socket, int error);
78 
79   std::unique_ptr<AsyncSocket> socket_;
80   bool listen_;
81   Buffer inbuf_;
82   Buffer outbuf_;
83   size_t max_insize_;
84   size_t max_outsize_;
85 
86   RTC_DISALLOW_COPY_AND_ASSIGN(AsyncTCPSocketBase);
87 };
88 
89 class AsyncTCPSocket : public AsyncTCPSocketBase {
90  public:
91   // Binds and connects |socket| and creates AsyncTCPSocket for
92   // it. Takes ownership of |socket|. Returns null if bind() or
93   // connect() fail (|socket| is destroyed in that case).
94   static AsyncTCPSocket* Create(AsyncSocket* socket,
95                                 const SocketAddress& bind_address,
96                                 const SocketAddress& remote_address);
97   AsyncTCPSocket(AsyncSocket* socket, bool listen);
~AsyncTCPSocket()98   ~AsyncTCPSocket() override {}
99 
100   int Send(const void* pv,
101            size_t cb,
102            const rtc::PacketOptions& options) override;
103   void ProcessInput(char* data, size_t* len) override;
104   void HandleIncomingConnection(AsyncSocket* socket) override;
105 
106  private:
107   RTC_DISALLOW_COPY_AND_ASSIGN(AsyncTCPSocket);
108 };
109 
110 }  // namespace rtc
111 
112 #endif  // RTC_BASE_ASYNC_TCP_SOCKET_H_
113