blob: 44d6c674267a6165f2b195f81f2d882f3364138f [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"
Niels Möller15ca5a92018-11-01 14:32:47 +010015#include "rtc_base/deprecation.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/dscp.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
Niels Möllere6933812018-11-05 13:01:41 +010054// TODO(bugs.webrtc.org/9584): Compatibility alias, delete as soon as downstream
55// code is updated.
56typedef int64_t PacketTime;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020057
58// Provides the ability to receive packets asynchronously. Sends are not
59// buffered since it is acceptable to drop packets under high load.
60class AsyncPacketSocket : public sigslot::has_slots<> {
61 public:
62 enum State {
63 STATE_CLOSED,
64 STATE_BINDING,
65 STATE_BOUND,
66 STATE_CONNECTING,
67 STATE_CONNECTED
68 };
69
70 AsyncPacketSocket();
71 ~AsyncPacketSocket() override;
72
73 // Returns current local address. Address may be set to null if the
74 // socket is not bound yet (GetState() returns STATE_BINDING).
75 virtual SocketAddress GetLocalAddress() const = 0;
76
77 // Returns remote address. Returns zeroes if this is not a client TCP socket.
78 virtual SocketAddress GetRemoteAddress() const = 0;
79
80 // Send a packet.
Yves Gerey665174f2018-06-19 15:03:05 +020081 virtual int Send(const void* pv, size_t cb, const PacketOptions& options) = 0;
82 virtual int SendTo(const void* pv,
83 size_t cb,
84 const SocketAddress& addr,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020085 const PacketOptions& options) = 0;
86
87 // Close the socket.
88 virtual int Close() = 0;
89
90 // Returns current state of the socket.
91 virtual State GetState() const = 0;
92
93 // Get/set options.
94 virtual int GetOption(Socket::Option opt, int* value) = 0;
95 virtual int SetOption(Socket::Option opt, int value) = 0;
96
97 // Get/Set current error.
98 // TODO: Remove SetError().
99 virtual int GetError() const = 0;
100 virtual void SetError(int error) = 0;
101
102 // Emitted each time a packet is read. Used only for UDP and
103 // connected TCP sockets.
Yves Gerey665174f2018-06-19 15:03:05 +0200104 sigslot::signal5<AsyncPacketSocket*,
105 const char*,
106 size_t,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200107 const SocketAddress&,
Niels Möllere6933812018-11-05 13:01:41 +0100108 // TODO(bugs.webrtc.org/9584): Change to passing the int64_t
109 // timestamp by value.
110 const int64_t&>
Yves Gerey665174f2018-06-19 15:03:05 +0200111 SignalReadPacket;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200112
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
Qingsi Wang6e641e62018-04-11 20:14:17 -0700140void CopySocketInformationToPacketInfo(size_t packet_size_bytes,
141 const AsyncPacketSocket& socket_from,
Qingsi Wang4ea53b32018-04-16 18:22:31 -0700142 bool is_connectionless,
Qingsi Wang6e641e62018-04-11 20:14:17 -0700143 rtc::PacketInfo* info);
144
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200145} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000146
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200147#endif // RTC_BASE_ASYNCPACKETSOCKET_H_