blob: bb0b3bcc048e75fe486efdf6557f3ca7171cbb02 [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"
15#include "rtc_base/dscp.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/socket.h"
Artem Titove41c4332018-07-25 15:04:28 +020017#include "rtc_base/third_party/sigslot/sigslot.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/timeutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020020namespace rtc {
21
22// This structure holds the info needed to update the packet send time header
23// extension, including the information needed to update the authentication tag
24// after changing the value.
25struct PacketTimeUpdateParams {
26 PacketTimeUpdateParams();
Qingsi Wang6e641e62018-04-11 20:14:17 -070027 PacketTimeUpdateParams(const PacketTimeUpdateParams& other);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020028 ~PacketTimeUpdateParams();
29
Qingsi Wang6e641e62018-04-11 20:14:17 -070030 int rtp_sendtime_extension_id = -1; // extension header id present in packet.
Yves Gerey665174f2018-06-19 15:03:05 +020031 std::vector<char> srtp_auth_key; // Authentication key.
32 int srtp_auth_tag_len = -1; // Authentication tag length.
33 int64_t srtp_packet_index = -1; // Required for Rtp Packet authentication.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020034};
35
36// This structure holds meta information for the packet which is about to send
37// over network.
38struct PacketOptions {
Qingsi Wang6e641e62018-04-11 20:14:17 -070039 PacketOptions();
40 explicit PacketOptions(DiffServCodePoint dscp);
41 PacketOptions(const PacketOptions& other);
42 ~PacketOptions();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020043
Qingsi Wang6e641e62018-04-11 20:14:17 -070044 DiffServCodePoint dscp = DSCP_NO_CHANGE;
Bjorn Mellem3a9c46d2018-04-25 13:24:48 -070045 // When used with RTP packets (for example, webrtc::PacketOptions), the value
46 // should be 16 bits. A value of -1 represents "not set".
47 int64_t packet_id = -1;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020048 PacketTimeUpdateParams packet_time_params;
Qingsi Wang6e641e62018-04-11 20:14:17 -070049 // PacketInfo is passed to SentPacket when signaling this packet is sent.
50 PacketInfo info_signaled_after_sent;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020051};
52
53// This structure will have the information about when packet is actually
54// received by socket.
55struct PacketTime {
56 PacketTime() : timestamp(-1), not_before(-1) {}
57 PacketTime(int64_t timestamp, int64_t not_before)
58 : timestamp(timestamp), not_before(not_before) {}
59
Yves Gerey665174f2018-06-19 15:03:05 +020060 int64_t timestamp; // Receive time after socket delivers the data.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020061
62 // Earliest possible time the data could have arrived, indicating the
63 // potential error in the |timestamp| value, in case the system, is busy. For
64 // example, the time of the last select() call.
65 // If unknown, this value will be set to zero.
66 int64_t not_before;
67};
68
69inline PacketTime CreatePacketTime(int64_t not_before) {
70 return PacketTime(TimeMicros(), not_before);
71}
72
73// Provides the ability to receive packets asynchronously. Sends are not
74// buffered since it is acceptable to drop packets under high load.
75class AsyncPacketSocket : public sigslot::has_slots<> {
76 public:
77 enum State {
78 STATE_CLOSED,
79 STATE_BINDING,
80 STATE_BOUND,
81 STATE_CONNECTING,
82 STATE_CONNECTED
83 };
84
85 AsyncPacketSocket();
86 ~AsyncPacketSocket() override;
87
88 // Returns current local address. Address may be set to null if the
89 // socket is not bound yet (GetState() returns STATE_BINDING).
90 virtual SocketAddress GetLocalAddress() const = 0;
91
92 // Returns remote address. Returns zeroes if this is not a client TCP socket.
93 virtual SocketAddress GetRemoteAddress() const = 0;
94
95 // Send a packet.
Yves Gerey665174f2018-06-19 15:03:05 +020096 virtual int Send(const void* pv, size_t cb, const PacketOptions& options) = 0;
97 virtual int SendTo(const void* pv,
98 size_t cb,
99 const SocketAddress& addr,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200100 const PacketOptions& options) = 0;
101
102 // Close the socket.
103 virtual int Close() = 0;
104
105 // Returns current state of the socket.
106 virtual State GetState() const = 0;
107
108 // Get/set options.
109 virtual int GetOption(Socket::Option opt, int* value) = 0;
110 virtual int SetOption(Socket::Option opt, int value) = 0;
111
112 // Get/Set current error.
113 // TODO: Remove SetError().
114 virtual int GetError() const = 0;
115 virtual void SetError(int error) = 0;
116
117 // Emitted each time a packet is read. Used only for UDP and
118 // connected TCP sockets.
Yves Gerey665174f2018-06-19 15:03:05 +0200119 sigslot::signal5<AsyncPacketSocket*,
120 const char*,
121 size_t,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200122 const SocketAddress&,
Yves Gerey665174f2018-06-19 15:03:05 +0200123 const PacketTime&>
124 SignalReadPacket;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200125
126 // Emitted each time a packet is sent.
127 sigslot::signal2<AsyncPacketSocket*, const SentPacket&> SignalSentPacket;
128
129 // Emitted when the socket is currently able to send.
130 sigslot::signal1<AsyncPacketSocket*> SignalReadyToSend;
131
132 // Emitted after address for the socket is allocated, i.e. binding
133 // is finished. State of the socket is changed from BINDING to BOUND
134 // (for UDP and server TCP sockets) or CONNECTING (for client TCP
135 // sockets).
136 sigslot::signal2<AsyncPacketSocket*, const SocketAddress&> SignalAddressReady;
137
138 // Emitted for client TCP sockets when state is changed from
139 // CONNECTING to CONNECTED.
140 sigslot::signal1<AsyncPacketSocket*> SignalConnect;
141
142 // Emitted for client TCP sockets when state is changed from
143 // CONNECTED to CLOSED.
144 sigslot::signal2<AsyncPacketSocket*, int> SignalClose;
145
146 // Used only for listening TCP sockets.
147 sigslot::signal2<AsyncPacketSocket*, AsyncPacketSocket*> SignalNewConnection;
148
149 private:
150 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncPacketSocket);
151};
152
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200160#endif // RTC_BASE_ASYNCPACKETSOCKET_H_