blob: 0affd245d422fd41d44111b670e3161adbb3f8f3 [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
11#ifndef WEBRTC_BASE_ASYNCPACKETSOCKET_H_
12#define WEBRTC_BASE_ASYNCPACKETSOCKET_H_
13
14#include "webrtc/base/dscp.h"
15#include "webrtc/base/sigslot.h"
16#include "webrtc/base/socket.h"
17#include "webrtc/base/timeutils.h"
18
19namespace rtc {
20
21// This structure holds the info needed to update the packet send time header
22// extension, including the information needed to update the authentication tag
23// after changing the value.
24struct PacketTimeUpdateParams {
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000025 PacketTimeUpdateParams();
26 ~PacketTimeUpdateParams();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000027
28 int rtp_sendtime_extension_id; // extension header id present in packet.
29 std::vector<char> srtp_auth_key; // Authentication key.
30 int srtp_auth_tag_len; // Authentication tag length.
31 int64 srtp_packet_index; // Required for Rtp Packet authentication.
32};
33
34// This structure holds meta information for the packet which is about to send
35// over network.
36struct PacketOptions {
37 PacketOptions() : dscp(DSCP_NO_CHANGE) {}
38 explicit PacketOptions(DiffServCodePoint dscp) : dscp(dscp) {}
39
40 DiffServCodePoint dscp;
41 PacketTimeUpdateParams packet_time_params;
42};
43
44// This structure will have the information about when packet is actually
45// received by socket.
46struct PacketTime {
47 PacketTime() : timestamp(-1), not_before(-1) {}
48 PacketTime(int64 timestamp, int64 not_before)
49 : timestamp(timestamp), not_before(not_before) {
50 }
51
52 int64 timestamp; // Receive time after socket delivers the data.
53 int64 not_before; // Earliest possible time the data could have arrived,
54 // indicating the potential error in the |timestamp| value,
55 // in case the system, is busy. For example, the time of
56 // the last select() call.
57 // If unknown, this value will be set to zero.
58};
59
60inline PacketTime CreatePacketTime(int64 not_before) {
61 return PacketTime(TimeMicros(), not_before);
62}
63
64// Provides the ability to receive packets asynchronously. Sends are not
65// buffered since it is acceptable to drop packets under high load.
66class AsyncPacketSocket : public sigslot::has_slots<> {
67 public:
68 enum State {
69 STATE_CLOSED,
70 STATE_BINDING,
71 STATE_BOUND,
72 STATE_CONNECTING,
73 STATE_CONNECTED
74 };
75
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000076 AsyncPacketSocket();
77 ~AsyncPacketSocket() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000078
79 // Returns current local address. Address may be set to NULL if the
80 // socket is not bound yet (GetState() returns STATE_BINDING).
81 virtual SocketAddress GetLocalAddress() const = 0;
82
83 // Returns remote address. Returns zeroes if this is not a client TCP socket.
84 virtual SocketAddress GetRemoteAddress() const = 0;
85
86 // Send a packet.
87 virtual int Send(const void *pv, size_t cb, const PacketOptions& options) = 0;
88 virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr,
89 const PacketOptions& options) = 0;
90
91 // Close the socket.
92 virtual int Close() = 0;
93
94 // Returns current state of the socket.
95 virtual State GetState() const = 0;
96
97 // Get/set options.
98 virtual int GetOption(Socket::Option opt, int* value) = 0;
99 virtual int SetOption(Socket::Option opt, int value) = 0;
100
101 // Get/Set current error.
102 // TODO: Remove SetError().
103 virtual int GetError() const = 0;
104 virtual void SetError(int error) = 0;
105
106 // Emitted each time a packet is read. Used only for UDP and
107 // connected TCP sockets.
108 sigslot::signal5<AsyncPacketSocket*, const char*, size_t,
109 const SocketAddress&,
110 const PacketTime&> SignalReadPacket;
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:
Thiago Farinaae0f0ee2015-04-04 23:56:53 +0000133 DISALLOW_COPY_AND_ASSIGN(AsyncPacketSocket);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000134};
135
136} // namespace rtc
137
138#endif // WEBRTC_BASE_ASYNCPACKETSOCKET_H_