blob: 2e334ec36d6d8048c8a4dc2181a8df7b6c6e2682 [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
Steve Antonf4172382020-01-27 23:45:0214#include <vector>
15
Mirko Bonadei92ea95e2017-09-15 04:47:3116#include "rtc_base/dscp.h"
Yves Gerey3e707812018-11-28 15:47:4917#include "rtc_base/network/sent_packet.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "rtc_base/socket.h"
Mirko Bonadei35214fc2019-09-23 12:54:2819#include "rtc_base/system/rtc_export.h"
Artem Titove41c4332018-07-25 13:04:2820#include "rtc_base/third_party/sigslot/sigslot.h"
Steve Anton10542f22019-01-11 17:11:0021#include "rtc_base/time_utils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2622
Henrik Kjellanderec78f1c2017-06-29 05:52:5023namespace rtc {
24
25// This structure holds the info needed to update the packet send time header
26// extension, including the information needed to update the authentication tag
27// after changing the value.
28struct PacketTimeUpdateParams {
29 PacketTimeUpdateParams();
Qingsi Wang6e641e62018-04-12 03:14:1730 PacketTimeUpdateParams(const PacketTimeUpdateParams& other);
Henrik Kjellanderec78f1c2017-06-29 05:52:5031 ~PacketTimeUpdateParams();
32
Qingsi Wang6e641e62018-04-12 03:14:1733 int rtp_sendtime_extension_id = -1; // extension header id present in packet.
Yves Gerey665174f2018-06-19 13:03:0534 std::vector<char> srtp_auth_key; // Authentication key.
35 int srtp_auth_tag_len = -1; // Authentication tag length.
36 int64_t srtp_packet_index = -1; // Required for Rtp Packet authentication.
Henrik Kjellanderec78f1c2017-06-29 05:52:5037};
38
39// This structure holds meta information for the packet which is about to send
40// over network.
Mirko Bonadei35214fc2019-09-23 12:54:2841struct RTC_EXPORT PacketOptions {
Qingsi Wang6e641e62018-04-12 03:14:1742 PacketOptions();
43 explicit PacketOptions(DiffServCodePoint dscp);
44 PacketOptions(const PacketOptions& other);
45 ~PacketOptions();
Henrik Kjellanderec78f1c2017-06-29 05:52:5046
Qingsi Wang6e641e62018-04-12 03:14:1747 DiffServCodePoint dscp = DSCP_NO_CHANGE;
Bjorn Mellem3a9c46d2018-04-25 20:24:4848 // When used with RTP packets (for example, webrtc::PacketOptions), the value
49 // should be 16 bits. A value of -1 represents "not set".
50 int64_t packet_id = -1;
Henrik Kjellanderec78f1c2017-06-29 05:52:5051 PacketTimeUpdateParams packet_time_params;
Qingsi Wang6e641e62018-04-12 03:14:1752 // PacketInfo is passed to SentPacket when signaling this packet is sent.
53 PacketInfo info_signaled_after_sent;
Henrik Kjellanderec78f1c2017-06-29 05:52:5054};
55
Henrik Kjellanderec78f1c2017-06-29 05:52:5056// Provides the ability to receive packets asynchronously. Sends are not
57// buffered since it is acceptable to drop packets under high load.
Mirko Bonadei35214fc2019-09-23 12:54:2858class RTC_EXPORT AsyncPacketSocket : public sigslot::has_slots<> {
Henrik Kjellanderec78f1c2017-06-29 05:52:5059 public:
60 enum State {
61 STATE_CLOSED,
62 STATE_BINDING,
63 STATE_BOUND,
64 STATE_CONNECTING,
65 STATE_CONNECTED
66 };
67
68 AsyncPacketSocket();
69 ~AsyncPacketSocket() override;
70
Byoungchan Lee14af7622022-01-11 20:24:5871 AsyncPacketSocket(const AsyncPacketSocket&) = delete;
72 AsyncPacketSocket& operator=(const AsyncPacketSocket&) = delete;
73
Henrik Kjellanderec78f1c2017-06-29 05:52:5074 // Returns current local address. Address may be set to null if the
75 // socket is not bound yet (GetState() returns STATE_BINDING).
76 virtual SocketAddress GetLocalAddress() const = 0;
77
78 // Returns remote address. Returns zeroes if this is not a client TCP socket.
79 virtual SocketAddress GetRemoteAddress() const = 0;
80
81 // Send a packet.
Yves Gerey665174f2018-06-19 13:03:0582 virtual int Send(const void* pv, size_t cb, const PacketOptions& options) = 0;
83 virtual int SendTo(const void* pv,
84 size_t cb,
85 const SocketAddress& addr,
Henrik Kjellanderec78f1c2017-06-29 05:52:5086 const PacketOptions& options) = 0;
87
88 // Close the socket.
89 virtual int Close() = 0;
90
91 // Returns current state of the socket.
92 virtual State GetState() const = 0;
93
94 // Get/set options.
95 virtual int GetOption(Socket::Option opt, int* value) = 0;
96 virtual int SetOption(Socket::Option opt, int value) = 0;
97
98 // Get/Set current error.
99 // TODO: Remove SetError().
100 virtual int GetError() const = 0;
101 virtual void SetError(int error) = 0;
102
103 // Emitted each time a packet is read. Used only for UDP and
104 // connected TCP sockets.
Yves Gerey665174f2018-06-19 13:03:05105 sigslot::signal5<AsyncPacketSocket*,
106 const char*,
107 size_t,
Henrik Kjellanderec78f1c2017-06-29 05:52:50108 const SocketAddress&,
Niels Möllere6933812018-11-05 12:01:41109 // TODO(bugs.webrtc.org/9584): Change to passing the int64_t
110 // timestamp by value.
111 const int64_t&>
Yves Gerey665174f2018-06-19 13:03:05112 SignalReadPacket;
Henrik Kjellanderec78f1c2017-06-29 05:52:50113
114 // Emitted each time a packet is sent.
115 sigslot::signal2<AsyncPacketSocket*, const SentPacket&> SignalSentPacket;
116
117 // Emitted when the socket is currently able to send.
118 sigslot::signal1<AsyncPacketSocket*> SignalReadyToSend;
119
120 // Emitted after address for the socket is allocated, i.e. binding
121 // is finished. State of the socket is changed from BINDING to BOUND
Niels Möller4a1c2c42021-09-28 08:17:07122 // (for UDP sockets).
Henrik Kjellanderec78f1c2017-06-29 05:52:50123 sigslot::signal2<AsyncPacketSocket*, const SocketAddress&> SignalAddressReady;
124
125 // Emitted for client TCP sockets when state is changed from
126 // CONNECTING to CONNECTED.
127 sigslot::signal1<AsyncPacketSocket*> SignalConnect;
128
129 // Emitted for client TCP sockets when state is changed from
130 // CONNECTED to CLOSED.
131 sigslot::signal2<AsyncPacketSocket*, int> SignalClose;
Henrik Kjellanderec78f1c2017-06-29 05:52:50132};
133
Niels Möllerd30ece12021-10-19 08:11:02134// Listen socket, producing an AsyncPacketSocket when a peer connects.
135class RTC_EXPORT AsyncListenSocket : public sigslot::has_slots<> {
136 public:
137 enum class State {
138 kClosed,
139 kBound,
140 };
141
142 // Returns current state of the socket.
143 virtual State GetState() const = 0;
144
145 // Returns current local address. Address may be set to null if the
146 // socket is not bound yet (GetState() returns kBinding).
147 virtual SocketAddress GetLocalAddress() const = 0;
148
Niels Möllerd30ece12021-10-19 08:11:02149 sigslot::signal2<AsyncListenSocket*, AsyncPacketSocket*> SignalNewConnection;
150};
Niels Möller6d19d142021-10-06 09:19:03151
Qingsi Wang6e641e62018-04-12 03:14:17152void CopySocketInformationToPacketInfo(size_t packet_size_bytes,
153 const AsyncPacketSocket& socket_from,
Qingsi Wang4ea53b32018-04-17 01:22:31154 bool is_connectionless,
Qingsi Wang6e641e62018-04-12 03:14:17155 rtc::PacketInfo* info);
156
Henrik Kjellanderec78f1c2017-06-29 05:52:50157} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26158
Steve Anton10542f22019-01-11 17:11:00159#endif // RTC_BASE_ASYNC_PACKET_SOCKET_H_