blob: 768fcd446b83e4cfd1510d5864488ae8c528afe7 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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
Steve Anton10542f22019-01-11 17:11:0011#ifndef RTC_BASE_ASYNC_PACKET_SOCKET_H_
12#define RTC_BASE_ASYNC_PACKET_SOCKET_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2613
Per K4ac37182023-11-15 09:43:3214#include <cstdint>
Steve Antonf4172382020-01-27 23:45:0215#include <vector>
16
Tomas Gunnarssonf15189d2022-04-13 09:03:5217#include "api/sequence_checker.h"
18#include "rtc_base/callback_list.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3119#include "rtc_base/dscp.h"
Per K4ac37182023-11-15 09:43:3220#include "rtc_base/network/received_packet.h"
Yves Gerey3e707812018-11-28 15:47:4921#include "rtc_base/network/sent_packet.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3122#include "rtc_base/socket.h"
Tomas Gunnarssonf15189d2022-04-13 09:03:5223#include "rtc_base/system/no_unique_address.h"
Mirko Bonadei35214fc2019-09-23 12:54:2824#include "rtc_base/system/rtc_export.h"
Artem Titove41c4332018-07-25 13:04:2825#include "rtc_base/third_party/sigslot/sigslot.h"
Steve Anton10542f22019-01-11 17:11:0026#include "rtc_base/time_utils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2627
Henrik Kjellanderec78f1c2017-06-29 05:52:5028namespace rtc {
29
30// This structure holds the info needed to update the packet send time header
31// extension, including the information needed to update the authentication tag
32// after changing the value.
33struct PacketTimeUpdateParams {
34 PacketTimeUpdateParams();
Qingsi Wang6e641e62018-04-12 03:14:1735 PacketTimeUpdateParams(const PacketTimeUpdateParams& other);
Henrik Kjellanderec78f1c2017-06-29 05:52:5036 ~PacketTimeUpdateParams();
37
Qingsi Wang6e641e62018-04-12 03:14:1738 int rtp_sendtime_extension_id = -1; // extension header id present in packet.
Yves Gerey665174f2018-06-19 13:03:0539 std::vector<char> srtp_auth_key; // Authentication key.
40 int srtp_auth_tag_len = -1; // Authentication tag length.
41 int64_t srtp_packet_index = -1; // Required for Rtp Packet authentication.
Henrik Kjellanderec78f1c2017-06-29 05:52:5042};
43
44// This structure holds meta information for the packet which is about to send
45// over network.
Mirko Bonadei35214fc2019-09-23 12:54:2846struct RTC_EXPORT PacketOptions {
Qingsi Wang6e641e62018-04-12 03:14:1747 PacketOptions();
48 explicit PacketOptions(DiffServCodePoint dscp);
49 PacketOptions(const PacketOptions& other);
50 ~PacketOptions();
Henrik Kjellanderec78f1c2017-06-29 05:52:5051
Qingsi Wang6e641e62018-04-12 03:14:1752 DiffServCodePoint dscp = DSCP_NO_CHANGE;
Bjorn Mellem3a9c46d2018-04-25 20:24:4853 // When used with RTP packets (for example, webrtc::PacketOptions), the value
54 // should be 16 bits. A value of -1 represents "not set".
55 int64_t packet_id = -1;
Henrik Kjellanderec78f1c2017-06-29 05:52:5056 PacketTimeUpdateParams packet_time_params;
Qingsi Wang6e641e62018-04-12 03:14:1757 // PacketInfo is passed to SentPacket when signaling this packet is sent.
58 PacketInfo info_signaled_after_sent;
Markus Handellc8c4a282023-05-08 14:46:2159 // True if this is a batchable packet. Batchable packets are collected at low
60 // levels and sent first when their AsyncPacketSocket receives a
61 // OnSendBatchComplete call.
62 bool batchable = false;
63 // True if this is the last packet of a batch.
64 bool last_packet_in_batch = false;
Henrik Kjellanderec78f1c2017-06-29 05:52:5065};
66
Henrik Kjellanderec78f1c2017-06-29 05:52:5067// Provides the ability to receive packets asynchronously. Sends are not
68// buffered since it is acceptable to drop packets under high load.
Mirko Bonadei35214fc2019-09-23 12:54:2869class RTC_EXPORT AsyncPacketSocket : public sigslot::has_slots<> {
Henrik Kjellanderec78f1c2017-06-29 05:52:5070 public:
71 enum State {
72 STATE_CLOSED,
73 STATE_BINDING,
74 STATE_BOUND,
75 STATE_CONNECTING,
76 STATE_CONNECTED
77 };
78
Tommic8482682023-03-23 21:20:3979 AsyncPacketSocket() = default;
Henrik Kjellanderec78f1c2017-06-29 05:52:5080 ~AsyncPacketSocket() override;
81
Byoungchan Lee14af7622022-01-11 20:24:5882 AsyncPacketSocket(const AsyncPacketSocket&) = delete;
83 AsyncPacketSocket& operator=(const AsyncPacketSocket&) = delete;
84
Henrik Kjellanderec78f1c2017-06-29 05:52:5085 // Returns current local address. Address may be set to null if the
86 // socket is not bound yet (GetState() returns STATE_BINDING).
87 virtual SocketAddress GetLocalAddress() const = 0;
88
89 // Returns remote address. Returns zeroes if this is not a client TCP socket.
90 virtual SocketAddress GetRemoteAddress() const = 0;
91
92 // Send a packet.
Yves Gerey665174f2018-06-19 13:03:0593 virtual int Send(const void* pv, size_t cb, const PacketOptions& options) = 0;
94 virtual int SendTo(const void* pv,
95 size_t cb,
96 const SocketAddress& addr,
Henrik Kjellanderec78f1c2017-06-29 05:52:5097 const PacketOptions& options) = 0;
98
99 // Close the socket.
100 virtual int Close() = 0;
101
102 // Returns current state of the socket.
103 virtual State GetState() const = 0;
104
105 // Get/set options.
106 virtual int GetOption(Socket::Option opt, int* value) = 0;
107 virtual int SetOption(Socket::Option opt, int value) = 0;
108
109 // Get/Set current error.
110 // TODO: Remove SetError().
111 virtual int GetError() const = 0;
112 virtual void SetError(int error) = 0;
113
Tomas Gunnarssonf15189d2022-04-13 09:03:52114 // Register a callback to be called when the socket is closed.
Tommi2afd2842023-09-05 07:36:24115 void SubscribeCloseEvent(
116 const void* removal_tag,
117 std::function<void(AsyncPacketSocket*, int)> callback);
118 void UnsubscribeCloseEvent(const void* removal_tag);
Tomas Gunnarssonf15189d2022-04-13 09:03:52119
Per K4ac37182023-11-15 09:43:32120 void RegisterReceivedPacketCallback(
121 absl::AnyInvocable<void(AsyncPacketSocket*, const rtc::ReceivedPacket&)>
122 received_packet_callback);
123 void DeregisterReceivedPacketCallback();
124
Henrik Kjellanderec78f1c2017-06-29 05:52:50125 // Emitted each time a packet is read. Used only for UDP and
126 // connected TCP sockets.
Per K4ac37182023-11-15 09:43:32127 // TODO(bugs.webrtc.org:15368): Deprecate and remove.
Yves Gerey665174f2018-06-19 13:03:05128 sigslot::signal5<AsyncPacketSocket*,
129 const char*,
130 size_t,
Henrik Kjellanderec78f1c2017-06-29 05:52:50131 const SocketAddress&,
Niels Möllere6933812018-11-05 12:01:41132 // TODO(bugs.webrtc.org/9584): Change to passing the int64_t
133 // timestamp by value.
134 const int64_t&>
Yves Gerey665174f2018-06-19 13:03:05135 SignalReadPacket;
Henrik Kjellanderec78f1c2017-06-29 05:52:50136
137 // Emitted each time a packet is sent.
138 sigslot::signal2<AsyncPacketSocket*, const SentPacket&> SignalSentPacket;
139
140 // Emitted when the socket is currently able to send.
141 sigslot::signal1<AsyncPacketSocket*> SignalReadyToSend;
142
143 // Emitted after address for the socket is allocated, i.e. binding
144 // is finished. State of the socket is changed from BINDING to BOUND
Niels Möller4a1c2c42021-09-28 08:17:07145 // (for UDP sockets).
Henrik Kjellanderec78f1c2017-06-29 05:52:50146 sigslot::signal2<AsyncPacketSocket*, const SocketAddress&> SignalAddressReady;
147
148 // Emitted for client TCP sockets when state is changed from
149 // CONNECTING to CONNECTED.
150 sigslot::signal1<AsyncPacketSocket*> SignalConnect;
151
Tomas Gunnarssonf15189d2022-04-13 09:03:52152 void NotifyClosedForTest(int err) { NotifyClosed(err); }
153
154 protected:
155 // TODO(bugs.webrtc.org/11943): Remove after updating downstream code.
156 void SignalClose(AsyncPacketSocket* s, int err) {
157 RTC_DCHECK_EQ(s, this);
158 NotifyClosed(err);
159 }
160
161 void NotifyClosed(int err) {
162 RTC_DCHECK_RUN_ON(&network_checker_);
163 on_close_.Send(this, err);
164 }
165
Per K4ac37182023-11-15 09:43:32166 // TODO(bugs.webrtc.org:15368): Deprecate and remove.
167 void NotifyPacketReceived(AsyncPacketSocket*,
168 const char* data,
169 size_t size,
170 const SocketAddress& address,
171 const int64_t& packet_time_us) {
172 NotifyPacketReceived(
173 ReceivedPacket::CreateFromLegacy(data, size, packet_time_us, address));
174 }
175
176 void NotifyPacketReceived(const rtc::ReceivedPacket& packet);
177
Tommic8482682023-03-23 21:20:39178 RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker network_checker_{
179 webrtc::SequenceChecker::kDetached};
Tomas Gunnarssonf15189d2022-04-13 09:03:52180
181 private:
182 webrtc::CallbackList<AsyncPacketSocket*, int> on_close_
183 RTC_GUARDED_BY(&network_checker_);
Per K4ac37182023-11-15 09:43:32184 absl::AnyInvocable<void(AsyncPacketSocket*, const rtc::ReceivedPacket&)>
185 received_packet_callback_ RTC_GUARDED_BY(&network_checker_);
Henrik Kjellanderec78f1c2017-06-29 05:52:50186};
187
Niels Möllerd30ece12021-10-19 08:11:02188// Listen socket, producing an AsyncPacketSocket when a peer connects.
189class RTC_EXPORT AsyncListenSocket : public sigslot::has_slots<> {
190 public:
191 enum class State {
192 kClosed,
193 kBound,
194 };
195
196 // Returns current state of the socket.
197 virtual State GetState() const = 0;
198
199 // Returns current local address. Address may be set to null if the
200 // socket is not bound yet (GetState() returns kBinding).
201 virtual SocketAddress GetLocalAddress() const = 0;
202
Niels Möllerd30ece12021-10-19 08:11:02203 sigslot::signal2<AsyncListenSocket*, AsyncPacketSocket*> SignalNewConnection;
204};
Niels Möller6d19d142021-10-06 09:19:03205
Qingsi Wang6e641e62018-04-12 03:14:17206void CopySocketInformationToPacketInfo(size_t packet_size_bytes,
207 const AsyncPacketSocket& socket_from,
Qingsi Wang4ea53b32018-04-17 01:22:31208 bool is_connectionless,
Qingsi Wang6e641e62018-04-12 03:14:17209 rtc::PacketInfo* info);
210
Henrik Kjellanderec78f1c2017-06-29 05:52:50211} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26212
Steve Anton10542f22019-01-11 17:11:00213#endif // RTC_BASE_ASYNC_PACKET_SOCKET_H_