henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_ASYNCPACKETSOCKET_H_ |
| 12 | #define RTC_BASE_ASYNCPACKETSOCKET_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "rtc_base/constructormagic.h" |
Niels Möller | 15ca5a9 | 2018-11-01 14:32:47 +0100 | [diff] [blame] | 15 | #include "rtc_base/deprecation.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/dscp.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/socket.h" |
Artem Titov | e41c433 | 2018-07-25 15:04:28 +0200 | [diff] [blame] | 18 | #include "rtc_base/third_party/sigslot/sigslot.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "rtc_base/timeutils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 20 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 21 | namespace 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. |
| 26 | struct PacketTimeUpdateParams { |
| 27 | PacketTimeUpdateParams(); |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 28 | PacketTimeUpdateParams(const PacketTimeUpdateParams& other); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 29 | ~PacketTimeUpdateParams(); |
| 30 | |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 31 | int rtp_sendtime_extension_id = -1; // extension header id present in packet. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 32 | 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 Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | // This structure holds meta information for the packet which is about to send |
| 38 | // over network. |
| 39 | struct PacketOptions { |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 40 | PacketOptions(); |
| 41 | explicit PacketOptions(DiffServCodePoint dscp); |
| 42 | PacketOptions(const PacketOptions& other); |
| 43 | ~PacketOptions(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 44 | |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 45 | DiffServCodePoint dscp = DSCP_NO_CHANGE; |
Bjorn Mellem | 3a9c46d | 2018-04-25 13:24:48 -0700 | [diff] [blame] | 46 | // 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 Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 49 | PacketTimeUpdateParams packet_time_params; |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 50 | // PacketInfo is passed to SentPacket when signaling this packet is sent. |
| 51 | PacketInfo info_signaled_after_sent; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | // This structure will have the information about when packet is actually |
| 55 | // received by socket. |
| 56 | struct PacketTime { |
Niels Möller | 15ca5a9 | 2018-11-01 14:32:47 +0100 | [diff] [blame] | 57 | 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 Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 65 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 66 | int64_t timestamp; // Receive time after socket delivers the data. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 67 | }; |
| 68 | |
Niels Möller | 15ca5a9 | 2018-11-01 14:32:47 +0100 | [diff] [blame] | 69 | // Deprecated |
| 70 | inline PacketTime CreatePacketTime(int64_t /* not_before */) { |
| 71 | return TimeMicros(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // Provides the ability to receive packets asynchronously. Sends are not |
| 75 | // buffered since it is acceptable to drop packets under high load. |
| 76 | class 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 97 | 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 Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 101 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 120 | sigslot::signal5<AsyncPacketSocket*, |
| 121 | const char*, |
| 122 | size_t, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 123 | const SocketAddress&, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 124 | const PacketTime&> |
| 125 | SignalReadPacket; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 126 | |
| 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 Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 154 | void CopySocketInformationToPacketInfo(size_t packet_size_bytes, |
| 155 | const AsyncPacketSocket& socket_from, |
Qingsi Wang | 4ea53b3 | 2018-04-16 18:22:31 -0700 | [diff] [blame] | 156 | bool is_connectionless, |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 157 | rtc::PacketInfo* info); |
| 158 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 159 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 160 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 161 | #endif // RTC_BASE_ASYNCPACKETSOCKET_H_ |