blob: b5fcf8edb7737b4c1897e5858398eb0d5334cce5 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 09:11:00 -080011#ifndef RTC_BASE_ASYNC_PACKET_SOCKET_H_
12#define RTC_BASE_ASYNC_PACKET_SOCKET_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Steve Antonf4172382020-01-27 15:45:02 -080014#include <vector>
15
Steve Anton10542f22019-01-11 09:11:00 -080016#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/dscp.h"
Yves Gerey3e707812018-11-28 16:47:49 +010018#include "rtc_base/network/sent_packet.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/socket.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020020#include "rtc_base/system/rtc_export.h"
Artem Titove41c4332018-07-25 15:04:28 +020021#include "rtc_base/third_party/sigslot/sigslot.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/time_utils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020024namespace rtc {
25
26// This structure holds the info needed to update the packet send time header
27// extension, including the information needed to update the authentication tag
28// after changing the value.
29struct PacketTimeUpdateParams {
30 PacketTimeUpdateParams();
Qingsi Wang6e641e62018-04-11 20:14:17 -070031 PacketTimeUpdateParams(const PacketTimeUpdateParams& other);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020032 ~PacketTimeUpdateParams();
33
Qingsi Wang6e641e62018-04-11 20:14:17 -070034 int rtp_sendtime_extension_id = -1; // extension header id present in packet.
Yves Gerey665174f2018-06-19 15:03:05 +020035 std::vector<char> srtp_auth_key; // Authentication key.
36 int srtp_auth_tag_len = -1; // Authentication tag length.
37 int64_t srtp_packet_index = -1; // Required for Rtp Packet authentication.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020038};
39
40// This structure holds meta information for the packet which is about to send
41// over network.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020042struct RTC_EXPORT PacketOptions {
Qingsi Wang6e641e62018-04-11 20:14:17 -070043 PacketOptions();
44 explicit PacketOptions(DiffServCodePoint dscp);
45 PacketOptions(const PacketOptions& other);
46 ~PacketOptions();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020047
Qingsi Wang6e641e62018-04-11 20:14:17 -070048 DiffServCodePoint dscp = DSCP_NO_CHANGE;
Bjorn Mellem3a9c46d2018-04-25 13:24:48 -070049 // When used with RTP packets (for example, webrtc::PacketOptions), the value
50 // should be 16 bits. A value of -1 represents "not set".
51 int64_t packet_id = -1;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020052 PacketTimeUpdateParams packet_time_params;
Qingsi Wang6e641e62018-04-11 20:14:17 -070053 // PacketInfo is passed to SentPacket when signaling this packet is sent.
54 PacketInfo info_signaled_after_sent;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020055};
56
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020057// Provides the ability to receive packets asynchronously. Sends are not
58// buffered since it is acceptable to drop packets under high load.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020059class RTC_EXPORT AsyncPacketSocket : public sigslot::has_slots<> {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020060 public:
61 enum State {
62 STATE_CLOSED,
63 STATE_BINDING,
64 STATE_BOUND,
65 STATE_CONNECTING,
66 STATE_CONNECTED
67 };
68
69 AsyncPacketSocket();
70 ~AsyncPacketSocket() override;
71
72 // Returns current local address. Address may be set to null if the
73 // socket is not bound yet (GetState() returns STATE_BINDING).
74 virtual SocketAddress GetLocalAddress() const = 0;
75
76 // Returns remote address. Returns zeroes if this is not a client TCP socket.
77 virtual SocketAddress GetRemoteAddress() const = 0;
78
79 // Send a packet.
Yves Gerey665174f2018-06-19 15:03:05 +020080 virtual int Send(const void* pv, size_t cb, const PacketOptions& options) = 0;
81 virtual int SendTo(const void* pv,
82 size_t cb,
83 const SocketAddress& addr,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020084 const PacketOptions& options) = 0;
85
86 // Close the socket.
87 virtual int Close() = 0;
88
89 // Returns current state of the socket.
90 virtual State GetState() const = 0;
91
92 // Get/set options.
93 virtual int GetOption(Socket::Option opt, int* value) = 0;
94 virtual int SetOption(Socket::Option opt, int value) = 0;
95
96 // Get/Set current error.
97 // TODO: Remove SetError().
98 virtual int GetError() const = 0;
99 virtual void SetError(int error) = 0;
100
101 // Emitted each time a packet is read. Used only for UDP and
102 // connected TCP sockets.
Yves Gerey665174f2018-06-19 15:03:05 +0200103 sigslot::signal5<AsyncPacketSocket*,
104 const char*,
105 size_t,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200106 const SocketAddress&,
Niels Möllere6933812018-11-05 13:01:41 +0100107 // TODO(bugs.webrtc.org/9584): Change to passing the int64_t
108 // timestamp by value.
109 const int64_t&>
Yves Gerey665174f2018-06-19 15:03:05 +0200110 SignalReadPacket;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200111
112 // Emitted each time a packet is sent.
113 sigslot::signal2<AsyncPacketSocket*, const SentPacket&> SignalSentPacket;
114
115 // Emitted when the socket is currently able to send.
116 sigslot::signal1<AsyncPacketSocket*> SignalReadyToSend;
117
118 // Emitted after address for the socket is allocated, i.e. binding
119 // is finished. State of the socket is changed from BINDING to BOUND
Niels Möller4a1c2c42021-09-28 10:17:07 +0200120 // (for UDP sockets).
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200121 sigslot::signal2<AsyncPacketSocket*, const SocketAddress&> SignalAddressReady;
122
123 // Emitted for client TCP sockets when state is changed from
124 // CONNECTING to CONNECTED.
125 sigslot::signal1<AsyncPacketSocket*> SignalConnect;
126
127 // Emitted for client TCP sockets when state is changed from
128 // CONNECTED to CLOSED.
129 sigslot::signal2<AsyncPacketSocket*, int> SignalClose;
130
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200131 private:
132 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncPacketSocket);
133};
134
Niels Möllerd30ece12021-10-19 10:11:02 +0200135// Listen socket, producing an AsyncPacketSocket when a peer connects.
136class RTC_EXPORT AsyncListenSocket : public sigslot::has_slots<> {
137 public:
138 enum class State {
139 kClosed,
140 kBound,
141 };
142
143 // Returns current state of the socket.
144 virtual State GetState() const = 0;
145
146 // Returns current local address. Address may be set to null if the
147 // socket is not bound yet (GetState() returns kBinding).
148 virtual SocketAddress GetLocalAddress() const = 0;
149
Niels Möllerd30ece12021-10-19 10:11:02 +0200150 sigslot::signal2<AsyncListenSocket*, AsyncPacketSocket*> SignalNewConnection;
151};
Niels Möller6d19d142021-10-06 11:19:03 +0200152
Qingsi Wang6e641e62018-04-11 20:14:17 -0700153void CopySocketInformationToPacketInfo(size_t packet_size_bytes,
154 const AsyncPacketSocket& socket_from,
Qingsi Wang4ea53b32018-04-16 18:22:31 -0700155 bool is_connectionless,
Qingsi Wang6e641e62018-04-11 20:14:17 -0700156 rtc::PacketInfo* info);
157
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200158} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000159
Steve Anton10542f22019-01-11 09:11:00 -0800160#endif // RTC_BASE_ASYNC_PACKET_SOCKET_H_