henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2004--2005, Google Inc. |
| 4 | * |
mallinath@webrtc.org | f5e5b3a | 2014-02-14 00:56:12 +0000 | [diff] [blame] | 5 | * Redistribution and use in source and binary forms, with or without |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
mallinath@webrtc.org | f5e5b3a | 2014-02-14 00:56:12 +0000 | [diff] [blame] | 8 | * 1. Redistributions of source code must retain the above copyright notice, |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
mallinath@webrtc.org | f5e5b3a | 2014-02-14 00:56:12 +0000 | [diff] [blame] | 13 | * 3. The name of the author may not be used to endorse or promote products |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
mallinath@webrtc.org | f5e5b3a | 2014-02-14 00:56:12 +0000 | [diff] [blame] | 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
mallinath@webrtc.org | f5e5b3a | 2014-02-14 00:56:12 +0000 | [diff] [blame] | 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
mallinath@webrtc.org | f5e5b3a | 2014-02-14 00:56:12 +0000 | [diff] [blame] | 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #ifndef TALK_BASE_ASYNCPACKETSOCKET_H_ |
| 29 | #define TALK_BASE_ASYNCPACKETSOCKET_H_ |
| 30 | |
mallinath@webrtc.org | 391247d | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 31 | #include "talk/base/dscp.h" |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 32 | #include "talk/base/sigslot.h" |
| 33 | #include "talk/base/socket.h" |
wu@webrtc.org | f89a403 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 34 | #include "talk/base/timeutils.h" |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 35 | |
| 36 | namespace talk_base { |
| 37 | |
wu@webrtc.org | 8a77f5b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 38 | // This structure holds the info needed to update the packet send time header |
| 39 | // extension, including the information needed to update the authentication tag |
| 40 | // after changing the value. |
| 41 | struct PacketTimeUpdateParams { |
| 42 | PacketTimeUpdateParams() |
| 43 | : rtp_sendtime_extension_id(-1), srtp_auth_tag_len(-1), |
| 44 | srtp_packet_index(-1) { |
| 45 | } |
| 46 | |
mallinath@webrtc.org | 5a1feae | 2014-02-17 18:49:41 +0000 | [diff] [blame] | 47 | int rtp_sendtime_extension_id; // extension header id present in packet. |
| 48 | std::vector<char> srtp_auth_key; // Authentication key. |
| 49 | int srtp_auth_tag_len; // Authentication tag length. |
| 50 | int64 srtp_packet_index; // Required for Rtp Packet authentication. |
wu@webrtc.org | 8a77f5b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | // This structure holds meta information for the packet which is about to send |
| 54 | // over network. |
| 55 | struct PacketOptions { |
| 56 | PacketOptions() : dscp(DSCP_NO_CHANGE) {} |
mallinath@webrtc.org | f5e5b3a | 2014-02-14 00:56:12 +0000 | [diff] [blame] | 57 | explicit PacketOptions(DiffServCodePoint dscp) : dscp(dscp) {} |
| 58 | |
wu@webrtc.org | 8a77f5b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 59 | DiffServCodePoint dscp; |
| 60 | PacketTimeUpdateParams packet_time_params; |
| 61 | }; |
| 62 | |
wu@webrtc.org | f89a403 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 63 | // This structure will have the information about when packet is actually |
| 64 | // received by socket. |
| 65 | struct PacketTime { |
mallinath@webrtc.org | 1114b94 | 2013-12-13 12:29:34 +0000 | [diff] [blame] | 66 | PacketTime() : timestamp(-1), not_before(-1) {} |
wu@webrtc.org | f89a403 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 67 | PacketTime(int64 timestamp, int64 not_before) |
| 68 | : timestamp(timestamp), not_before(not_before) { |
| 69 | } |
| 70 | |
| 71 | int64 timestamp; // Receive time after socket delivers the data. |
| 72 | int64 not_before; // Earliest possible time the data could have arrived, |
| 73 | // indicating the potential error in the |timestamp| value, |
| 74 | // in case the system, is busy. For example, the time of |
| 75 | // the last select() call. |
| 76 | // If unknown, this value will be set to zero. |
| 77 | }; |
| 78 | |
| 79 | inline PacketTime CreatePacketTime(int64 not_before) { |
| 80 | return PacketTime(TimeMicros(), not_before); |
| 81 | } |
| 82 | |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 83 | // Provides the ability to receive packets asynchronously. Sends are not |
| 84 | // buffered since it is acceptable to drop packets under high load. |
| 85 | class AsyncPacketSocket : public sigslot::has_slots<> { |
| 86 | public: |
| 87 | enum State { |
| 88 | STATE_CLOSED, |
| 89 | STATE_BINDING, |
| 90 | STATE_BOUND, |
| 91 | STATE_CONNECTING, |
| 92 | STATE_CONNECTED |
| 93 | }; |
| 94 | |
| 95 | AsyncPacketSocket() { } |
| 96 | virtual ~AsyncPacketSocket() { } |
| 97 | |
| 98 | // Returns current local address. Address may be set to NULL if the |
| 99 | // socket is not bound yet (GetState() returns STATE_BINDING). |
| 100 | virtual SocketAddress GetLocalAddress() const = 0; |
| 101 | |
| 102 | // Returns remote address. Returns zeroes if this is not a client TCP socket. |
| 103 | virtual SocketAddress GetRemoteAddress() const = 0; |
| 104 | |
| 105 | // Send a packet. |
mallinath@webrtc.org | f5e5b3a | 2014-02-14 00:56:12 +0000 | [diff] [blame] | 106 | virtual int Send(const void *pv, size_t cb, const PacketOptions& options) = 0; |
mallinath@webrtc.org | 391247d | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 107 | virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr, |
mallinath@webrtc.org | f5e5b3a | 2014-02-14 00:56:12 +0000 | [diff] [blame] | 108 | const PacketOptions& options) = 0; |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 109 | |
| 110 | // Close the socket. |
| 111 | virtual int Close() = 0; |
| 112 | |
| 113 | // Returns current state of the socket. |
| 114 | virtual State GetState() const = 0; |
| 115 | |
| 116 | // Get/set options. |
| 117 | virtual int GetOption(Socket::Option opt, int* value) = 0; |
| 118 | virtual int SetOption(Socket::Option opt, int value) = 0; |
| 119 | |
| 120 | // Get/Set current error. |
| 121 | // TODO: Remove SetError(). |
| 122 | virtual int GetError() const = 0; |
| 123 | virtual void SetError(int error) = 0; |
| 124 | |
| 125 | // Emitted each time a packet is read. Used only for UDP and |
| 126 | // connected TCP sockets. |
wu@webrtc.org | f89a403 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 127 | sigslot::signal5<AsyncPacketSocket*, const char*, size_t, |
| 128 | const SocketAddress&, |
| 129 | const PacketTime&> SignalReadPacket; |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 130 | |
| 131 | // Emitted when the socket is currently able to send. |
| 132 | sigslot::signal1<AsyncPacketSocket*> SignalReadyToSend; |
| 133 | |
| 134 | // Emitted after address for the socket is allocated, i.e. binding |
| 135 | // is finished. State of the socket is changed from BINDING to BOUND |
| 136 | // (for UDP and server TCP sockets) or CONNECTING (for client TCP |
| 137 | // sockets). |
| 138 | sigslot::signal2<AsyncPacketSocket*, const SocketAddress&> SignalAddressReady; |
| 139 | |
| 140 | // Emitted for client TCP sockets when state is changed from |
| 141 | // CONNECTING to CONNECTED. |
| 142 | sigslot::signal1<AsyncPacketSocket*> SignalConnect; |
| 143 | |
| 144 | // Emitted for client TCP sockets when state is changed from |
| 145 | // CONNECTED to CLOSED. |
| 146 | sigslot::signal2<AsyncPacketSocket*, int> SignalClose; |
| 147 | |
| 148 | // Used only for listening TCP sockets. |
| 149 | sigslot::signal2<AsyncPacketSocket*, AsyncPacketSocket*> SignalNewConnection; |
| 150 | |
| 151 | private: |
| 152 | DISALLOW_EVIL_CONSTRUCTORS(AsyncPacketSocket); |
| 153 | }; |
| 154 | |
| 155 | } // namespace talk_base |
| 156 | |
| 157 | #endif // TALK_BASE_ASYNCPACKETSOCKET_H_ |