blob: d9e1bff66b542567e7db539d99ddc4a18c32258d [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004--2005, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
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.
13 * 3. The name of the author may not be used to endorse or promote products
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
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
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,
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
25 * 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
48 int rtp_sendtime_extension_id; // extension header id present in packet.
49 Buffer 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};
53
54// This structure holds meta information for the packet which is about to send
55// over network.
56struct PacketOptions {
57 PacketOptions() : dscp(DSCP_NO_CHANGE) {}
58 DiffServCodePoint dscp;
59 PacketTimeUpdateParams packet_time_params;
60};
61
wu@webrtc.orga9890802013-12-13 00:21:03 +000062// This structure will have the information about when packet is actually
63// received by socket.
64struct PacketTime {
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +000065 PacketTime() : timestamp(-1), not_before(-1) {}
wu@webrtc.orga9890802013-12-13 00:21:03 +000066 PacketTime(int64 timestamp, int64 not_before)
67 : timestamp(timestamp), not_before(not_before) {
68 }
69
70 int64 timestamp; // Receive time after socket delivers the data.
71 int64 not_before; // Earliest possible time the data could have arrived,
72 // indicating the potential error in the |timestamp| value,
73 // in case the system, is busy. For example, the time of
74 // the last select() call.
75 // If unknown, this value will be set to zero.
76};
77
78inline PacketTime CreatePacketTime(int64 not_before) {
79 return PacketTime(TimeMicros(), not_before);
80}
81
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082// Provides the ability to receive packets asynchronously. Sends are not
83// buffered since it is acceptable to drop packets under high load.
84class AsyncPacketSocket : public sigslot::has_slots<> {
85 public:
86 enum State {
87 STATE_CLOSED,
88 STATE_BINDING,
89 STATE_BOUND,
90 STATE_CONNECTING,
91 STATE_CONNECTED
92 };
93
94 AsyncPacketSocket() { }
95 virtual ~AsyncPacketSocket() { }
96
97 // Returns current local address. Address may be set to NULL if the
98 // socket is not bound yet (GetState() returns STATE_BINDING).
99 virtual SocketAddress GetLocalAddress() const = 0;
100
101 // Returns remote address. Returns zeroes if this is not a client TCP socket.
102 virtual SocketAddress GetRemoteAddress() const = 0;
103
104 // Send a packet.
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000105 virtual int Send(const void *pv, size_t cb, DiffServCodePoint dscp) = 0;
106 virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr,
107 DiffServCodePoint) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108
109 // Close the socket.
110 virtual int Close() = 0;
111
112 // Returns current state of the socket.
113 virtual State GetState() const = 0;
114
115 // Get/set options.
116 virtual int GetOption(Socket::Option opt, int* value) = 0;
117 virtual int SetOption(Socket::Option opt, int value) = 0;
118
119 // Get/Set current error.
120 // TODO: Remove SetError().
121 virtual int GetError() const = 0;
122 virtual void SetError(int error) = 0;
123
124 // Emitted each time a packet is read. Used only for UDP and
125 // connected TCP sockets.
wu@webrtc.orga9890802013-12-13 00:21:03 +0000126 sigslot::signal5<AsyncPacketSocket*, const char*, size_t,
127 const SocketAddress&,
128 const PacketTime&> SignalReadPacket;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129
130 // Emitted when the socket is currently able to send.
131 sigslot::signal1<AsyncPacketSocket*> SignalReadyToSend;
132
133 // Emitted after address for the socket is allocated, i.e. binding
134 // is finished. State of the socket is changed from BINDING to BOUND
135 // (for UDP and server TCP sockets) or CONNECTING (for client TCP
136 // sockets).
137 sigslot::signal2<AsyncPacketSocket*, const SocketAddress&> SignalAddressReady;
138
139 // Emitted for client TCP sockets when state is changed from
140 // CONNECTING to CONNECTED.
141 sigslot::signal1<AsyncPacketSocket*> SignalConnect;
142
143 // Emitted for client TCP sockets when state is changed from
144 // CONNECTED to CLOSED.
145 sigslot::signal2<AsyncPacketSocket*, int> SignalClose;
146
147 // Used only for listening TCP sockets.
148 sigslot::signal2<AsyncPacketSocket*, AsyncPacketSocket*> SignalNewConnection;
149
150 private:
151 DISALLOW_EVIL_CONSTRUCTORS(AsyncPacketSocket);
152};
153
154} // namespace talk_base
155
156#endif // TALK_BASE_ASYNCPACKETSOCKET_H_