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