• 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 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 
18 namespace 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.
23 class AsyncTCPSocketBase : public AsyncPacketSocket {
24  public:
25   AsyncTCPSocketBase(AsyncSocket* socket, bool listen, size_t max_packet_size);
26   virtual ~AsyncTCPSocketBase();
27 
28   // Pure virtual methods to send and recv data.
29   virtual int Send(const void *pv, size_t cb,
30                    const rtc::PacketOptions& options) = 0;
31   virtual void ProcessInput(char* data, size_t* len) = 0;
32   // Signals incoming connection.
33   virtual void HandleIncomingConnection(AsyncSocket* socket) = 0;
34 
35   virtual SocketAddress GetLocalAddress() const;
36   virtual SocketAddress GetRemoteAddress() const;
37   virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr,
38                      const rtc::PacketOptions& options);
39   virtual int Close();
40 
41   virtual State GetState() const;
42   virtual int GetOption(Socket::Option opt, int* value);
43   virtual int SetOption(Socket::Option opt, int value);
44   virtual int GetError() const;
45   virtual void SetError(int error);
46 
47  protected:
48   // Binds and connects |socket| and creates AsyncTCPSocket for
49   // it. Takes ownership of |socket|. Returns NULL if bind() or
50   // connect() fail (|socket| is destroyed in that case).
51   static AsyncSocket* ConnectSocket(AsyncSocket* socket,
52                                     const SocketAddress& bind_address,
53                                     const SocketAddress& remote_address);
54   virtual int SendRaw(const void* pv, size_t cb);
55   int FlushOutBuffer();
56   // Add data to |outbuf_|.
57   void AppendToOutBuffer(const void* pv, size_t cb);
58 
59   // Helper methods for |outpos_|.
IsOutBufferEmpty()60   bool IsOutBufferEmpty() const { return outpos_ == 0; }
ClearOutBuffer()61   void ClearOutBuffer() { outpos_ = 0; }
62 
63  private:
64   // Called by the underlying socket
65   void OnConnectEvent(AsyncSocket* socket);
66   void OnReadEvent(AsyncSocket* socket);
67   void OnWriteEvent(AsyncSocket* socket);
68   void OnCloseEvent(AsyncSocket* socket, int error);
69 
70   scoped_ptr<AsyncSocket> socket_;
71   bool listen_;
72   char* inbuf_, * outbuf_;
73   size_t insize_, inpos_, outsize_, outpos_;
74 
75   DISALLOW_EVIL_CONSTRUCTORS(AsyncTCPSocketBase);
76 };
77 
78 class AsyncTCPSocket : public AsyncTCPSocketBase {
79  public:
80   // Binds and connects |socket| and creates AsyncTCPSocket for
81   // it. Takes ownership of |socket|. Returns NULL if bind() or
82   // connect() fail (|socket| is destroyed in that case).
83   static AsyncTCPSocket* Create(AsyncSocket* socket,
84                                 const SocketAddress& bind_address,
85                                 const SocketAddress& remote_address);
86   AsyncTCPSocket(AsyncSocket* socket, bool listen);
~AsyncTCPSocket()87   virtual ~AsyncTCPSocket() {}
88 
89   virtual int Send(const void* pv, size_t cb,
90                    const rtc::PacketOptions& options);
91   virtual void ProcessInput(char* data, size_t* len);
92   virtual void HandleIncomingConnection(AsyncSocket* socket);
93 
94  private:
95   DISALLOW_EVIL_CONSTRUCTORS(AsyncTCPSocket);
96 };
97 
98 }  // namespace rtc
99 
100 #endif  // WEBRTC_BASE_ASYNCTCPSOCKET_H_
101