• 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_ASYNCSOCKET_H_
12 #define WEBRTC_BASE_ASYNCSOCKET_H_
13 
14 #include "webrtc/base/common.h"
15 #include "webrtc/base/sigslot.h"
16 #include "webrtc/base/socket.h"
17 
18 namespace rtc {
19 
20 // TODO: Remove Socket and rename AsyncSocket to Socket.
21 
22 // Provides the ability to perform socket I/O asynchronously.
23 class AsyncSocket : public Socket {
24  public:
25   AsyncSocket();
26   ~AsyncSocket() override;
27 
28   AsyncSocket* Accept(SocketAddress* paddr) override = 0;
29 
30   // SignalReadEvent and SignalWriteEvent use multi_threaded_local to allow
31   // access concurrently from different thread.
32   // For example SignalReadEvent::connect will be called in AsyncUDPSocket ctor
33   // but at the same time the SocketDispatcher maybe signaling the read event.
34   // ready to read
35   sigslot::signal1<AsyncSocket*,
36                    sigslot::multi_threaded_local> SignalReadEvent;
37   // ready to write
38   sigslot::signal1<AsyncSocket*,
39                    sigslot::multi_threaded_local> SignalWriteEvent;
40   sigslot::signal1<AsyncSocket*> SignalConnectEvent;     // connected
41   sigslot::signal2<AsyncSocket*, int> SignalCloseEvent;  // closed
42 };
43 
44 class AsyncSocketAdapter : public AsyncSocket, public sigslot::has_slots<> {
45  public:
46   // The adapted socket may explicitly be NULL, and later assigned using Attach.
47   // However, subclasses which support detached mode must override any methods
48   // that will be called during the detached period (usually GetState()), to
49   // avoid dereferencing a null pointer.
50   explicit AsyncSocketAdapter(AsyncSocket* socket);
51   ~AsyncSocketAdapter() override;
52   void Attach(AsyncSocket* socket);
53   SocketAddress GetLocalAddress() const override;
54   SocketAddress GetRemoteAddress() const override;
55   int Bind(const SocketAddress& addr) override;
56   int Connect(const SocketAddress& addr) override;
57   int Send(const void* pv, size_t cb) override;
58   int SendTo(const void* pv, size_t cb, const SocketAddress& addr) override;
59   int Recv(void* pv, size_t cb) override;
60   int RecvFrom(void* pv, size_t cb, SocketAddress* paddr) override;
61   int Listen(int backlog) override;
62   AsyncSocket* Accept(SocketAddress* paddr) override;
63   int Close() override;
64   int GetError() const override;
65   void SetError(int error) override;
66   ConnState GetState() const override;
67   int EstimateMTU(uint16_t* mtu) override;
68   int GetOption(Option opt, int* value) override;
69   int SetOption(Option opt, int value) override;
70 
71  protected:
72   virtual void OnConnectEvent(AsyncSocket* socket);
73   virtual void OnReadEvent(AsyncSocket* socket);
74   virtual void OnWriteEvent(AsyncSocket* socket);
75   virtual void OnCloseEvent(AsyncSocket* socket, int err);
76 
77   AsyncSocket* socket_;
78 };
79 
80 }  // namespace rtc
81 
82 #endif  // WEBRTC_BASE_ASYNCSOCKET_H_
83