blob: 091f1d0fe715fae89d12ab5b5eeb445cbf02b2ae [file] [log] [blame]
henrike@webrtc.org0e118e72013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004--2005, Google Inc.
4 *
mallinath@webrtc.orgf5e5b3a2014-02-14 00:56:12 +00005 * Redistribution and use in source and binary forms, with or without
henrike@webrtc.org0e118e72013-07-10 00:45:36 +00006 * modification, are permitted provided that the following conditions are met:
7 *
mallinath@webrtc.orgf5e5b3a2014-02-14 00:56:12 +00008 * 1. Redistributions of source code must retain the above copyright notice,
henrike@webrtc.org0e118e72013-07-10 00:45:36 +00009 * 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.orgf5e5b3a2014-02-14 00:56:12 +000013 * 3. The name of the author may not be used to endorse or promote products
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000014 * 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.orgf5e5b3a2014-02-14 00:56:12 +000017 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000018 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
mallinath@webrtc.orgf5e5b3a2014-02-14 00:56:12 +000019 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000020 * 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.orgf5e5b3a2014-02-14 00:56:12 +000023 * 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.org0e118e72013-07-10 00:45:36 +000025 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef TALK_BASE_ASYNCPACKETSOCKET_H_
29#define TALK_BASE_ASYNCPACKETSOCKET_H_
30
mallinath@webrtc.org391247d2013-09-23 20:34:45 +000031#include "talk/base/dscp.h"
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000032#include "talk/base/sigslot.h"
33#include "talk/base/socket.h"
wu@webrtc.orgf89a4032013-12-13 00:21:03 +000034#include "talk/base/timeutils.h"
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000035
36namespace talk_base {
37
wu@webrtc.org8a77f5b2014-02-13 23:18:49 +000038// 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.
41struct PacketTimeUpdateParams {
42 PacketTimeUpdateParams()
43 : rtp_sendtime_extension_id(-1), srtp_auth_tag_len(-1),
44 srtp_packet_index(-1) {
45 }
46
mallinath@webrtc.org5a1feae2014-02-17 18:49:41 +000047 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.org8a77f5b2014-02-13 23:18:49 +000051};
52
53// This structure holds meta information for the packet which is about to send
54// over network.
55struct PacketOptions {
56 PacketOptions() : dscp(DSCP_NO_CHANGE) {}
mallinath@webrtc.orgf5e5b3a2014-02-14 00:56:12 +000057 explicit PacketOptions(DiffServCodePoint dscp) : dscp(dscp) {}
58
wu@webrtc.org8a77f5b2014-02-13 23:18:49 +000059 DiffServCodePoint dscp;
60 PacketTimeUpdateParams packet_time_params;
61};
62
wu@webrtc.orgf89a4032013-12-13 00:21:03 +000063// This structure will have the information about when packet is actually
64// received by socket.
65struct PacketTime {
mallinath@webrtc.org1114b942013-12-13 12:29:34 +000066 PacketTime() : timestamp(-1), not_before(-1) {}
wu@webrtc.orgf89a4032013-12-13 00:21:03 +000067 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
79inline PacketTime CreatePacketTime(int64 not_before) {
80 return PacketTime(TimeMicros(), not_before);
81}
82
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000083// Provides the ability to receive packets asynchronously. Sends are not
84// buffered since it is acceptable to drop packets under high load.
85class 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.orgf5e5b3a2014-02-14 00:56:12 +0000106 virtual int Send(const void *pv, size_t cb, const PacketOptions& options) = 0;
mallinath@webrtc.org391247d2013-09-23 20:34:45 +0000107 virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr,
mallinath@webrtc.orgf5e5b3a2014-02-14 00:56:12 +0000108 const PacketOptions& options) = 0;
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000109
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.orgf89a4032013-12-13 00:21:03 +0000127 sigslot::signal5<AsyncPacketSocket*, const char*, size_t,
128 const SocketAddress&,
129 const PacketTime&> SignalReadPacket;
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000130
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_