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" |
| 16 | #include "rtc_base/sigslot.h" |
| 17 | #include "rtc_base/socket.h" |
| 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(); |
| 27 | ~PacketTimeUpdateParams(); |
| 28 | |
| 29 | int rtp_sendtime_extension_id; // extension header id present in packet. |
| 30 | std::vector<char> srtp_auth_key; // Authentication key. |
| 31 | int srtp_auth_tag_len; // Authentication tag length. |
| 32 | int64_t srtp_packet_index; // Required for Rtp Packet authentication. |
| 33 | }; |
| 34 | |
| 35 | // This structure holds meta information for the packet which is about to send |
| 36 | // over network. |
| 37 | struct PacketOptions { |
| 38 | PacketOptions() : dscp(DSCP_NO_CHANGE), packet_id(-1) {} |
| 39 | explicit PacketOptions(DiffServCodePoint dscp) : dscp(dscp), packet_id(-1) {} |
| 40 | |
| 41 | DiffServCodePoint dscp; |
| 42 | int packet_id; // 16 bits, -1 represents "not set". |
| 43 | PacketTimeUpdateParams packet_time_params; |
| 44 | }; |
| 45 | |
| 46 | // This structure will have the information about when packet is actually |
| 47 | // received by socket. |
| 48 | struct PacketTime { |
| 49 | PacketTime() : timestamp(-1), not_before(-1) {} |
| 50 | PacketTime(int64_t timestamp, int64_t not_before) |
| 51 | : timestamp(timestamp), not_before(not_before) {} |
| 52 | |
| 53 | int64_t timestamp; // Receive time after socket delivers the data. |
| 54 | |
| 55 | // Earliest possible time the data could have arrived, indicating the |
| 56 | // potential error in the |timestamp| value, in case the system, is busy. For |
| 57 | // example, the time of the last select() call. |
| 58 | // If unknown, this value will be set to zero. |
| 59 | int64_t not_before; |
| 60 | }; |
| 61 | |
| 62 | inline PacketTime CreatePacketTime(int64_t not_before) { |
| 63 | return PacketTime(TimeMicros(), not_before); |
| 64 | } |
| 65 | |
| 66 | // Provides the ability to receive packets asynchronously. Sends are not |
| 67 | // buffered since it is acceptable to drop packets under high load. |
| 68 | class AsyncPacketSocket : public sigslot::has_slots<> { |
| 69 | public: |
| 70 | enum State { |
| 71 | STATE_CLOSED, |
| 72 | STATE_BINDING, |
| 73 | STATE_BOUND, |
| 74 | STATE_CONNECTING, |
| 75 | STATE_CONNECTED |
| 76 | }; |
| 77 | |
| 78 | AsyncPacketSocket(); |
| 79 | ~AsyncPacketSocket() override; |
| 80 | |
| 81 | // Returns current local address. Address may be set to null if the |
| 82 | // socket is not bound yet (GetState() returns STATE_BINDING). |
| 83 | virtual SocketAddress GetLocalAddress() const = 0; |
| 84 | |
| 85 | // Returns remote address. Returns zeroes if this is not a client TCP socket. |
| 86 | virtual SocketAddress GetRemoteAddress() const = 0; |
| 87 | |
| 88 | // Send a packet. |
| 89 | virtual int Send(const void *pv, size_t cb, const PacketOptions& options) = 0; |
| 90 | virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr, |
| 91 | const PacketOptions& options) = 0; |
| 92 | |
| 93 | // Close the socket. |
| 94 | virtual int Close() = 0; |
| 95 | |
| 96 | // Returns current state of the socket. |
| 97 | virtual State GetState() const = 0; |
| 98 | |
| 99 | // Get/set options. |
| 100 | virtual int GetOption(Socket::Option opt, int* value) = 0; |
| 101 | virtual int SetOption(Socket::Option opt, int value) = 0; |
| 102 | |
| 103 | // Get/Set current error. |
| 104 | // TODO: Remove SetError(). |
| 105 | virtual int GetError() const = 0; |
| 106 | virtual void SetError(int error) = 0; |
| 107 | |
| 108 | // Emitted each time a packet is read. Used only for UDP and |
| 109 | // connected TCP sockets. |
| 110 | sigslot::signal5<AsyncPacketSocket*, const char*, size_t, |
| 111 | const SocketAddress&, |
| 112 | const PacketTime&> SignalReadPacket; |
| 113 | |
| 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 |
| 122 | // (for UDP and server TCP sockets) or CONNECTING (for client TCP |
| 123 | // sockets). |
| 124 | sigslot::signal2<AsyncPacketSocket*, const SocketAddress&> SignalAddressReady; |
| 125 | |
| 126 | // Emitted for client TCP sockets when state is changed from |
| 127 | // CONNECTING to CONNECTED. |
| 128 | sigslot::signal1<AsyncPacketSocket*> SignalConnect; |
| 129 | |
| 130 | // Emitted for client TCP sockets when state is changed from |
| 131 | // CONNECTED to CLOSED. |
| 132 | sigslot::signal2<AsyncPacketSocket*, int> SignalClose; |
| 133 | |
| 134 | // Used only for listening TCP sockets. |
| 135 | sigslot::signal2<AsyncPacketSocket*, AsyncPacketSocket*> SignalNewConnection; |
| 136 | |
| 137 | private: |
| 138 | RTC_DISALLOW_COPY_AND_ASSIGN(AsyncPacketSocket); |
| 139 | }; |
| 140 | |
| 141 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 142 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 143 | #endif // RTC_BASE_ASYNCPACKETSOCKET_H_ |