blob: 2ffacfe6db3febe76a8fe43876a10177eb855b7b [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_ASYNCPACKETSOCKET_H_
12#define RTC_BASE_ASYNCPACKETSOCKET_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/constructormagic.h"
15#include "rtc_base/dscp.h"
16#include "rtc_base/sigslot.h"
17#include "rtc_base/socket.h"
18#include "rtc_base/timeutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020020namespace rtc {
21
22// This structure holds the info needed to update the packet send time header
23// extension, including the information needed to update the authentication tag
24// after changing the value.
25struct PacketTimeUpdateParams {
26 PacketTimeUpdateParams();
Qingsi Wang6e641e62018-04-11 20:14:17 -070027 PacketTimeUpdateParams(const PacketTimeUpdateParams& other);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020028 ~PacketTimeUpdateParams();
29
Qingsi Wang6e641e62018-04-11 20:14:17 -070030 int rtp_sendtime_extension_id = -1; // extension header id present in packet.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020031 std::vector<char> srtp_auth_key; // Authentication key.
Qingsi Wang6e641e62018-04-11 20:14:17 -070032 int srtp_auth_tag_len = -1; // Authentication tag length.
33 int64_t srtp_packet_index = -1; // Required for Rtp Packet authentication.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020034};
35
36// This structure holds meta information for the packet which is about to send
37// over network.
38struct PacketOptions {
Qingsi Wang6e641e62018-04-11 20:14:17 -070039 PacketOptions();
40 explicit PacketOptions(DiffServCodePoint dscp);
41 PacketOptions(const PacketOptions& other);
42 ~PacketOptions();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020043
Qingsi Wang6e641e62018-04-11 20:14:17 -070044 DiffServCodePoint dscp = DSCP_NO_CHANGE;
45 int packet_id = -1; // 16 bits, -1 represents "not set".
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020046 PacketTimeUpdateParams packet_time_params;
Qingsi Wang6e641e62018-04-11 20:14:17 -070047 // PacketInfo is passed to SentPacket when signaling this packet is sent.
48 PacketInfo info_signaled_after_sent;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020049};
50
51// This structure will have the information about when packet is actually
52// received by socket.
53struct PacketTime {
54 PacketTime() : timestamp(-1), not_before(-1) {}
55 PacketTime(int64_t timestamp, int64_t not_before)
56 : timestamp(timestamp), not_before(not_before) {}
57
58 int64_t timestamp; // Receive time after socket delivers the data.
59
60 // Earliest possible time the data could have arrived, indicating the
61 // potential error in the |timestamp| value, in case the system, is busy. For
62 // example, the time of the last select() call.
63 // If unknown, this value will be set to zero.
64 int64_t not_before;
65};
66
67inline PacketTime CreatePacketTime(int64_t not_before) {
68 return PacketTime(TimeMicros(), not_before);
69}
70
71// Provides the ability to receive packets asynchronously. Sends are not
72// buffered since it is acceptable to drop packets under high load.
73class AsyncPacketSocket : public sigslot::has_slots<> {
74 public:
75 enum State {
76 STATE_CLOSED,
77 STATE_BINDING,
78 STATE_BOUND,
79 STATE_CONNECTING,
80 STATE_CONNECTED
81 };
82
83 AsyncPacketSocket();
84 ~AsyncPacketSocket() override;
85
86 // Returns current local address. Address may be set to null if the
87 // socket is not bound yet (GetState() returns STATE_BINDING).
88 virtual SocketAddress GetLocalAddress() const = 0;
89
90 // Returns remote address. Returns zeroes if this is not a client TCP socket.
91 virtual SocketAddress GetRemoteAddress() const = 0;
92
93 // Send a packet.
94 virtual int Send(const void *pv, size_t cb, const PacketOptions& options) = 0;
95 virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr,
96 const PacketOptions& options) = 0;
97
98 // Close the socket.
99 virtual int Close() = 0;
100
101 // Returns current state of the socket.
102 virtual State GetState() const = 0;
103
104 // Get/set options.
105 virtual int GetOption(Socket::Option opt, int* value) = 0;
106 virtual int SetOption(Socket::Option opt, int value) = 0;
107
108 // Get/Set current error.
109 // TODO: Remove SetError().
110 virtual int GetError() const = 0;
111 virtual void SetError(int error) = 0;
112
113 // Emitted each time a packet is read. Used only for UDP and
114 // connected TCP sockets.
115 sigslot::signal5<AsyncPacketSocket*, const char*, size_t,
116 const SocketAddress&,
117 const PacketTime&> SignalReadPacket;
118
119 // Emitted each time a packet is sent.
120 sigslot::signal2<AsyncPacketSocket*, const SentPacket&> SignalSentPacket;
121
122 // Emitted when the socket is currently able to send.
123 sigslot::signal1<AsyncPacketSocket*> SignalReadyToSend;
124
125 // Emitted after address for the socket is allocated, i.e. binding
126 // is finished. State of the socket is changed from BINDING to BOUND
127 // (for UDP and server TCP sockets) or CONNECTING (for client TCP
128 // sockets).
129 sigslot::signal2<AsyncPacketSocket*, const SocketAddress&> SignalAddressReady;
130
131 // Emitted for client TCP sockets when state is changed from
132 // CONNECTING to CONNECTED.
133 sigslot::signal1<AsyncPacketSocket*> SignalConnect;
134
135 // Emitted for client TCP sockets when state is changed from
136 // CONNECTED to CLOSED.
137 sigslot::signal2<AsyncPacketSocket*, int> SignalClose;
138
139 // Used only for listening TCP sockets.
140 sigslot::signal2<AsyncPacketSocket*, AsyncPacketSocket*> SignalNewConnection;
141
142 private:
143 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncPacketSocket);
144};
145
Qingsi Wang6e641e62018-04-11 20:14:17 -0700146void CopySocketInformationToPacketInfo(size_t packet_size_bytes,
147 const AsyncPacketSocket& socket_from,
Qingsi Wang4ea53b32018-04-16 18:22:31 -0700148 bool is_connectionless,
Qingsi Wang6e641e62018-04-11 20:14:17 -0700149 rtc::PacketInfo* info);
150
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200151} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000152
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200153#endif // RTC_BASE_ASYNCPACKETSOCKET_H_