xref: /aosp_15_r20/external/webrtc/rtc_base/async_socket.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 <memory>
18 
19 #include "rtc_base/socket.h"
20 #include "rtc_base/socket_address.h"
21 #include "rtc_base/third_party/sigslot/sigslot.h"
22 
23 namespace rtc {
24 
25 class AsyncSocketAdapter : public Socket, public sigslot::has_slots<> {
26  public:
27   // Takes ownership of the passed in socket.
28   // TODO(bugs.webrtc.org/6424): Change to unique_ptr here and in callers.
29   explicit AsyncSocketAdapter(Socket* socket);
30 
31   SocketAddress GetLocalAddress() const override;
32   SocketAddress GetRemoteAddress() const override;
33   int Bind(const SocketAddress& addr) override;
34   int Connect(const SocketAddress& addr) override;
35   int Send(const void* pv, size_t cb) override;
36   int SendTo(const void* pv, size_t cb, const SocketAddress& addr) override;
37   int Recv(void* pv, size_t cb, int64_t* timestamp) override;
38   int RecvFrom(void* pv,
39                size_t cb,
40                SocketAddress* paddr,
41                int64_t* timestamp) override;
42   int Listen(int backlog) override;
43   Socket* Accept(SocketAddress* paddr) override;
44   int Close() override;
45   int GetError() const override;
46   void SetError(int error) override;
47   ConnState GetState() const override;
48   int GetOption(Option opt, int* value) override;
49   int SetOption(Option opt, int value) override;
50 
51  protected:
52   virtual void OnConnectEvent(Socket* socket);
53   virtual void OnReadEvent(Socket* socket);
54   virtual void OnWriteEvent(Socket* socket);
55   virtual void OnCloseEvent(Socket* socket, int err);
56 
GetSocket()57   Socket* GetSocket() const { return socket_.get(); }
58 
59  private:
60   const std::unique_ptr<Socket> socket_;
61 };
62 
63 }  // namespace rtc
64 
65 #endif  // RTC_BASE_ASYNC_SOCKET_H_
66