blob: 50c07e205645b8e8bfba42b3f79646dda1a5d268 [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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_ASYNC_PACKET_SOCKET_H_
12#define RTC_BASE_ASYNC_PACKET_SOCKET_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Steve Anton10542f22019-01-11 09:11:00 -080014#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#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"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020018#include "rtc_base/system/rtc_export.h"
Artem Titove41c4332018-07-25 15:04:28 +020019#include "rtc_base/third_party/sigslot/sigslot.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/time_utils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020022namespace rtc {
23
24// This structure holds the info needed to update the packet send time header
25// extension, including the information needed to update the authentication tag
26// after changing the value.
27struct PacketTimeUpdateParams {
28 PacketTimeUpdateParams();
Qingsi Wang6e641e62018-04-11 20:14:17 -070029 PacketTimeUpdateParams(const PacketTimeUpdateParams& other);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020030 ~PacketTimeUpdateParams();
31
Qingsi Wang6e641e62018-04-11 20:14:17 -070032 int rtp_sendtime_extension_id = -1; // extension header id present in packet.
Yves Gerey665174f2018-06-19 15:03:05 +020033 std::vector<char> srtp_auth_key; // Authentication key.
34 int srtp_auth_tag_len = -1; // Authentication tag length.
35 int64_t srtp_packet_index = -1; // Required for Rtp Packet authentication.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020036};
37
38// This structure holds meta information for the packet which is about to send
39// over network.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020040struct RTC_EXPORT PacketOptions {
Qingsi Wang6e641e62018-04-11 20:14:17 -070041 PacketOptions();
42 explicit PacketOptions(DiffServCodePoint dscp);
43 PacketOptions(const PacketOptions& other);
44 ~PacketOptions();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020045
Qingsi Wang6e641e62018-04-11 20:14:17 -070046 DiffServCodePoint dscp = DSCP_NO_CHANGE;
Bjorn Mellem3a9c46d2018-04-25 13:24:48 -070047 // When used with RTP packets (for example, webrtc::PacketOptions), the value
48 // should be 16 bits. A value of -1 represents "not set".
49 int64_t packet_id = -1;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020050 PacketTimeUpdateParams packet_time_params;
Qingsi Wang6e641e62018-04-11 20:14:17 -070051 // PacketInfo is passed to SentPacket when signaling this packet is sent.
52 PacketInfo info_signaled_after_sent;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020053};
54
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020055// Provides the ability to receive packets asynchronously. Sends are not
56// buffered since it is acceptable to drop packets under high load.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020057class RTC_EXPORT AsyncPacketSocket : public sigslot::has_slots<> {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020058 public:
59 enum State {
60 STATE_CLOSED,
61 STATE_BINDING,
62 STATE_BOUND,
63 STATE_CONNECTING,
64 STATE_CONNECTED
65 };
66
67 AsyncPacketSocket();
68 ~AsyncPacketSocket() override;
69
70 // Returns current local address. Address may be set to null if the
71 // socket is not bound yet (GetState() returns STATE_BINDING).
72 virtual SocketAddress GetLocalAddress() const = 0;
73
74 // Returns remote address. Returns zeroes if this is not a client TCP socket.
75 virtual SocketAddress GetRemoteAddress() const = 0;
76
77 // Send a packet.
Yves Gerey665174f2018-06-19 15:03:05 +020078 virtual int Send(const void* pv, size_t cb, const PacketOptions& options) = 0;
79 virtual int SendTo(const void* pv,
80 size_t cb,
81 const SocketAddress& addr,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020082 const PacketOptions& options) = 0;
83
84 // Close the socket.
85 virtual int Close() = 0;
86
87 // Returns current state of the socket.
88 virtual State GetState() const = 0;
89
90 // Get/set options.
91 virtual int GetOption(Socket::Option opt, int* value) = 0;
92 virtual int SetOption(Socket::Option opt, int value) = 0;
93
94 // Get/Set current error.
95 // TODO: Remove SetError().
96 virtual int GetError() const = 0;
97 virtual void SetError(int error) = 0;
98
99 // Emitted each time a packet is read. Used only for UDP and
100 // connected TCP sockets.
Yves Gerey665174f2018-06-19 15:03:05 +0200101 sigslot::signal5<AsyncPacketSocket*,
102 const char*,
103 size_t,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200104 const SocketAddress&,
Niels Möllere6933812018-11-05 13:01:41 +0100105 // TODO(bugs.webrtc.org/9584): Change to passing the int64_t
106 // timestamp by value.
107 const int64_t&>
Yves Gerey665174f2018-06-19 15:03:05 +0200108 SignalReadPacket;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200109
110 // Emitted each time a packet is sent.
111 sigslot::signal2<AsyncPacketSocket*, const SentPacket&> SignalSentPacket;
112
113 // Emitted when the socket is currently able to send.
114 sigslot::signal1<AsyncPacketSocket*> SignalReadyToSend;
115
116 // Emitted after address for the socket is allocated, i.e. binding
117 // is finished. State of the socket is changed from BINDING to BOUND
118 // (for UDP and server TCP sockets) or CONNECTING (for client TCP
119 // sockets).
120 sigslot::signal2<AsyncPacketSocket*, const SocketAddress&> SignalAddressReady;
121
122 // Emitted for client TCP sockets when state is changed from
123 // CONNECTING to CONNECTED.
124 sigslot::signal1<AsyncPacketSocket*> SignalConnect;
125
126 // Emitted for client TCP sockets when state is changed from
127 // CONNECTED to CLOSED.
128 sigslot::signal2<AsyncPacketSocket*, int> SignalClose;
129
130 // Used only for listening TCP sockets.
131 sigslot::signal2<AsyncPacketSocket*, AsyncPacketSocket*> SignalNewConnection;
132
133 private:
134 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncPacketSocket);
135};
136
Qingsi Wang6e641e62018-04-11 20:14:17 -0700137void CopySocketInformationToPacketInfo(size_t packet_size_bytes,
138 const AsyncPacketSocket& socket_from,
Qingsi Wang4ea53b32018-04-16 18:22:31 -0700139 bool is_connectionless,
Qingsi Wang6e641e62018-04-11 20:14:17 -0700140 rtc::PacketInfo* info);
141
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200142} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000143
Steve Anton10542f22019-01-11 09:11:00 -0800144#endif // RTC_BASE_ASYNC_PACKET_SOCKET_H_