xref: /aosp_15_r20/external/webrtc/rtc_base/async_udp_socket.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef RTC_BASE_ASYNC_UDP_SOCKET_H_
12*d9f75844SAndroid Build Coastguard Worker #define RTC_BASE_ASYNC_UDP_SOCKET_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <stddef.h>
15*d9f75844SAndroid Build Coastguard Worker 
16*d9f75844SAndroid Build Coastguard Worker #include <cstdint>
17*d9f75844SAndroid Build Coastguard Worker #include <memory>
18*d9f75844SAndroid Build Coastguard Worker 
19*d9f75844SAndroid Build Coastguard Worker #include "absl/types/optional.h"
20*d9f75844SAndroid Build Coastguard Worker #include "api/sequence_checker.h"
21*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/async_packet_socket.h"
22*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/socket.h"
23*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/socket_address.h"
24*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/socket_factory.h"
25*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/thread_annotations.h"
26*d9f75844SAndroid Build Coastguard Worker 
27*d9f75844SAndroid Build Coastguard Worker namespace rtc {
28*d9f75844SAndroid Build Coastguard Worker 
29*d9f75844SAndroid Build Coastguard Worker // Provides the ability to receive packets asynchronously.  Sends are not
30*d9f75844SAndroid Build Coastguard Worker // buffered since it is acceptable to drop packets under high load.
31*d9f75844SAndroid Build Coastguard Worker class AsyncUDPSocket : public AsyncPacketSocket {
32*d9f75844SAndroid Build Coastguard Worker  public:
33*d9f75844SAndroid Build Coastguard Worker   // Binds `socket` and creates AsyncUDPSocket for it. Takes ownership
34*d9f75844SAndroid Build Coastguard Worker   // of `socket`. Returns null if bind() fails (`socket` is destroyed
35*d9f75844SAndroid Build Coastguard Worker   // in that case).
36*d9f75844SAndroid Build Coastguard Worker   static AsyncUDPSocket* Create(Socket* socket,
37*d9f75844SAndroid Build Coastguard Worker                                 const SocketAddress& bind_address);
38*d9f75844SAndroid Build Coastguard Worker   // Creates a new socket for sending asynchronous UDP packets using an
39*d9f75844SAndroid Build Coastguard Worker   // asynchronous socket from the given factory.
40*d9f75844SAndroid Build Coastguard Worker   static AsyncUDPSocket* Create(SocketFactory* factory,
41*d9f75844SAndroid Build Coastguard Worker                                 const SocketAddress& bind_address);
42*d9f75844SAndroid Build Coastguard Worker   explicit AsyncUDPSocket(Socket* socket);
43*d9f75844SAndroid Build Coastguard Worker   ~AsyncUDPSocket() = default;
44*d9f75844SAndroid Build Coastguard Worker 
45*d9f75844SAndroid Build Coastguard Worker   SocketAddress GetLocalAddress() const override;
46*d9f75844SAndroid Build Coastguard Worker   SocketAddress GetRemoteAddress() const override;
47*d9f75844SAndroid Build Coastguard Worker   int Send(const void* pv,
48*d9f75844SAndroid Build Coastguard Worker            size_t cb,
49*d9f75844SAndroid Build Coastguard Worker            const rtc::PacketOptions& options) override;
50*d9f75844SAndroid Build Coastguard Worker   int SendTo(const void* pv,
51*d9f75844SAndroid Build Coastguard Worker              size_t cb,
52*d9f75844SAndroid Build Coastguard Worker              const SocketAddress& addr,
53*d9f75844SAndroid Build Coastguard Worker              const rtc::PacketOptions& options) override;
54*d9f75844SAndroid Build Coastguard Worker   int Close() override;
55*d9f75844SAndroid Build Coastguard Worker 
56*d9f75844SAndroid Build Coastguard Worker   State GetState() const override;
57*d9f75844SAndroid Build Coastguard Worker   int GetOption(Socket::Option opt, int* value) override;
58*d9f75844SAndroid Build Coastguard Worker   int SetOption(Socket::Option opt, int value) override;
59*d9f75844SAndroid Build Coastguard Worker   int GetError() const override;
60*d9f75844SAndroid Build Coastguard Worker   void SetError(int error) override;
61*d9f75844SAndroid Build Coastguard Worker 
62*d9f75844SAndroid Build Coastguard Worker  private:
63*d9f75844SAndroid Build Coastguard Worker   // Called when the underlying socket is ready to be read from.
64*d9f75844SAndroid Build Coastguard Worker   void OnReadEvent(Socket* socket);
65*d9f75844SAndroid Build Coastguard Worker   // Called when the underlying socket is ready to send.
66*d9f75844SAndroid Build Coastguard Worker   void OnWriteEvent(Socket* socket);
67*d9f75844SAndroid Build Coastguard Worker 
68*d9f75844SAndroid Build Coastguard Worker   RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker sequence_checker_;
69*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<Socket> socket_;
70*d9f75844SAndroid Build Coastguard Worker   static constexpr int BUF_SIZE = 64 * 1024;
71*d9f75844SAndroid Build Coastguard Worker   char buf_[BUF_SIZE] RTC_GUARDED_BY(sequence_checker_);
72*d9f75844SAndroid Build Coastguard Worker   absl::optional<int64_t> socket_time_offset_ RTC_GUARDED_BY(sequence_checker_);
73*d9f75844SAndroid Build Coastguard Worker };
74*d9f75844SAndroid Build Coastguard Worker 
75*d9f75844SAndroid Build Coastguard Worker }  // namespace rtc
76*d9f75844SAndroid Build Coastguard Worker 
77*d9f75844SAndroid Build Coastguard Worker #endif  // RTC_BASE_ASYNC_UDP_SOCKET_H_
78