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 {
25 PacketTimeUpdateParams();
26 ~PacketTimeUpdateParams();
27
28 int rtp_sendtime_extension_id; // extension header id present in packet.
29 std::vector<char> srtp_auth_key; // Authentication key.
30 int srtp_auth_tag_len; // Authentication tag length.
31 int64_t srtp_packet_index; // Required for Rtp Packet authentication.
32 };
33
34 // This structure holds meta information for the packet which is about to send
35 // over network.
36 struct PacketOptions {
PacketOptionsPacketOptions37 PacketOptions() : dscp(DSCP_NO_CHANGE), packet_id(-1) {}
PacketOptionsPacketOptions38 explicit PacketOptions(DiffServCodePoint dscp) : dscp(dscp), packet_id(-1) {}
39
40 DiffServCodePoint dscp;
41 int packet_id; // 16 bits, -1 represents "not set".
42 PacketTimeUpdateParams packet_time_params;
43 };
44
45 // This structure will have the information about when packet is actually
46 // received by socket.
47 struct PacketTime {
PacketTimePacketTime48 PacketTime() : timestamp(-1), not_before(-1) {}
PacketTimePacketTime49 PacketTime(int64_t timestamp, int64_t not_before)
50 : timestamp(timestamp), not_before(not_before) {}
51
52 int64_t timestamp; // Receive time after socket delivers the data.
53
54 // Earliest possible time the data could have arrived, indicating the
55 // potential error in the |timestamp| value, in case the system, is busy. For
56 // example, the time of the last select() call.
57 // If unknown, this value will be set to zero.
58 int64_t not_before;
59 };
60
CreatePacketTime(int64_t not_before)61 inline PacketTime CreatePacketTime(int64_t not_before) {
62 return PacketTime(TimeMicros(), not_before);
63 }
64
65 // Provides the ability to receive packets asynchronously. Sends are not
66 // buffered since it is acceptable to drop packets under high load.
67 class AsyncPacketSocket : public sigslot::has_slots<> {
68 public:
69 enum State {
70 STATE_CLOSED,
71 STATE_BINDING,
72 STATE_BOUND,
73 STATE_CONNECTING,
74 STATE_CONNECTED
75 };
76
77 AsyncPacketSocket();
78 ~AsyncPacketSocket() override;
79
80 // Returns current local address. Address may be set to NULL if the
81 // socket is not bound yet (GetState() returns STATE_BINDING).
82 virtual SocketAddress GetLocalAddress() const = 0;
83
84 // Returns remote address. Returns zeroes if this is not a client TCP socket.
85 virtual SocketAddress GetRemoteAddress() const = 0;
86
87 // Send a packet.
88 virtual int Send(const void *pv, size_t cb, const PacketOptions& options) = 0;
89 virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr,
90 const PacketOptions& options) = 0;
91
92 // Close the socket.
93 virtual int Close() = 0;
94
95 // Returns current state of the socket.
96 virtual State GetState() const = 0;
97
98 // Get/set options.
99 virtual int GetOption(Socket::Option opt, int* value) = 0;
100 virtual int SetOption(Socket::Option opt, int value) = 0;
101
102 // Get/Set current error.
103 // TODO: Remove SetError().
104 virtual int GetError() const = 0;
105 virtual void SetError(int error) = 0;
106
107 // Emitted each time a packet is read. Used only for UDP and
108 // connected TCP sockets.
109 sigslot::signal5<AsyncPacketSocket*, const char*, size_t,
110 const SocketAddress&,
111 const PacketTime&> SignalReadPacket;
112
113 // Emitted each time a packet is sent.
114 sigslot::signal2<AsyncPacketSocket*, const SentPacket&> SignalSentPacket;
115
116 // Emitted when the socket is currently able to send.
117 sigslot::signal1<AsyncPacketSocket*> SignalReadyToSend;
118
119 // Emitted after address for the socket is allocated, i.e. binding
120 // is finished. State of the socket is changed from BINDING to BOUND
121 // (for UDP and server TCP sockets) or CONNECTING (for client TCP
122 // sockets).
123 sigslot::signal2<AsyncPacketSocket*, const SocketAddress&> SignalAddressReady;
124
125 // Emitted for client TCP sockets when state is changed from
126 // CONNECTING to CONNECTED.
127 sigslot::signal1<AsyncPacketSocket*> SignalConnect;
128
129 // Emitted for client TCP sockets when state is changed from
130 // CONNECTED to CLOSED.
131 sigslot::signal2<AsyncPacketSocket*, int> SignalClose;
132
133 // Used only for listening TCP sockets.
134 sigslot::signal2<AsyncPacketSocket*, AsyncPacketSocket*> SignalNewConnection;
135
136 private:
137 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncPacketSocket);
138 };
139
140 } // namespace rtc
141
142 #endif // WEBRTC_BASE_ASYNCPACKETSOCKET_H_
143