blob: b1d5a4a15f27ccb3e925ef885eb674ee2263eceb [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004--2005, Google Inc.
4 *
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00005 * Redistribution and use in source and binary forms, with or without
henrike@webrtc.org28e20752013-07-10 00:45:36 +00006 * modification, are permitted provided that the following conditions are met:
7 *
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00008 * 1. Redistributions of source code must retain the above copyright notice,
henrike@webrtc.org28e20752013-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.org385857d2014-02-14 00:56:12 +000013 * 3. The name of the author may not be used to endorse or promote products
henrike@webrtc.org28e20752013-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.org385857d2014-02-14 00:56:12 +000017 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
mallinath@webrtc.org385857d2014-02-14 00:56:12 +000019 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
henrike@webrtc.org28e20752013-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.org385857d2014-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.org28e20752013-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
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000031#include "talk/base/buffer.h"
mallinath@webrtc.org1112c302013-09-23 20:34:45 +000032#include "talk/base/dscp.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033#include "talk/base/sigslot.h"
34#include "talk/base/socket.h"
wu@webrtc.orga9890802013-12-13 00:21:03 +000035#include "talk/base/timeutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036
37namespace talk_base {
38
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000039// This structure holds the info needed to update the packet send time header
40// extension, including the information needed to update the authentication tag
41// after changing the value.
42struct PacketTimeUpdateParams {
43 PacketTimeUpdateParams()
44 : rtp_sendtime_extension_id(-1), srtp_auth_tag_len(-1),
45 srtp_packet_index(-1) {
46 }
47
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +000048 int rtp_sendtime_extension_id; // extension header id present in packet.
49 std::vector<char> srtp_auth_key; // Authentication key.
50 int srtp_auth_tag_len; // Authentication tag length.
51 int64 srtp_packet_index; // Required for Rtp Packet authentication.
52 int payload_len; // Raw payload length, before any wrapping
53 // like TURN/GTURN.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000054};
55
56// This structure holds meta information for the packet which is about to send
57// over network.
58struct PacketOptions {
59 PacketOptions() : dscp(DSCP_NO_CHANGE) {}
mallinath@webrtc.org385857d2014-02-14 00:56:12 +000060 explicit PacketOptions(DiffServCodePoint dscp) : dscp(dscp) {}
61
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000062 DiffServCodePoint dscp;
63 PacketTimeUpdateParams packet_time_params;
64};
65
wu@webrtc.orga9890802013-12-13 00:21:03 +000066// This structure will have the information about when packet is actually
67// received by socket.
68struct PacketTime {
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +000069 PacketTime() : timestamp(-1), not_before(-1) {}
wu@webrtc.orga9890802013-12-13 00:21:03 +000070 PacketTime(int64 timestamp, int64 not_before)
71 : timestamp(timestamp), not_before(not_before) {
72 }
73
74 int64 timestamp; // Receive time after socket delivers the data.
75 int64 not_before; // Earliest possible time the data could have arrived,
76 // indicating the potential error in the |timestamp| value,
77 // in case the system, is busy. For example, the time of
78 // the last select() call.
79 // If unknown, this value will be set to zero.
80};
81
82inline PacketTime CreatePacketTime(int64 not_before) {
83 return PacketTime(TimeMicros(), not_before);
84}
85
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086// Provides the ability to receive packets asynchronously. Sends are not
87// buffered since it is acceptable to drop packets under high load.
88class AsyncPacketSocket : public sigslot::has_slots<> {
89 public:
90 enum State {
91 STATE_CLOSED,
92 STATE_BINDING,
93 STATE_BOUND,
94 STATE_CONNECTING,
95 STATE_CONNECTED
96 };
97
98 AsyncPacketSocket() { }
99 virtual ~AsyncPacketSocket() { }
100
101 // Returns current local address. Address may be set to NULL if the
102 // socket is not bound yet (GetState() returns STATE_BINDING).
103 virtual SocketAddress GetLocalAddress() const = 0;
104
105 // Returns remote address. Returns zeroes if this is not a client TCP socket.
106 virtual SocketAddress GetRemoteAddress() const = 0;
107
108 // Send a packet.
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000109 virtual int Send(const void *pv, size_t cb, const PacketOptions& options) = 0;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000110 virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr,
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000111 const PacketOptions& options) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112
113 // Close the socket.
114 virtual int Close() = 0;
115
116 // Returns current state of the socket.
117 virtual State GetState() const = 0;
118
119 // Get/set options.
120 virtual int GetOption(Socket::Option opt, int* value) = 0;
121 virtual int SetOption(Socket::Option opt, int value) = 0;
122
123 // Get/Set current error.
124 // TODO: Remove SetError().
125 virtual int GetError() const = 0;
126 virtual void SetError(int error) = 0;
127
128 // Emitted each time a packet is read. Used only for UDP and
129 // connected TCP sockets.
wu@webrtc.orga9890802013-12-13 00:21:03 +0000130 sigslot::signal5<AsyncPacketSocket*, const char*, size_t,
131 const SocketAddress&,
132 const PacketTime&> SignalReadPacket;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133
134 // Emitted when the socket is currently able to send.
135 sigslot::signal1<AsyncPacketSocket*> SignalReadyToSend;
136
137 // Emitted after address for the socket is allocated, i.e. binding
138 // is finished. State of the socket is changed from BINDING to BOUND
139 // (for UDP and server TCP sockets) or CONNECTING (for client TCP
140 // sockets).
141 sigslot::signal2<AsyncPacketSocket*, const SocketAddress&> SignalAddressReady;
142
143 // Emitted for client TCP sockets when state is changed from
144 // CONNECTING to CONNECTED.
145 sigslot::signal1<AsyncPacketSocket*> SignalConnect;
146
147 // Emitted for client TCP sockets when state is changed from
148 // CONNECTED to CLOSED.
149 sigslot::signal2<AsyncPacketSocket*, int> SignalClose;
150
151 // Used only for listening TCP sockets.
152 sigslot::signal2<AsyncPacketSocket*, AsyncPacketSocket*> SignalNewConnection;
153
154 private:
155 DISALLOW_EVIL_CONSTRUCTORS(AsyncPacketSocket);
156};
157
158} // namespace talk_base
159
160#endif // TALK_BASE_ASYNCPACKETSOCKET_H_