3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #ifndef TALK_BASE_ASYNCPACKETSOCKET_H_
29 #define TALK_BASE_ASYNCPACKETSOCKET_H_
31 #include "talk/base/buffer.h"
32 #include "talk/base/dscp.h"
33 #include "talk/base/sigslot.h"
34 #include "talk/base/socket.h"
35 #include "talk/base/timeutils.h"
39 // 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.
42 struct PacketTimeUpdateParams {
43 PacketTimeUpdateParams()
44 : rtp_sendtime_extension_id(-1), srtp_auth_tag_len(-1),
45 srtp_packet_index(-1) {
48 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
56 // This structure holds meta information for the packet which is about to send
58 struct PacketOptions {
59 PacketOptions() : dscp(DSCP_NO_CHANGE) {}
60 explicit PacketOptions(DiffServCodePoint dscp) : dscp(dscp) {}
62 DiffServCodePoint dscp;
63 PacketTimeUpdateParams packet_time_params;
66 // This structure will have the information about when packet is actually
67 // received by socket.
69 PacketTime() : timestamp(-1), not_before(-1) {}
70 PacketTime(int64 timestamp, int64 not_before)
71 : timestamp(timestamp), not_before(not_before) {
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.
82 inline PacketTime CreatePacketTime(int64 not_before) {
83 return PacketTime(TimeMicros(), not_before);
86 // Provides the ability to receive packets asynchronously. Sends are not
87 // buffered since it is acceptable to drop packets under high load.
88 class AsyncPacketSocket : public sigslot::has_slots<> {
98 AsyncPacketSocket() { }
99 virtual ~AsyncPacketSocket() { }
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;
105 // Returns remote address. Returns zeroes if this is not a client TCP socket.
106 virtual SocketAddress GetRemoteAddress() const = 0;
109 virtual int Send(const void *pv, size_t cb, const PacketOptions& options) = 0;
110 virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr,
111 const PacketOptions& options) = 0;
114 virtual int Close() = 0;
116 // Returns current state of the socket.
117 virtual State GetState() const = 0;
120 virtual int GetOption(Socket::Option opt, int* value) = 0;
121 virtual int SetOption(Socket::Option opt, int value) = 0;
123 // Get/Set current error.
124 // TODO: Remove SetError().
125 virtual int GetError() const = 0;
126 virtual void SetError(int error) = 0;
128 // Emitted each time a packet is read. Used only for UDP and
129 // connected TCP sockets.
130 sigslot::signal5<AsyncPacketSocket*, const char*, size_t,
131 const SocketAddress&,
132 const PacketTime&> SignalReadPacket;
134 // Emitted when the socket is currently able to send.
135 sigslot::signal1<AsyncPacketSocket*> SignalReadyToSend;
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
141 sigslot::signal2<AsyncPacketSocket*, const SocketAddress&> SignalAddressReady;
143 // Emitted for client TCP sockets when state is changed from
144 // CONNECTING to CONNECTED.
145 sigslot::signal1<AsyncPacketSocket*> SignalConnect;
147 // Emitted for client TCP sockets when state is changed from
148 // CONNECTED to CLOSED.
149 sigslot::signal2<AsyncPacketSocket*, int> SignalClose;
151 // Used only for listening TCP sockets.
152 sigslot::signal2<AsyncPacketSocket*, AsyncPacketSocket*> SignalNewConnection;
155 DISALLOW_EVIL_CONSTRUCTORS(AsyncPacketSocket);
158 } // namespace talk_base
160 #endif // TALK_BASE_ASYNCPACKETSOCKET_H_