blob: e671f0d4be5543920973ea06d7a2d3e29bf33760 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_ASYNCPACKETSOCKET_H_
12#define RTC_BASE_ASYNCPACKETSOCKET_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/constructormagic.h"
Niels Möller15ca5a92018-11-01 14:32:47 +010015#include "rtc_base/deprecation.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/dscp.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/socket.h"
Artem Titove41c4332018-07-25 15:04:28 +020018#include "rtc_base/third_party/sigslot/sigslot.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/timeutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000020
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020021namespace rtc {
22
23// This structure holds the info needed to update the packet send time header
24// extension, including the information needed to update the authentication tag
25// after changing the value.
26struct PacketTimeUpdateParams {
27 PacketTimeUpdateParams();
Qingsi Wang6e641e62018-04-11 20:14:17 -070028 PacketTimeUpdateParams(const PacketTimeUpdateParams& other);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020029 ~PacketTimeUpdateParams();
30
Qingsi Wang6e641e62018-04-11 20:14:17 -070031 int rtp_sendtime_extension_id = -1; // extension header id present in packet.
Yves Gerey665174f2018-06-19 15:03:05 +020032 std::vector<char> srtp_auth_key; // Authentication key.
33 int srtp_auth_tag_len = -1; // Authentication tag length.
34 int64_t srtp_packet_index = -1; // Required for Rtp Packet authentication.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020035};
36
37// This structure holds meta information for the packet which is about to send
38// over network.
39struct PacketOptions {
Qingsi Wang6e641e62018-04-11 20:14:17 -070040 PacketOptions();
41 explicit PacketOptions(DiffServCodePoint dscp);
42 PacketOptions(const PacketOptions& other);
43 ~PacketOptions();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020044
Qingsi Wang6e641e62018-04-11 20:14:17 -070045 DiffServCodePoint dscp = DSCP_NO_CHANGE;
Bjorn Mellem3a9c46d2018-04-25 13:24:48 -070046 // When used with RTP packets (for example, webrtc::PacketOptions), the value
47 // should be 16 bits. A value of -1 represents "not set".
48 int64_t packet_id = -1;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020049 PacketTimeUpdateParams packet_time_params;
Qingsi Wang6e641e62018-04-11 20:14:17 -070050 // PacketInfo is passed to SentPacket when signaling this packet is sent.
51 PacketInfo info_signaled_after_sent;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020052};
53
54// This structure will have the information about when packet is actually
55// received by socket.
56struct PacketTime {
Niels Möller15ca5a92018-11-01 14:32:47 +010057 PacketTime() : timestamp(-1) {}
58 // Intentionally implicit.
59 PacketTime(int64_t timestamp) : timestamp(timestamp) {}
60 // Deprecated
61 PacketTime(int64_t timestamp, int64_t /* not_before */)
62 : timestamp(timestamp) {}
63
64 operator int64_t() const { return timestamp; }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020065
Yves Gerey665174f2018-06-19 15:03:05 +020066 int64_t timestamp; // Receive time after socket delivers the data.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020067};
68
Niels Möller15ca5a92018-11-01 14:32:47 +010069// Deprecated
70inline PacketTime CreatePacketTime(int64_t /* not_before */) {
71 return TimeMicros();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020072}
73
74// Provides the ability to receive packets asynchronously. Sends are not
75// buffered since it is acceptable to drop packets under high load.
76class AsyncPacketSocket : public sigslot::has_slots<> {
77 public:
78 enum State {
79 STATE_CLOSED,
80 STATE_BINDING,
81 STATE_BOUND,
82 STATE_CONNECTING,
83 STATE_CONNECTED
84 };
85
86 AsyncPacketSocket();
87 ~AsyncPacketSocket() override;
88
89 // Returns current local address. Address may be set to null if the
90 // socket is not bound yet (GetState() returns STATE_BINDING).
91 virtual SocketAddress GetLocalAddress() const = 0;
92
93 // Returns remote address. Returns zeroes if this is not a client TCP socket.
94 virtual SocketAddress GetRemoteAddress() const = 0;
95
96 // Send a packet.
Yves Gerey665174f2018-06-19 15:03:05 +020097 virtual int Send(const void* pv, size_t cb, const PacketOptions& options) = 0;
98 virtual int SendTo(const void* pv,
99 size_t cb,
100 const SocketAddress& addr,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200101 const PacketOptions& options) = 0;
102
103 // Close the socket.
104 virtual int Close() = 0;
105
106 // Returns current state of the socket.
107 virtual State GetState() const = 0;
108
109 // Get/set options.
110 virtual int GetOption(Socket::Option opt, int* value) = 0;
111 virtual int SetOption(Socket::Option opt, int value) = 0;
112
113 // Get/Set current error.
114 // TODO: Remove SetError().
115 virtual int GetError() const = 0;
116 virtual void SetError(int error) = 0;
117
118 // Emitted each time a packet is read. Used only for UDP and
119 // connected TCP sockets.
Yves Gerey665174f2018-06-19 15:03:05 +0200120 sigslot::signal5<AsyncPacketSocket*,
121 const char*,
122 size_t,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200123 const SocketAddress&,
Yves Gerey665174f2018-06-19 15:03:05 +0200124 const PacketTime&>
125 SignalReadPacket;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200126
127 // Emitted each time a packet is sent.
128 sigslot::signal2<AsyncPacketSocket*, const SentPacket&> SignalSentPacket;
129
130 // Emitted when the socket is currently able to send.
131 sigslot::signal1<AsyncPacketSocket*> SignalReadyToSend;
132
133 // Emitted after address for the socket is allocated, i.e. binding
134 // is finished. State of the socket is changed from BINDING to BOUND
135 // (for UDP and server TCP sockets) or CONNECTING (for client TCP
136 // sockets).
137 sigslot::signal2<AsyncPacketSocket*, const SocketAddress&> SignalAddressReady;
138
139 // Emitted for client TCP sockets when state is changed from
140 // CONNECTING to CONNECTED.
141 sigslot::signal1<AsyncPacketSocket*> SignalConnect;
142
143 // Emitted for client TCP sockets when state is changed from
144 // CONNECTED to CLOSED.
145 sigslot::signal2<AsyncPacketSocket*, int> SignalClose;
146
147 // Used only for listening TCP sockets.
148 sigslot::signal2<AsyncPacketSocket*, AsyncPacketSocket*> SignalNewConnection;
149
150 private:
151 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncPacketSocket);
152};
153
Qingsi Wang6e641e62018-04-11 20:14:17 -0700154void CopySocketInformationToPacketInfo(size_t packet_size_bytes,
155 const AsyncPacketSocket& socket_from,
Qingsi Wang4ea53b32018-04-16 18:22:31 -0700156 bool is_connectionless,
Qingsi Wang6e641e62018-04-11 20:14:17 -0700157 rtc::PacketInfo* info);
158
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200159} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000160
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200161#endif // RTC_BASE_ASYNCPACKETSOCKET_H_