blob: 0e31c2bac14a7a99e822023de7dd329055f70680 [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"
Yves Gerey3e707812018-11-28 16:47:49 +010016#include "rtc_base/network/sent_packet.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/socket.h"
Artem Titove41c4332018-07-25 15:04:28 +020018#include "rtc_base/third_party/sigslot/sigslot.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/timeutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000020
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020021namespace rtc {
22
23// This structure holds the info needed to update the packet send time header
24// extension, including the information needed to update the authentication tag
25// after changing the value.
26struct PacketTimeUpdateParams {
27 PacketTimeUpdateParams();
Qingsi Wang6e641e62018-04-11 20:14:17 -070028 PacketTimeUpdateParams(const PacketTimeUpdateParams& other);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020029 ~PacketTimeUpdateParams();
30
Qingsi Wang6e641e62018-04-11 20:14:17 -070031 int rtp_sendtime_extension_id = -1; // extension header id present in packet.
Yves Gerey665174f2018-06-19 15:03:05 +020032 std::vector<char> srtp_auth_key; // Authentication key.
33 int srtp_auth_tag_len = -1; // Authentication tag length.
34 int64_t srtp_packet_index = -1; // Required for Rtp Packet authentication.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020035};
36
37// This structure holds meta information for the packet which is about to send
38// over network.
39struct PacketOptions {
Qingsi Wang6e641e62018-04-11 20:14:17 -070040 PacketOptions();
41 explicit PacketOptions(DiffServCodePoint dscp);
42 PacketOptions(const PacketOptions& other);
43 ~PacketOptions();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020044
Qingsi Wang6e641e62018-04-11 20:14:17 -070045 DiffServCodePoint dscp = DSCP_NO_CHANGE;
Bjorn Mellem3a9c46d2018-04-25 13:24:48 -070046 // When used with RTP packets (for example, webrtc::PacketOptions), the value
47 // should be 16 bits. A value of -1 represents "not set".
48 int64_t packet_id = -1;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020049 PacketTimeUpdateParams packet_time_params;
Qingsi Wang6e641e62018-04-11 20:14:17 -070050 // PacketInfo is passed to SentPacket when signaling this packet is sent.
51 PacketInfo info_signaled_after_sent;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020052};
53
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020054// Provides the ability to receive packets asynchronously. Sends are not
55// buffered since it is acceptable to drop packets under high load.
56class AsyncPacketSocket : public sigslot::has_slots<> {
57 public:
58 enum State {
59 STATE_CLOSED,
60 STATE_BINDING,
61 STATE_BOUND,
62 STATE_CONNECTING,
63 STATE_CONNECTED
64 };
65
66 AsyncPacketSocket();
67 ~AsyncPacketSocket() override;
68
69 // Returns current local address. Address may be set to null if the
70 // socket is not bound yet (GetState() returns STATE_BINDING).
71 virtual SocketAddress GetLocalAddress() const = 0;
72
73 // Returns remote address. Returns zeroes if this is not a client TCP socket.
74 virtual SocketAddress GetRemoteAddress() const = 0;
75
76 // Send a packet.
Yves Gerey665174f2018-06-19 15:03:05 +020077 virtual int Send(const void* pv, size_t cb, const PacketOptions& options) = 0;
78 virtual int SendTo(const void* pv,
79 size_t cb,
80 const SocketAddress& addr,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020081 const PacketOptions& options) = 0;
82
83 // Close the socket.
84 virtual int Close() = 0;
85
86 // Returns current state of the socket.
87 virtual State GetState() const = 0;
88
89 // Get/set options.
90 virtual int GetOption(Socket::Option opt, int* value) = 0;
91 virtual int SetOption(Socket::Option opt, int value) = 0;
92
93 // Get/Set current error.
94 // TODO: Remove SetError().
95 virtual int GetError() const = 0;
96 virtual void SetError(int error) = 0;
97
98 // Emitted each time a packet is read. Used only for UDP and
99 // connected TCP sockets.
Yves Gerey665174f2018-06-19 15:03:05 +0200100 sigslot::signal5<AsyncPacketSocket*,
101 const char*,
102 size_t,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200103 const SocketAddress&,
Niels Möllere6933812018-11-05 13:01:41 +0100104 // TODO(bugs.webrtc.org/9584): Change to passing the int64_t
105 // timestamp by value.
106 const int64_t&>
Yves Gerey665174f2018-06-19 15:03:05 +0200107 SignalReadPacket;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200108
109 // Emitted each time a packet is sent.
110 sigslot::signal2<AsyncPacketSocket*, const SentPacket&> SignalSentPacket;
111
112 // Emitted when the socket is currently able to send.
113 sigslot::signal1<AsyncPacketSocket*> SignalReadyToSend;
114
115 // Emitted after address for the socket is allocated, i.e. binding
116 // is finished. State of the socket is changed from BINDING to BOUND
117 // (for UDP and server TCP sockets) or CONNECTING (for client TCP
118 // sockets).
119 sigslot::signal2<AsyncPacketSocket*, const SocketAddress&> SignalAddressReady;
120
121 // Emitted for client TCP sockets when state is changed from
122 // CONNECTING to CONNECTED.
123 sigslot::signal1<AsyncPacketSocket*> SignalConnect;
124
125 // Emitted for client TCP sockets when state is changed from
126 // CONNECTED to CLOSED.
127 sigslot::signal2<AsyncPacketSocket*, int> SignalClose;
128
129 // Used only for listening TCP sockets.
130 sigslot::signal2<AsyncPacketSocket*, AsyncPacketSocket*> SignalNewConnection;
131
132 private:
133 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncPacketSocket);
134};
135
Qingsi Wang6e641e62018-04-11 20:14:17 -0700136void CopySocketInformationToPacketInfo(size_t packet_size_bytes,
137 const AsyncPacketSocket& socket_from,
Qingsi Wang4ea53b32018-04-16 18:22:31 -0700138 bool is_connectionless,
Qingsi Wang6e641e62018-04-11 20:14:17 -0700139 rtc::PacketInfo* info);
140
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200141} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000142
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200143#endif // RTC_BASE_ASYNCPACKETSOCKET_H_