1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #ifndef WEBRTC_BASE_ASYNCPACKETSOCKET_H_
12 #define WEBRTC_BASE_ASYNCPACKETSOCKET_H_
13
14 #include "webrtc/base/dscp.h"
15 #include "webrtc/base/sigslot.h"
16 #include "webrtc/base/socket.h"
17 #include "webrtc/base/timeutils.h"
18
19 namespace rtc {
20
21 // This structure holds the info needed to update the packet send time header
22 // extension, including the information needed to update the authentication tag
23 // after changing the value.
24 struct PacketTimeUpdateParams {
PacketTimeUpdateParamsPacketTimeUpdateParams25 PacketTimeUpdateParams()
26 : rtp_sendtime_extension_id(-1), srtp_auth_tag_len(-1),
27 srtp_packet_index(-1) {
28 }
29
30 int rtp_sendtime_extension_id; // extension header id present in packet.
31 std::vector<char> srtp_auth_key; // Authentication key.
32 int srtp_auth_tag_len; // Authentication tag length.
33 int64 srtp_packet_index; // Required for Rtp Packet authentication.
34 };
35
36 // This structure holds meta information for the packet which is about to send
37 // over network.
38 struct PacketOptions {
PacketOptionsPacketOptions39 PacketOptions() : dscp(DSCP_NO_CHANGE) {}
PacketOptionsPacketOptions40 explicit PacketOptions(DiffServCodePoint dscp) : dscp(dscp) {}
41
42 DiffServCodePoint dscp;
43 PacketTimeUpdateParams packet_time_params;
44 };
45
46 // This structure will have the information about when packet is actually
47 // received by socket.
48 struct PacketTime {
PacketTimePacketTime49 PacketTime() : timestamp(-1), not_before(-1) {}
PacketTimePacketTime50 PacketTime(int64 timestamp, int64 not_before)
51 : timestamp(timestamp), not_before(not_before) {
52 }
53
54 int64 timestamp; // Receive time after socket delivers the data.
55 int64 not_before; // Earliest possible time the data could have arrived,
56 // indicating the potential error in the |timestamp| value,
57 // in case the system, is busy. For example, the time of
58 // the last select() call.
59 // If unknown, this value will be set to zero.
60 };
61
CreatePacketTime(int64 not_before)62 inline PacketTime CreatePacketTime(int64 not_before) {
63 return PacketTime(TimeMicros(), not_before);
64 }
65
66 // Provides the ability to receive packets asynchronously. Sends are not
67 // buffered since it is acceptable to drop packets under high load.
68 class AsyncPacketSocket : public sigslot::has_slots<> {
69 public:
70 enum State {
71 STATE_CLOSED,
72 STATE_BINDING,
73 STATE_BOUND,
74 STATE_CONNECTING,
75 STATE_CONNECTED
76 };
77
AsyncPacketSocket()78 AsyncPacketSocket() { }
~AsyncPacketSocket()79 virtual ~AsyncPacketSocket() { }
80
81 // Returns current local address. Address may be set to NULL if the
82 // socket is not bound yet (GetState() returns STATE_BINDING).
83 virtual SocketAddress GetLocalAddress() const = 0;
84
85 // Returns remote address. Returns zeroes if this is not a client TCP socket.
86 virtual SocketAddress GetRemoteAddress() const = 0;
87
88 // Send a packet.
89 virtual int Send(const void *pv, size_t cb, const PacketOptions& options) = 0;
90 virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr,
91 const PacketOptions& options) = 0;
92
93 // Close the socket.
94 virtual int Close() = 0;
95
96 // Returns current state of the socket.
97 virtual State GetState() const = 0;
98
99 // Get/set options.
100 virtual int GetOption(Socket::Option opt, int* value) = 0;
101 virtual int SetOption(Socket::Option opt, int value) = 0;
102
103 // Get/Set current error.
104 // TODO: Remove SetError().
105 virtual int GetError() const = 0;
106 virtual void SetError(int error) = 0;
107
108 // Emitted each time a packet is read. Used only for UDP and
109 // connected TCP sockets.
110 sigslot::signal5<AsyncPacketSocket*, const char*, size_t,
111 const SocketAddress&,
112 const PacketTime&> SignalReadPacket;
113
114 // Emitted when the socket is currently able to send.
115 sigslot::signal1<AsyncPacketSocket*> SignalReadyToSend;
116
117 // Emitted after address for the socket is allocated, i.e. binding
118 // is finished. State of the socket is changed from BINDING to BOUND
119 // (for UDP and server TCP sockets) or CONNECTING (for client TCP
120 // sockets).
121 sigslot::signal2<AsyncPacketSocket*, const SocketAddress&> SignalAddressReady;
122
123 // Emitted for client TCP sockets when state is changed from
124 // CONNECTING to CONNECTED.
125 sigslot::signal1<AsyncPacketSocket*> SignalConnect;
126
127 // Emitted for client TCP sockets when state is changed from
128 // CONNECTED to CLOSED.
129 sigslot::signal2<AsyncPacketSocket*, int> SignalClose;
130
131 // Used only for listening TCP sockets.
132 sigslot::signal2<AsyncPacketSocket*, AsyncPacketSocket*> SignalNewConnection;
133
134 private:
135 DISALLOW_EVIL_CONSTRUCTORS(AsyncPacketSocket);
136 };
137
138 } // namespace rtc
139
140 #endif // WEBRTC_BASE_ASYNCPACKETSOCKET_H_
141