blob: b35ba0c5030670d0f53e2e8e665170acd3881edf [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 04:47:3111#ifndef RTC_BASE_ASYNCPACKETSOCKET_H_
12#define RTC_BASE_ASYNCPACKETSOCKET_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2613
Mirko Bonadei92ea95e2017-09-15 04:47:3114#include "rtc_base/constructormagic.h"
Niels Möller15ca5a92018-11-01 13:32:4715#include "rtc_base/deprecation.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3116#include "rtc_base/dscp.h"
Yves Gerey3e707812018-11-28 15:47:4917#include "rtc_base/network/sent_packet.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "rtc_base/socket.h"
Artem Titove41c4332018-07-25 13:04:2819#include "rtc_base/third_party/sigslot/sigslot.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3120#include "rtc_base/timeutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2621
Henrik Kjellanderec78f1c2017-06-29 05:52:5022namespace 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-12 03:14:1729 PacketTimeUpdateParams(const PacketTimeUpdateParams& other);
Henrik Kjellanderec78f1c2017-06-29 05:52:5030 ~PacketTimeUpdateParams();
31
Qingsi Wang6e641e62018-04-12 03:14:1732 int rtp_sendtime_extension_id = -1; // extension header id present in packet.
Yves Gerey665174f2018-06-19 13:03:0533 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 05:52:5036};
37
38// This structure holds meta information for the packet which is about to send
39// over network.
40struct PacketOptions {
Qingsi Wang6e641e62018-04-12 03:14:1741 PacketOptions();
42 explicit PacketOptions(DiffServCodePoint dscp);
43 PacketOptions(const PacketOptions& other);
44 ~PacketOptions();
Henrik Kjellanderec78f1c2017-06-29 05:52:5045
Qingsi Wang6e641e62018-04-12 03:14:1746 DiffServCodePoint dscp = DSCP_NO_CHANGE;
Bjorn Mellem3a9c46d2018-04-25 20:24:4847 // 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 05:52:5050 PacketTimeUpdateParams packet_time_params;
Qingsi Wang6e641e62018-04-12 03:14:1751 // PacketInfo is passed to SentPacket when signaling this packet is sent.
52 PacketInfo info_signaled_after_sent;
Henrik Kjellanderec78f1c2017-06-29 05:52:5053};
54
Niels Möllere6933812018-11-05 12:01:4155// TODO(bugs.webrtc.org/9584): Compatibility alias, delete as soon as downstream
56// code is updated.
57typedef int64_t PacketTime;
Henrik Kjellanderec78f1c2017-06-29 05:52:5058
59// Provides the ability to receive packets asynchronously. Sends are not
60// buffered since it is acceptable to drop packets under high load.
61class AsyncPacketSocket : public sigslot::has_slots<> {
62 public:
63 enum State {
64 STATE_CLOSED,
65 STATE_BINDING,
66 STATE_BOUND,
67 STATE_CONNECTING,
68 STATE_CONNECTED
69 };
70
71 AsyncPacketSocket();
72 ~AsyncPacketSocket() override;
73
74 // Returns current local address. Address may be set to null if the
75 // socket is not bound yet (GetState() returns STATE_BINDING).
76 virtual SocketAddress GetLocalAddress() const = 0;
77
78 // Returns remote address. Returns zeroes if this is not a client TCP socket.
79 virtual SocketAddress GetRemoteAddress() const = 0;
80
81 // Send a packet.
Yves Gerey665174f2018-06-19 13:03:0582 virtual int Send(const void* pv, size_t cb, const PacketOptions& options) = 0;
83 virtual int SendTo(const void* pv,
84 size_t cb,
85 const SocketAddress& addr,
Henrik Kjellanderec78f1c2017-06-29 05:52:5086 const PacketOptions& options) = 0;
87
88 // Close the socket.
89 virtual int Close() = 0;
90
91 // Returns current state of the socket.
92 virtual State GetState() const = 0;
93
94 // Get/set options.
95 virtual int GetOption(Socket::Option opt, int* value) = 0;
96 virtual int SetOption(Socket::Option opt, int value) = 0;
97
98 // Get/Set current error.
99 // TODO: Remove SetError().
100 virtual int GetError() const = 0;
101 virtual void SetError(int error) = 0;
102
103 // Emitted each time a packet is read. Used only for UDP and
104 // connected TCP sockets.
Yves Gerey665174f2018-06-19 13:03:05105 sigslot::signal5<AsyncPacketSocket*,
106 const char*,
107 size_t,
Henrik Kjellanderec78f1c2017-06-29 05:52:50108 const SocketAddress&,
Niels Möllere6933812018-11-05 12:01:41109 // TODO(bugs.webrtc.org/9584): Change to passing the int64_t
110 // timestamp by value.
111 const int64_t&>
Yves Gerey665174f2018-06-19 13:03:05112 SignalReadPacket;
Henrik Kjellanderec78f1c2017-06-29 05:52:50113
114 // Emitted each time a packet is sent.
115 sigslot::signal2<AsyncPacketSocket*, const SentPacket&> SignalSentPacket;
116
117 // Emitted when the socket is currently able to send.
118 sigslot::signal1<AsyncPacketSocket*> SignalReadyToSend;
119
120 // Emitted after address for the socket is allocated, i.e. binding
121 // is finished. State of the socket is changed from BINDING to BOUND
122 // (for UDP and server TCP sockets) or CONNECTING (for client TCP
123 // sockets).
124 sigslot::signal2<AsyncPacketSocket*, const SocketAddress&> SignalAddressReady;
125
126 // Emitted for client TCP sockets when state is changed from
127 // CONNECTING to CONNECTED.
128 sigslot::signal1<AsyncPacketSocket*> SignalConnect;
129
130 // Emitted for client TCP sockets when state is changed from
131 // CONNECTED to CLOSED.
132 sigslot::signal2<AsyncPacketSocket*, int> SignalClose;
133
134 // Used only for listening TCP sockets.
135 sigslot::signal2<AsyncPacketSocket*, AsyncPacketSocket*> SignalNewConnection;
136
137 private:
138 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncPacketSocket);
139};
140
Qingsi Wang6e641e62018-04-12 03:14:17141void CopySocketInformationToPacketInfo(size_t packet_size_bytes,
142 const AsyncPacketSocket& socket_from,
Qingsi Wang4ea53b32018-04-17 01:22:31143 bool is_connectionless,
Qingsi Wang6e641e62018-04-12 03:14:17144 rtc::PacketInfo* info);
145
Henrik Kjellanderec78f1c2017-06-29 05:52:50146} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26147
Mirko Bonadei92ea95e2017-09-15 04:47:31148#endif // RTC_BASE_ASYNCPACKETSOCKET_H_