xref: /aosp_15_r20/external/webrtc/rtc_base/async_packet_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_PACKET_SOCKET_H_
12 #define RTC_BASE_ASYNC_PACKET_SOCKET_H_
13 
14 #include <vector>
15 
16 #include "api/sequence_checker.h"
17 #include "rtc_base/callback_list.h"
18 #include "rtc_base/dscp.h"
19 #include "rtc_base/network/sent_packet.h"
20 #include "rtc_base/socket.h"
21 #include "rtc_base/system/no_unique_address.h"
22 #include "rtc_base/system/rtc_export.h"
23 #include "rtc_base/third_party/sigslot/sigslot.h"
24 #include "rtc_base/time_utils.h"
25 
26 namespace rtc {
27 
28 // This structure holds the info needed to update the packet send time header
29 // extension, including the information needed to update the authentication tag
30 // after changing the value.
31 struct PacketTimeUpdateParams {
32   PacketTimeUpdateParams();
33   PacketTimeUpdateParams(const PacketTimeUpdateParams& other);
34   ~PacketTimeUpdateParams();
35 
36   int rtp_sendtime_extension_id = -1;  // extension header id present in packet.
37   std::vector<char> srtp_auth_key;     // Authentication key.
38   int srtp_auth_tag_len = -1;          // Authentication tag length.
39   int64_t srtp_packet_index = -1;  // Required for Rtp Packet authentication.
40 };
41 
42 // This structure holds meta information for the packet which is about to send
43 // over network.
44 struct RTC_EXPORT PacketOptions {
45   PacketOptions();
46   explicit PacketOptions(DiffServCodePoint dscp);
47   PacketOptions(const PacketOptions& other);
48   ~PacketOptions();
49 
50   DiffServCodePoint dscp = DSCP_NO_CHANGE;
51   // When used with RTP packets (for example, webrtc::PacketOptions), the value
52   // should be 16 bits. A value of -1 represents "not set".
53   int64_t packet_id = -1;
54   PacketTimeUpdateParams packet_time_params;
55   // PacketInfo is passed to SentPacket when signaling this packet is sent.
56   PacketInfo info_signaled_after_sent;
57 };
58 
59 // Provides the ability to receive packets asynchronously. Sends are not
60 // buffered since it is acceptable to drop packets under high load.
61 class RTC_EXPORT AsyncPacketSocket : public sigslot::has_slots<> {
62  public:
63   enum State {
64     STATE_CLOSED,
65     STATE_BINDING,
66     STATE_BOUND,
67     STATE_CONNECTING,
68     STATE_CONNECTED
69   };
70 
71   AsyncPacketSocket();
72   ~AsyncPacketSocket() override;
73 
74   AsyncPacketSocket(const AsyncPacketSocket&) = delete;
75   AsyncPacketSocket& operator=(const AsyncPacketSocket&) = delete;
76 
77   // Returns current local address. Address may be set to null if the
78   // socket is not bound yet (GetState() returns STATE_BINDING).
79   virtual SocketAddress GetLocalAddress() const = 0;
80 
81   // Returns remote address. Returns zeroes if this is not a client TCP socket.
82   virtual SocketAddress GetRemoteAddress() const = 0;
83 
84   // Send a packet.
85   virtual int Send(const void* pv, size_t cb, const PacketOptions& options) = 0;
86   virtual int SendTo(const void* pv,
87                      size_t cb,
88                      const SocketAddress& addr,
89                      const PacketOptions& options) = 0;
90 
91   // Close the socket.
92   virtual int Close() = 0;
93 
94   // Returns current state of the socket.
95   virtual State GetState() const = 0;
96 
97   // Get/set options.
98   virtual int GetOption(Socket::Option opt, int* value) = 0;
99   virtual int SetOption(Socket::Option opt, int value) = 0;
100 
101   // Get/Set current error.
102   // TODO: Remove SetError().
103   virtual int GetError() const = 0;
104   virtual void SetError(int error) = 0;
105 
106   // Register a callback to be called when the socket is closed.
107   void SubscribeClose(const void* removal_tag,
108                       std::function<void(AsyncPacketSocket*, int)> callback);
109   void UnsubscribeClose(const void* removal_tag);
110 
111   // Emitted each time a packet is read. Used only for UDP and
112   // connected TCP sockets.
113   sigslot::signal5<AsyncPacketSocket*,
114                    const char*,
115                    size_t,
116                    const SocketAddress&,
117                    // TODO(bugs.webrtc.org/9584): Change to passing the int64_t
118                    // timestamp by value.
119                    const int64_t&>
120       SignalReadPacket;
121 
122   // Emitted each time a packet is sent.
123   sigslot::signal2<AsyncPacketSocket*, const SentPacket&> SignalSentPacket;
124 
125   // Emitted when the socket is currently able to send.
126   sigslot::signal1<AsyncPacketSocket*> SignalReadyToSend;
127 
128   // Emitted after address for the socket is allocated, i.e. binding
129   // is finished. State of the socket is changed from BINDING to BOUND
130   // (for UDP sockets).
131   sigslot::signal2<AsyncPacketSocket*, const SocketAddress&> SignalAddressReady;
132 
133   // Emitted for client TCP sockets when state is changed from
134   // CONNECTING to CONNECTED.
135   sigslot::signal1<AsyncPacketSocket*> SignalConnect;
136 
NotifyClosedForTest(int err)137   void NotifyClosedForTest(int err) { NotifyClosed(err); }
138 
139  protected:
140   // TODO(bugs.webrtc.org/11943): Remove after updating downstream code.
SignalClose(AsyncPacketSocket * s,int err)141   void SignalClose(AsyncPacketSocket* s, int err) {
142     RTC_DCHECK_EQ(s, this);
143     NotifyClosed(err);
144   }
145 
NotifyClosed(int err)146   void NotifyClosed(int err) {
147     RTC_DCHECK_RUN_ON(&network_checker_);
148     on_close_.Send(this, err);
149   }
150 
151   RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker network_checker_;
152 
153  private:
154   webrtc::CallbackList<AsyncPacketSocket*, int> on_close_
155       RTC_GUARDED_BY(&network_checker_);
156 };
157 
158 // Listen socket, producing an AsyncPacketSocket when a peer connects.
159 class RTC_EXPORT AsyncListenSocket : public sigslot::has_slots<> {
160  public:
161   enum class State {
162     kClosed,
163     kBound,
164   };
165 
166   // Returns current state of the socket.
167   virtual State GetState() const = 0;
168 
169   // Returns current local address. Address may be set to null if the
170   // socket is not bound yet (GetState() returns kBinding).
171   virtual SocketAddress GetLocalAddress() const = 0;
172 
173   sigslot::signal2<AsyncListenSocket*, AsyncPacketSocket*> SignalNewConnection;
174 };
175 
176 void CopySocketInformationToPacketInfo(size_t packet_size_bytes,
177                                        const AsyncPacketSocket& socket_from,
178                                        bool is_connectionless,
179                                        rtc::PacketInfo* info);
180 
181 }  // namespace rtc
182 
183 #endif  // RTC_BASE_ASYNC_PACKET_SOCKET_H_
184