blob: 949ec67c839d76c4c31e774182b826fdf6394aa3 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
19namespace 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.
24struct PacketTimeUpdateParams {
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000025 PacketTimeUpdateParams();
26 ~PacketTimeUpdateParams();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000027
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.
Peter Boström0c4e06b2015-10-07 12:23:21 +020031 int64_t srtp_packet_index; // Required for Rtp Packet authentication.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000032};
33
34// This structure holds meta information for the packet which is about to send
35// over network.
36struct PacketOptions {
stefanc1aeaf02015-10-15 07:26:07 -070037 PacketOptions() : dscp(DSCP_NO_CHANGE), packet_id(-1) {}
38 explicit PacketOptions(DiffServCodePoint dscp) : dscp(dscp), packet_id(-1) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000039
40 DiffServCodePoint dscp;
stefanc1aeaf02015-10-15 07:26:07 -070041 int packet_id; // 16 bits, -1 represents "not set".
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000042 PacketTimeUpdateParams packet_time_params;
43};
44
45// This structure will have the information about when packet is actually
46// received by socket.
47struct PacketTime {
48 PacketTime() : timestamp(-1), not_before(-1) {}
Peter Boström0c4e06b2015-10-07 12:23:21 +020049 PacketTime(int64_t timestamp, int64_t not_before)
50 : timestamp(timestamp), not_before(not_before) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000051
Peter Boström0c4e06b2015-10-07 12:23:21 +020052 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;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059};
60
Peter Boström0c4e06b2015-10-07 12:23:21 +020061inline PacketTime CreatePacketTime(int64_t not_before) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000062 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.
67class 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
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000077 AsyncPacketSocket();
78 ~AsyncPacketSocket() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000079
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
stefanc1aeaf02015-10-15 07:26:07 -0700113 // Emitted each time a packet is sent.
114 sigslot::signal2<AsyncPacketSocket*, const SentPacket&> SignalSentPacket;
115
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000116 // 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:
henrikg3c089d72015-09-16 05:37:44 -0700137 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncPacketSocket);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000138};
139
140} // namespace rtc
141
142#endif // WEBRTC_BASE_ASYNCPACKETSOCKET_H_