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" |
| 15 | #include "rtc_base/dscp.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/socket.h" |
Artem Titov | e41c433 | 2018-07-25 15:04:28 +0200 | [diff] [blame^] | 17 | #include "rtc_base/third_party/sigslot/sigslot.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/timeutils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 19 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 20 | namespace 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. |
| 25 | struct PacketTimeUpdateParams { |
| 26 | PacketTimeUpdateParams(); |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 27 | PacketTimeUpdateParams(const PacketTimeUpdateParams& other); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 28 | ~PacketTimeUpdateParams(); |
| 29 | |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 30 | int rtp_sendtime_extension_id = -1; // extension header id present in packet. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 31 | 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 Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | // This structure holds meta information for the packet which is about to send |
| 37 | // over network. |
| 38 | struct PacketOptions { |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 39 | PacketOptions(); |
| 40 | explicit PacketOptions(DiffServCodePoint dscp); |
| 41 | PacketOptions(const PacketOptions& other); |
| 42 | ~PacketOptions(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 43 | |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 44 | DiffServCodePoint dscp = DSCP_NO_CHANGE; |
Bjorn Mellem | 3a9c46d | 2018-04-25 13:24:48 -0700 | [diff] [blame] | 45 | // 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 Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 48 | PacketTimeUpdateParams packet_time_params; |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 49 | // PacketInfo is passed to SentPacket when signaling this packet is sent. |
| 50 | PacketInfo info_signaled_after_sent; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | // This structure will have the information about when packet is actually |
| 54 | // received by socket. |
| 55 | struct 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 60 | int64_t timestamp; // Receive time after socket delivers the data. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 61 | |
| 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 | |
| 69 | inline 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. |
| 75 | class 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 96 | 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 Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 100 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 119 | sigslot::signal5<AsyncPacketSocket*, |
| 120 | const char*, |
| 121 | size_t, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 122 | const SocketAddress&, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 123 | const PacketTime&> |
| 124 | SignalReadPacket; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 125 | |
| 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 Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 153 | void CopySocketInformationToPacketInfo(size_t packet_size_bytes, |
| 154 | const AsyncPacketSocket& socket_from, |
Qingsi Wang | 4ea53b3 | 2018-04-16 18:22:31 -0700 | [diff] [blame] | 155 | bool is_connectionless, |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 156 | rtc::PacketInfo* info); |
| 157 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 158 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 159 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 160 | #endif // RTC_BASE_ASYNCPACKETSOCKET_H_ |