1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker * Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker *
4*d9f75844SAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker * that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker * tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker * in the file PATENTS. All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker * be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker */
10*d9f75844SAndroid Build Coastguard Worker #ifndef NET_DCSCTP_PUBLIC_DCSCTP_SOCKET_H_
11*d9f75844SAndroid Build Coastguard Worker #define NET_DCSCTP_PUBLIC_DCSCTP_SOCKET_H_
12*d9f75844SAndroid Build Coastguard Worker
13*d9f75844SAndroid Build Coastguard Worker #include <cstdint>
14*d9f75844SAndroid Build Coastguard Worker #include <memory>
15*d9f75844SAndroid Build Coastguard Worker #include <utility>
16*d9f75844SAndroid Build Coastguard Worker
17*d9f75844SAndroid Build Coastguard Worker #include "absl/strings/string_view.h"
18*d9f75844SAndroid Build Coastguard Worker #include "absl/types/optional.h"
19*d9f75844SAndroid Build Coastguard Worker #include "api/array_view.h"
20*d9f75844SAndroid Build Coastguard Worker #include "api/task_queue/task_queue_base.h"
21*d9f75844SAndroid Build Coastguard Worker #include "net/dcsctp/public/dcsctp_handover_state.h"
22*d9f75844SAndroid Build Coastguard Worker #include "net/dcsctp/public/dcsctp_message.h"
23*d9f75844SAndroid Build Coastguard Worker #include "net/dcsctp/public/dcsctp_options.h"
24*d9f75844SAndroid Build Coastguard Worker #include "net/dcsctp/public/packet_observer.h"
25*d9f75844SAndroid Build Coastguard Worker #include "net/dcsctp/public/timeout.h"
26*d9f75844SAndroid Build Coastguard Worker #include "net/dcsctp/public/types.h"
27*d9f75844SAndroid Build Coastguard Worker
28*d9f75844SAndroid Build Coastguard Worker namespace dcsctp {
29*d9f75844SAndroid Build Coastguard Worker
30*d9f75844SAndroid Build Coastguard Worker // The socket/association state
31*d9f75844SAndroid Build Coastguard Worker enum class SocketState {
32*d9f75844SAndroid Build Coastguard Worker // The socket is closed.
33*d9f75844SAndroid Build Coastguard Worker kClosed,
34*d9f75844SAndroid Build Coastguard Worker // The socket has initiated a connection, which is not yet established. Note
35*d9f75844SAndroid Build Coastguard Worker // that for incoming connections and for reconnections when the socket is
36*d9f75844SAndroid Build Coastguard Worker // already connected, the socket will not transition to this state.
37*d9f75844SAndroid Build Coastguard Worker kConnecting,
38*d9f75844SAndroid Build Coastguard Worker // The socket is connected, and the connection is established.
39*d9f75844SAndroid Build Coastguard Worker kConnected,
40*d9f75844SAndroid Build Coastguard Worker // The socket is shutting down, and the connection is not yet closed.
41*d9f75844SAndroid Build Coastguard Worker kShuttingDown,
42*d9f75844SAndroid Build Coastguard Worker };
43*d9f75844SAndroid Build Coastguard Worker
44*d9f75844SAndroid Build Coastguard Worker // Send options for sending messages
45*d9f75844SAndroid Build Coastguard Worker struct SendOptions {
46*d9f75844SAndroid Build Coastguard Worker // If the message should be sent with unordered message delivery.
47*d9f75844SAndroid Build Coastguard Worker IsUnordered unordered = IsUnordered(false);
48*d9f75844SAndroid Build Coastguard Worker
49*d9f75844SAndroid Build Coastguard Worker // If set, will discard messages that haven't been correctly sent and
50*d9f75844SAndroid Build Coastguard Worker // received before the lifetime has expired. This is only available if the
51*d9f75844SAndroid Build Coastguard Worker // peer supports Partial Reliability Extension (RFC3758).
52*d9f75844SAndroid Build Coastguard Worker absl::optional<DurationMs> lifetime = absl::nullopt;
53*d9f75844SAndroid Build Coastguard Worker
54*d9f75844SAndroid Build Coastguard Worker // If set, limits the number of retransmissions. This is only available
55*d9f75844SAndroid Build Coastguard Worker // if the peer supports Partial Reliability Extension (RFC3758).
56*d9f75844SAndroid Build Coastguard Worker absl::optional<size_t> max_retransmissions = absl::nullopt;
57*d9f75844SAndroid Build Coastguard Worker
58*d9f75844SAndroid Build Coastguard Worker // If set, will generate lifecycle events for this message. See e.g.
59*d9f75844SAndroid Build Coastguard Worker // `DcSctpSocketCallbacks::OnLifecycleMessageFullySent`. This value is decided
60*d9f75844SAndroid Build Coastguard Worker // by the client and the library will provide it to all lifecycle callbacks.
61*d9f75844SAndroid Build Coastguard Worker LifecycleId lifecycle_id = LifecycleId::NotSet();
62*d9f75844SAndroid Build Coastguard Worker };
63*d9f75844SAndroid Build Coastguard Worker
64*d9f75844SAndroid Build Coastguard Worker enum class ErrorKind {
65*d9f75844SAndroid Build Coastguard Worker // Indicates that no error has occurred. This will never be the case when
66*d9f75844SAndroid Build Coastguard Worker // `OnError` or `OnAborted` is called.
67*d9f75844SAndroid Build Coastguard Worker kNoError,
68*d9f75844SAndroid Build Coastguard Worker // There have been too many retries or timeouts, and the library has given up.
69*d9f75844SAndroid Build Coastguard Worker kTooManyRetries,
70*d9f75844SAndroid Build Coastguard Worker // A command was received that is only possible to execute when the socket is
71*d9f75844SAndroid Build Coastguard Worker // connected, which it is not.
72*d9f75844SAndroid Build Coastguard Worker kNotConnected,
73*d9f75844SAndroid Build Coastguard Worker // Parsing of the command or its parameters failed.
74*d9f75844SAndroid Build Coastguard Worker kParseFailed,
75*d9f75844SAndroid Build Coastguard Worker // Commands are received in the wrong sequence, which indicates a
76*d9f75844SAndroid Build Coastguard Worker // synchronisation mismatch between the peers.
77*d9f75844SAndroid Build Coastguard Worker kWrongSequence,
78*d9f75844SAndroid Build Coastguard Worker // The peer has reported an issue using ERROR or ABORT command.
79*d9f75844SAndroid Build Coastguard Worker kPeerReported,
80*d9f75844SAndroid Build Coastguard Worker // The peer has performed a protocol violation.
81*d9f75844SAndroid Build Coastguard Worker kProtocolViolation,
82*d9f75844SAndroid Build Coastguard Worker // The receive or send buffers have been exhausted.
83*d9f75844SAndroid Build Coastguard Worker kResourceExhaustion,
84*d9f75844SAndroid Build Coastguard Worker // The client has performed an invalid operation.
85*d9f75844SAndroid Build Coastguard Worker kUnsupportedOperation,
86*d9f75844SAndroid Build Coastguard Worker };
87*d9f75844SAndroid Build Coastguard Worker
ToString(ErrorKind error)88*d9f75844SAndroid Build Coastguard Worker inline constexpr absl::string_view ToString(ErrorKind error) {
89*d9f75844SAndroid Build Coastguard Worker switch (error) {
90*d9f75844SAndroid Build Coastguard Worker case ErrorKind::kNoError:
91*d9f75844SAndroid Build Coastguard Worker return "NO_ERROR";
92*d9f75844SAndroid Build Coastguard Worker case ErrorKind::kTooManyRetries:
93*d9f75844SAndroid Build Coastguard Worker return "TOO_MANY_RETRIES";
94*d9f75844SAndroid Build Coastguard Worker case ErrorKind::kNotConnected:
95*d9f75844SAndroid Build Coastguard Worker return "NOT_CONNECTED";
96*d9f75844SAndroid Build Coastguard Worker case ErrorKind::kParseFailed:
97*d9f75844SAndroid Build Coastguard Worker return "PARSE_FAILED";
98*d9f75844SAndroid Build Coastguard Worker case ErrorKind::kWrongSequence:
99*d9f75844SAndroid Build Coastguard Worker return "WRONG_SEQUENCE";
100*d9f75844SAndroid Build Coastguard Worker case ErrorKind::kPeerReported:
101*d9f75844SAndroid Build Coastguard Worker return "PEER_REPORTED";
102*d9f75844SAndroid Build Coastguard Worker case ErrorKind::kProtocolViolation:
103*d9f75844SAndroid Build Coastguard Worker return "PROTOCOL_VIOLATION";
104*d9f75844SAndroid Build Coastguard Worker case ErrorKind::kResourceExhaustion:
105*d9f75844SAndroid Build Coastguard Worker return "RESOURCE_EXHAUSTION";
106*d9f75844SAndroid Build Coastguard Worker case ErrorKind::kUnsupportedOperation:
107*d9f75844SAndroid Build Coastguard Worker return "UNSUPPORTED_OPERATION";
108*d9f75844SAndroid Build Coastguard Worker }
109*d9f75844SAndroid Build Coastguard Worker }
110*d9f75844SAndroid Build Coastguard Worker
111*d9f75844SAndroid Build Coastguard Worker enum class SendStatus {
112*d9f75844SAndroid Build Coastguard Worker // The message was enqueued successfully. As sending the message is done
113*d9f75844SAndroid Build Coastguard Worker // asynchronously, this is no guarantee that the message has been actually
114*d9f75844SAndroid Build Coastguard Worker // sent.
115*d9f75844SAndroid Build Coastguard Worker kSuccess,
116*d9f75844SAndroid Build Coastguard Worker // The message was rejected as the payload was empty (which is not allowed in
117*d9f75844SAndroid Build Coastguard Worker // SCTP).
118*d9f75844SAndroid Build Coastguard Worker kErrorMessageEmpty,
119*d9f75844SAndroid Build Coastguard Worker // The message was rejected as the payload was larger than what has been set
120*d9f75844SAndroid Build Coastguard Worker // as `DcSctpOptions.max_message_size`.
121*d9f75844SAndroid Build Coastguard Worker kErrorMessageTooLarge,
122*d9f75844SAndroid Build Coastguard Worker // The message could not be enqueued as the socket is out of resources. This
123*d9f75844SAndroid Build Coastguard Worker // mainly indicates that the send queue is full.
124*d9f75844SAndroid Build Coastguard Worker kErrorResourceExhaustion,
125*d9f75844SAndroid Build Coastguard Worker // The message could not be sent as the socket is shutting down.
126*d9f75844SAndroid Build Coastguard Worker kErrorShuttingDown,
127*d9f75844SAndroid Build Coastguard Worker };
128*d9f75844SAndroid Build Coastguard Worker
ToString(SendStatus error)129*d9f75844SAndroid Build Coastguard Worker inline constexpr absl::string_view ToString(SendStatus error) {
130*d9f75844SAndroid Build Coastguard Worker switch (error) {
131*d9f75844SAndroid Build Coastguard Worker case SendStatus::kSuccess:
132*d9f75844SAndroid Build Coastguard Worker return "SUCCESS";
133*d9f75844SAndroid Build Coastguard Worker case SendStatus::kErrorMessageEmpty:
134*d9f75844SAndroid Build Coastguard Worker return "ERROR_MESSAGE_EMPTY";
135*d9f75844SAndroid Build Coastguard Worker case SendStatus::kErrorMessageTooLarge:
136*d9f75844SAndroid Build Coastguard Worker return "ERROR_MESSAGE_TOO_LARGE";
137*d9f75844SAndroid Build Coastguard Worker case SendStatus::kErrorResourceExhaustion:
138*d9f75844SAndroid Build Coastguard Worker return "ERROR_RESOURCE_EXHAUSTION";
139*d9f75844SAndroid Build Coastguard Worker case SendStatus::kErrorShuttingDown:
140*d9f75844SAndroid Build Coastguard Worker return "ERROR_SHUTTING_DOWN";
141*d9f75844SAndroid Build Coastguard Worker }
142*d9f75844SAndroid Build Coastguard Worker }
143*d9f75844SAndroid Build Coastguard Worker
144*d9f75844SAndroid Build Coastguard Worker // Return value of ResetStreams.
145*d9f75844SAndroid Build Coastguard Worker enum class ResetStreamsStatus {
146*d9f75844SAndroid Build Coastguard Worker // If the connection is not yet established, this will be returned.
147*d9f75844SAndroid Build Coastguard Worker kNotConnected,
148*d9f75844SAndroid Build Coastguard Worker // Indicates that ResetStreams operation has been successfully initiated.
149*d9f75844SAndroid Build Coastguard Worker kPerformed,
150*d9f75844SAndroid Build Coastguard Worker // Indicates that ResetStreams has failed as it's not supported by the peer.
151*d9f75844SAndroid Build Coastguard Worker kNotSupported,
152*d9f75844SAndroid Build Coastguard Worker };
153*d9f75844SAndroid Build Coastguard Worker
ToString(ResetStreamsStatus error)154*d9f75844SAndroid Build Coastguard Worker inline constexpr absl::string_view ToString(ResetStreamsStatus error) {
155*d9f75844SAndroid Build Coastguard Worker switch (error) {
156*d9f75844SAndroid Build Coastguard Worker case ResetStreamsStatus::kNotConnected:
157*d9f75844SAndroid Build Coastguard Worker return "NOT_CONNECTED";
158*d9f75844SAndroid Build Coastguard Worker case ResetStreamsStatus::kPerformed:
159*d9f75844SAndroid Build Coastguard Worker return "PERFORMED";
160*d9f75844SAndroid Build Coastguard Worker case ResetStreamsStatus::kNotSupported:
161*d9f75844SAndroid Build Coastguard Worker return "NOT_SUPPORTED";
162*d9f75844SAndroid Build Coastguard Worker }
163*d9f75844SAndroid Build Coastguard Worker }
164*d9f75844SAndroid Build Coastguard Worker
165*d9f75844SAndroid Build Coastguard Worker // Return value of DcSctpSocketCallbacks::SendPacketWithStatus.
166*d9f75844SAndroid Build Coastguard Worker enum class SendPacketStatus {
167*d9f75844SAndroid Build Coastguard Worker // Indicates that the packet was successfully sent. As sending is unreliable,
168*d9f75844SAndroid Build Coastguard Worker // there are no guarantees that the packet was actually delivered.
169*d9f75844SAndroid Build Coastguard Worker kSuccess,
170*d9f75844SAndroid Build Coastguard Worker // The packet was not sent due to a temporary failure, such as the local send
171*d9f75844SAndroid Build Coastguard Worker // buffer becoming exhausted. This return value indicates that the socket will
172*d9f75844SAndroid Build Coastguard Worker // recover and sending that packet can be retried at a later time.
173*d9f75844SAndroid Build Coastguard Worker kTemporaryFailure,
174*d9f75844SAndroid Build Coastguard Worker // The packet was not sent due to other reasons.
175*d9f75844SAndroid Build Coastguard Worker kError,
176*d9f75844SAndroid Build Coastguard Worker };
177*d9f75844SAndroid Build Coastguard Worker
178*d9f75844SAndroid Build Coastguard Worker // Represent known SCTP implementations.
179*d9f75844SAndroid Build Coastguard Worker enum class SctpImplementation {
180*d9f75844SAndroid Build Coastguard Worker // There is not enough information toto determine any SCTP implementation.
181*d9f75844SAndroid Build Coastguard Worker kUnknown,
182*d9f75844SAndroid Build Coastguard Worker // This implementation.
183*d9f75844SAndroid Build Coastguard Worker kDcsctp,
184*d9f75844SAndroid Build Coastguard Worker // https://github.com/sctplab/usrsctp.
185*d9f75844SAndroid Build Coastguard Worker kUsrSctp,
186*d9f75844SAndroid Build Coastguard Worker // Any other implementation.
187*d9f75844SAndroid Build Coastguard Worker kOther,
188*d9f75844SAndroid Build Coastguard Worker };
189*d9f75844SAndroid Build Coastguard Worker
ToString(SctpImplementation implementation)190*d9f75844SAndroid Build Coastguard Worker inline constexpr absl::string_view ToString(SctpImplementation implementation) {
191*d9f75844SAndroid Build Coastguard Worker switch (implementation) {
192*d9f75844SAndroid Build Coastguard Worker case SctpImplementation::kUnknown:
193*d9f75844SAndroid Build Coastguard Worker return "unknown";
194*d9f75844SAndroid Build Coastguard Worker case SctpImplementation::kDcsctp:
195*d9f75844SAndroid Build Coastguard Worker return "dcsctp";
196*d9f75844SAndroid Build Coastguard Worker case SctpImplementation::kUsrSctp:
197*d9f75844SAndroid Build Coastguard Worker return "usrsctp";
198*d9f75844SAndroid Build Coastguard Worker case SctpImplementation::kOther:
199*d9f75844SAndroid Build Coastguard Worker return "other";
200*d9f75844SAndroid Build Coastguard Worker }
201*d9f75844SAndroid Build Coastguard Worker }
202*d9f75844SAndroid Build Coastguard Worker
203*d9f75844SAndroid Build Coastguard Worker // Tracked metrics, which is the return value of GetMetrics. Optional members
204*d9f75844SAndroid Build Coastguard Worker // will be unset when they are not yet known.
205*d9f75844SAndroid Build Coastguard Worker struct Metrics {
206*d9f75844SAndroid Build Coastguard Worker // Transmission stats and metrics.
207*d9f75844SAndroid Build Coastguard Worker
208*d9f75844SAndroid Build Coastguard Worker // Number of packets sent.
209*d9f75844SAndroid Build Coastguard Worker size_t tx_packets_count = 0;
210*d9f75844SAndroid Build Coastguard Worker
211*d9f75844SAndroid Build Coastguard Worker // Number of messages requested to be sent.
212*d9f75844SAndroid Build Coastguard Worker size_t tx_messages_count = 0;
213*d9f75844SAndroid Build Coastguard Worker
214*d9f75844SAndroid Build Coastguard Worker // The current congestion window (cwnd) in bytes, corresponding to spinfo_cwnd
215*d9f75844SAndroid Build Coastguard Worker // defined in RFC6458.
216*d9f75844SAndroid Build Coastguard Worker size_t cwnd_bytes = 0;
217*d9f75844SAndroid Build Coastguard Worker
218*d9f75844SAndroid Build Coastguard Worker // Smoothed round trip time, corresponding to spinfo_srtt defined in RFC6458.
219*d9f75844SAndroid Build Coastguard Worker int srtt_ms = 0;
220*d9f75844SAndroid Build Coastguard Worker
221*d9f75844SAndroid Build Coastguard Worker // Number of data items in the retransmission queue that haven’t been
222*d9f75844SAndroid Build Coastguard Worker // acked/nacked yet and are in-flight. Corresponding to sstat_unackdata
223*d9f75844SAndroid Build Coastguard Worker // defined in RFC6458. This may be an approximation when there are messages in
224*d9f75844SAndroid Build Coastguard Worker // the send queue that haven't been fragmented/packetized yet.
225*d9f75844SAndroid Build Coastguard Worker size_t unack_data_count = 0;
226*d9f75844SAndroid Build Coastguard Worker
227*d9f75844SAndroid Build Coastguard Worker // Receive stats and metrics.
228*d9f75844SAndroid Build Coastguard Worker
229*d9f75844SAndroid Build Coastguard Worker // Number of packets received.
230*d9f75844SAndroid Build Coastguard Worker size_t rx_packets_count = 0;
231*d9f75844SAndroid Build Coastguard Worker
232*d9f75844SAndroid Build Coastguard Worker // Number of messages received.
233*d9f75844SAndroid Build Coastguard Worker size_t rx_messages_count = 0;
234*d9f75844SAndroid Build Coastguard Worker
235*d9f75844SAndroid Build Coastguard Worker // The peer’s last announced receiver window size, corresponding to
236*d9f75844SAndroid Build Coastguard Worker // sstat_rwnd defined in RFC6458.
237*d9f75844SAndroid Build Coastguard Worker uint32_t peer_rwnd_bytes = 0;
238*d9f75844SAndroid Build Coastguard Worker
239*d9f75844SAndroid Build Coastguard Worker // Returns the detected SCTP implementation of the peer. As this is not
240*d9f75844SAndroid Build Coastguard Worker // explicitly signalled during the connection establishment, heuristics is
241*d9f75844SAndroid Build Coastguard Worker // used to analyze e.g. the state cookie in the INIT-ACK chunk.
242*d9f75844SAndroid Build Coastguard Worker SctpImplementation peer_implementation = SctpImplementation::kUnknown;
243*d9f75844SAndroid Build Coastguard Worker
244*d9f75844SAndroid Build Coastguard Worker // Indicates if RFC8260 User Message Interleaving has been negotiated by both
245*d9f75844SAndroid Build Coastguard Worker // peers.
246*d9f75844SAndroid Build Coastguard Worker bool uses_message_interleaving = false;
247*d9f75844SAndroid Build Coastguard Worker
248*d9f75844SAndroid Build Coastguard Worker // The number of negotiated incoming and outgoing streams, which is configured
249*d9f75844SAndroid Build Coastguard Worker // locally as `DcSctpOptions::announced_maximum_incoming_streams` and
250*d9f75844SAndroid Build Coastguard Worker // `DcSctpOptions::announced_maximum_outgoing_streams`, and which will be
251*d9f75844SAndroid Build Coastguard Worker // signaled by the peer during connection.
252*d9f75844SAndroid Build Coastguard Worker uint16_t negotiated_maximum_incoming_streams = 0;
253*d9f75844SAndroid Build Coastguard Worker uint16_t negotiated_maximum_outgoing_streams = 0;
254*d9f75844SAndroid Build Coastguard Worker };
255*d9f75844SAndroid Build Coastguard Worker
256*d9f75844SAndroid Build Coastguard Worker // Callbacks that the DcSctpSocket will call synchronously to the owning
257*d9f75844SAndroid Build Coastguard Worker // client. It is allowed to call back into the library from callbacks that start
258*d9f75844SAndroid Build Coastguard Worker // with "On". It has been explicitly documented when it's not allowed to call
259*d9f75844SAndroid Build Coastguard Worker // back into this library from within a callback.
260*d9f75844SAndroid Build Coastguard Worker //
261*d9f75844SAndroid Build Coastguard Worker // Theses callbacks are only synchronously triggered as a result of the client
262*d9f75844SAndroid Build Coastguard Worker // calling a public method in `DcSctpSocketInterface`.
263*d9f75844SAndroid Build Coastguard Worker class DcSctpSocketCallbacks {
264*d9f75844SAndroid Build Coastguard Worker public:
265*d9f75844SAndroid Build Coastguard Worker virtual ~DcSctpSocketCallbacks() = default;
266*d9f75844SAndroid Build Coastguard Worker
267*d9f75844SAndroid Build Coastguard Worker // Called when the library wants the packet serialized as `data` to be sent.
268*d9f75844SAndroid Build Coastguard Worker //
269*d9f75844SAndroid Build Coastguard Worker // TODO(bugs.webrtc.org/12943): This method is deprecated, see
270*d9f75844SAndroid Build Coastguard Worker // `SendPacketWithStatus`.
271*d9f75844SAndroid Build Coastguard Worker //
272*d9f75844SAndroid Build Coastguard Worker // Note that it's NOT ALLOWED to call into this library from within this
273*d9f75844SAndroid Build Coastguard Worker // callback.
SendPacket(rtc::ArrayView<const uint8_t> data)274*d9f75844SAndroid Build Coastguard Worker virtual void SendPacket(rtc::ArrayView<const uint8_t> data) {}
275*d9f75844SAndroid Build Coastguard Worker
276*d9f75844SAndroid Build Coastguard Worker // Called when the library wants the packet serialized as `data` to be sent.
277*d9f75844SAndroid Build Coastguard Worker //
278*d9f75844SAndroid Build Coastguard Worker // Note that it's NOT ALLOWED to call into this library from within this
279*d9f75844SAndroid Build Coastguard Worker // callback.
SendPacketWithStatus(rtc::ArrayView<const uint8_t> data)280*d9f75844SAndroid Build Coastguard Worker virtual SendPacketStatus SendPacketWithStatus(
281*d9f75844SAndroid Build Coastguard Worker rtc::ArrayView<const uint8_t> data) {
282*d9f75844SAndroid Build Coastguard Worker SendPacket(data);
283*d9f75844SAndroid Build Coastguard Worker return SendPacketStatus::kSuccess;
284*d9f75844SAndroid Build Coastguard Worker }
285*d9f75844SAndroid Build Coastguard Worker
286*d9f75844SAndroid Build Coastguard Worker // Called when the library wants to create a Timeout. The callback must return
287*d9f75844SAndroid Build Coastguard Worker // an object that implements that interface.
288*d9f75844SAndroid Build Coastguard Worker //
289*d9f75844SAndroid Build Coastguard Worker // Low precision tasks are scheduled more efficiently by using leeway to
290*d9f75844SAndroid Build Coastguard Worker // reduce Idle Wake Ups and is the preferred precision whenever possible. High
291*d9f75844SAndroid Build Coastguard Worker // precision timeouts do not have this leeway, but is still limited by OS
292*d9f75844SAndroid Build Coastguard Worker // timer precision. At the time of writing, kLow's additional leeway may be up
293*d9f75844SAndroid Build Coastguard Worker // to 17 ms, but please see webrtc::TaskQueueBase::DelayPrecision for
294*d9f75844SAndroid Build Coastguard Worker // up-to-date information.
295*d9f75844SAndroid Build Coastguard Worker //
296*d9f75844SAndroid Build Coastguard Worker // Note that it's NOT ALLOWED to call into this library from within this
297*d9f75844SAndroid Build Coastguard Worker // callback.
CreateTimeout(webrtc::TaskQueueBase::DelayPrecision precision)298*d9f75844SAndroid Build Coastguard Worker virtual std::unique_ptr<Timeout> CreateTimeout(
299*d9f75844SAndroid Build Coastguard Worker webrtc::TaskQueueBase::DelayPrecision precision) {
300*d9f75844SAndroid Build Coastguard Worker // TODO(hbos): When dependencies have migrated to this new signature, make
301*d9f75844SAndroid Build Coastguard Worker // this pure virtual and delete the other version.
302*d9f75844SAndroid Build Coastguard Worker return CreateTimeout();
303*d9f75844SAndroid Build Coastguard Worker }
304*d9f75844SAndroid Build Coastguard Worker // TODO(hbos): When dependencies have migrated to the other signature, delete
305*d9f75844SAndroid Build Coastguard Worker // this version.
CreateTimeout()306*d9f75844SAndroid Build Coastguard Worker virtual std::unique_ptr<Timeout> CreateTimeout() {
307*d9f75844SAndroid Build Coastguard Worker return CreateTimeout(webrtc::TaskQueueBase::DelayPrecision::kLow);
308*d9f75844SAndroid Build Coastguard Worker }
309*d9f75844SAndroid Build Coastguard Worker
310*d9f75844SAndroid Build Coastguard Worker // Returns the current time in milliseconds (from any epoch).
311*d9f75844SAndroid Build Coastguard Worker //
312*d9f75844SAndroid Build Coastguard Worker // Note that it's NOT ALLOWED to call into this library from within this
313*d9f75844SAndroid Build Coastguard Worker // callback.
314*d9f75844SAndroid Build Coastguard Worker virtual TimeMs TimeMillis() = 0;
315*d9f75844SAndroid Build Coastguard Worker
316*d9f75844SAndroid Build Coastguard Worker // Called when the library needs a random number uniformly distributed between
317*d9f75844SAndroid Build Coastguard Worker // `low` (inclusive) and `high` (exclusive). The random numbers used by the
318*d9f75844SAndroid Build Coastguard Worker // library are not used for cryptographic purposes. There are no requirements
319*d9f75844SAndroid Build Coastguard Worker // that the random number generator must be secure.
320*d9f75844SAndroid Build Coastguard Worker //
321*d9f75844SAndroid Build Coastguard Worker // Note that it's NOT ALLOWED to call into this library from within this
322*d9f75844SAndroid Build Coastguard Worker // callback.
323*d9f75844SAndroid Build Coastguard Worker virtual uint32_t GetRandomInt(uint32_t low, uint32_t high) = 0;
324*d9f75844SAndroid Build Coastguard Worker
325*d9f75844SAndroid Build Coastguard Worker // Triggered when the outgoing message buffer is empty, meaning that there are
326*d9f75844SAndroid Build Coastguard Worker // no more queued messages, but there can still be packets in-flight or to be
327*d9f75844SAndroid Build Coastguard Worker // retransmitted. (in contrast to SCTP_SENDER_DRY_EVENT).
328*d9f75844SAndroid Build Coastguard Worker //
329*d9f75844SAndroid Build Coastguard Worker // Note that it's NOT ALLOWED to call into this library from within this
330*d9f75844SAndroid Build Coastguard Worker // callback.
331*d9f75844SAndroid Build Coastguard Worker ABSL_DEPRECATED("Use OnTotalBufferedAmountLow instead")
NotifyOutgoingMessageBufferEmpty()332*d9f75844SAndroid Build Coastguard Worker virtual void NotifyOutgoingMessageBufferEmpty() {}
333*d9f75844SAndroid Build Coastguard Worker
334*d9f75844SAndroid Build Coastguard Worker // Called when the library has received an SCTP message in full and delivers
335*d9f75844SAndroid Build Coastguard Worker // it to the upper layer.
336*d9f75844SAndroid Build Coastguard Worker //
337*d9f75844SAndroid Build Coastguard Worker // It is allowed to call into this library from within this callback.
338*d9f75844SAndroid Build Coastguard Worker virtual void OnMessageReceived(DcSctpMessage message) = 0;
339*d9f75844SAndroid Build Coastguard Worker
340*d9f75844SAndroid Build Coastguard Worker // Triggered when an non-fatal error is reported by either this library or
341*d9f75844SAndroid Build Coastguard Worker // from the other peer (by sending an ERROR command). These should be logged,
342*d9f75844SAndroid Build Coastguard Worker // but no other action need to be taken as the association is still viable.
343*d9f75844SAndroid Build Coastguard Worker //
344*d9f75844SAndroid Build Coastguard Worker // It is allowed to call into this library from within this callback.
345*d9f75844SAndroid Build Coastguard Worker virtual void OnError(ErrorKind error, absl::string_view message) = 0;
346*d9f75844SAndroid Build Coastguard Worker
347*d9f75844SAndroid Build Coastguard Worker // Triggered when the socket has aborted - either as decided by this socket
348*d9f75844SAndroid Build Coastguard Worker // due to e.g. too many retransmission attempts, or by the peer when
349*d9f75844SAndroid Build Coastguard Worker // receiving an ABORT command. No other callbacks will be done after this
350*d9f75844SAndroid Build Coastguard Worker // callback, unless reconnecting.
351*d9f75844SAndroid Build Coastguard Worker //
352*d9f75844SAndroid Build Coastguard Worker // It is allowed to call into this library from within this callback.
353*d9f75844SAndroid Build Coastguard Worker virtual void OnAborted(ErrorKind error, absl::string_view message) = 0;
354*d9f75844SAndroid Build Coastguard Worker
355*d9f75844SAndroid Build Coastguard Worker // Called when calling `Connect` succeeds, but also for incoming successful
356*d9f75844SAndroid Build Coastguard Worker // connection attempts.
357*d9f75844SAndroid Build Coastguard Worker //
358*d9f75844SAndroid Build Coastguard Worker // It is allowed to call into this library from within this callback.
359*d9f75844SAndroid Build Coastguard Worker virtual void OnConnected() = 0;
360*d9f75844SAndroid Build Coastguard Worker
361*d9f75844SAndroid Build Coastguard Worker // Called when the socket is closed in a controlled way. No other
362*d9f75844SAndroid Build Coastguard Worker // callbacks will be done after this callback, unless reconnecting.
363*d9f75844SAndroid Build Coastguard Worker //
364*d9f75844SAndroid Build Coastguard Worker // It is allowed to call into this library from within this callback.
365*d9f75844SAndroid Build Coastguard Worker virtual void OnClosed() = 0;
366*d9f75844SAndroid Build Coastguard Worker
367*d9f75844SAndroid Build Coastguard Worker // On connection restarted (by peer). This is just a notification, and the
368*d9f75844SAndroid Build Coastguard Worker // association is expected to work fine after this call, but there could have
369*d9f75844SAndroid Build Coastguard Worker // been packet loss as a result of restarting the association.
370*d9f75844SAndroid Build Coastguard Worker //
371*d9f75844SAndroid Build Coastguard Worker // It is allowed to call into this library from within this callback.
372*d9f75844SAndroid Build Coastguard Worker virtual void OnConnectionRestarted() = 0;
373*d9f75844SAndroid Build Coastguard Worker
374*d9f75844SAndroid Build Coastguard Worker // Indicates that a stream reset request has failed.
375*d9f75844SAndroid Build Coastguard Worker //
376*d9f75844SAndroid Build Coastguard Worker // It is allowed to call into this library from within this callback.
377*d9f75844SAndroid Build Coastguard Worker virtual void OnStreamsResetFailed(
378*d9f75844SAndroid Build Coastguard Worker rtc::ArrayView<const StreamID> outgoing_streams,
379*d9f75844SAndroid Build Coastguard Worker absl::string_view reason) = 0;
380*d9f75844SAndroid Build Coastguard Worker
381*d9f75844SAndroid Build Coastguard Worker // Indicates that a stream reset request has been performed.
382*d9f75844SAndroid Build Coastguard Worker //
383*d9f75844SAndroid Build Coastguard Worker // It is allowed to call into this library from within this callback.
384*d9f75844SAndroid Build Coastguard Worker virtual void OnStreamsResetPerformed(
385*d9f75844SAndroid Build Coastguard Worker rtc::ArrayView<const StreamID> outgoing_streams) = 0;
386*d9f75844SAndroid Build Coastguard Worker
387*d9f75844SAndroid Build Coastguard Worker // When a peer has reset some of its outgoing streams, this will be called. An
388*d9f75844SAndroid Build Coastguard Worker // empty list indicates that all streams have been reset.
389*d9f75844SAndroid Build Coastguard Worker //
390*d9f75844SAndroid Build Coastguard Worker // It is allowed to call into this library from within this callback.
391*d9f75844SAndroid Build Coastguard Worker virtual void OnIncomingStreamsReset(
392*d9f75844SAndroid Build Coastguard Worker rtc::ArrayView<const StreamID> incoming_streams) = 0;
393*d9f75844SAndroid Build Coastguard Worker
394*d9f75844SAndroid Build Coastguard Worker // Will be called when the amount of data buffered to be sent falls to or
395*d9f75844SAndroid Build Coastguard Worker // below the threshold set when calling `SetBufferedAmountLowThreshold`.
396*d9f75844SAndroid Build Coastguard Worker //
397*d9f75844SAndroid Build Coastguard Worker // It is allowed to call into this library from within this callback.
OnBufferedAmountLow(StreamID stream_id)398*d9f75844SAndroid Build Coastguard Worker virtual void OnBufferedAmountLow(StreamID stream_id) {}
399*d9f75844SAndroid Build Coastguard Worker
400*d9f75844SAndroid Build Coastguard Worker // Will be called when the total amount of data buffered (in the entire send
401*d9f75844SAndroid Build Coastguard Worker // buffer, for all streams) falls to or below the threshold specified in
402*d9f75844SAndroid Build Coastguard Worker // `DcSctpOptions::total_buffered_amount_low_threshold`.
OnTotalBufferedAmountLow()403*d9f75844SAndroid Build Coastguard Worker virtual void OnTotalBufferedAmountLow() {}
404*d9f75844SAndroid Build Coastguard Worker
405*d9f75844SAndroid Build Coastguard Worker // == Lifecycle Events ==
406*d9f75844SAndroid Build Coastguard Worker //
407*d9f75844SAndroid Build Coastguard Worker // If a `lifecycle_id` is provided as `SendOptions`, lifecycle callbacks will
408*d9f75844SAndroid Build Coastguard Worker // be triggered as the message is processed by the library.
409*d9f75844SAndroid Build Coastguard Worker //
410*d9f75844SAndroid Build Coastguard Worker // The possible transitions are shown in the graph below:
411*d9f75844SAndroid Build Coastguard Worker //
412*d9f75844SAndroid Build Coastguard Worker // DcSctpSocket::Send ────────────────────────┐
413*d9f75844SAndroid Build Coastguard Worker // │ │
414*d9f75844SAndroid Build Coastguard Worker // │ │
415*d9f75844SAndroid Build Coastguard Worker // v v
416*d9f75844SAndroid Build Coastguard Worker // OnLifecycleMessageFullySent ───────> OnLifecycleMessageExpired
417*d9f75844SAndroid Build Coastguard Worker // │ │
418*d9f75844SAndroid Build Coastguard Worker // │ │
419*d9f75844SAndroid Build Coastguard Worker // v v
420*d9f75844SAndroid Build Coastguard Worker // OnLifeCycleMessageDelivered ────────────> OnLifecycleEnd
421*d9f75844SAndroid Build Coastguard Worker
422*d9f75844SAndroid Build Coastguard Worker // OnLifecycleMessageFullySent will be called when a message has been fully
423*d9f75844SAndroid Build Coastguard Worker // sent, meaning that the last fragment has been produced from the send queue
424*d9f75844SAndroid Build Coastguard Worker // and sent on the network. Note that this will trigger at most once per
425*d9f75844SAndroid Build Coastguard Worker // message even if the message was retransmitted due to packet loss.
426*d9f75844SAndroid Build Coastguard Worker //
427*d9f75844SAndroid Build Coastguard Worker // This is a lifecycle event.
428*d9f75844SAndroid Build Coastguard Worker //
429*d9f75844SAndroid Build Coastguard Worker // Note that it's NOT ALLOWED to call into this library from within this
430*d9f75844SAndroid Build Coastguard Worker // callback.
OnLifecycleMessageFullySent(LifecycleId lifecycle_id)431*d9f75844SAndroid Build Coastguard Worker virtual void OnLifecycleMessageFullySent(LifecycleId lifecycle_id) {}
432*d9f75844SAndroid Build Coastguard Worker
433*d9f75844SAndroid Build Coastguard Worker // OnLifecycleMessageExpired will be called when a message has expired. If it
434*d9f75844SAndroid Build Coastguard Worker // was expired with data remaining in the send queue that had not been sent
435*d9f75844SAndroid Build Coastguard Worker // ever, `maybe_delivered` will be set to false. If `maybe_delivered` is true,
436*d9f75844SAndroid Build Coastguard Worker // the message has at least once been sent and may have been correctly
437*d9f75844SAndroid Build Coastguard Worker // received by the peer, but it has expired before the receiver managed to
438*d9f75844SAndroid Build Coastguard Worker // acknowledge it. This means that if `maybe_delivered` is true, it's unknown
439*d9f75844SAndroid Build Coastguard Worker // if the message was lost or was delivered, and if `maybe_delivered` is
440*d9f75844SAndroid Build Coastguard Worker // false, it's guaranteed to not be delivered.
441*d9f75844SAndroid Build Coastguard Worker //
442*d9f75844SAndroid Build Coastguard Worker // It's guaranteed that `OnLifecycleMessageDelivered` is not called if this
443*d9f75844SAndroid Build Coastguard Worker // callback has triggered.
444*d9f75844SAndroid Build Coastguard Worker //
445*d9f75844SAndroid Build Coastguard Worker // This is a lifecycle event.
446*d9f75844SAndroid Build Coastguard Worker //
447*d9f75844SAndroid Build Coastguard Worker // Note that it's NOT ALLOWED to call into this library from within this
448*d9f75844SAndroid Build Coastguard Worker // callback.
OnLifecycleMessageExpired(LifecycleId lifecycle_id,bool maybe_delivered)449*d9f75844SAndroid Build Coastguard Worker virtual void OnLifecycleMessageExpired(LifecycleId lifecycle_id,
450*d9f75844SAndroid Build Coastguard Worker bool maybe_delivered) {}
451*d9f75844SAndroid Build Coastguard Worker
452*d9f75844SAndroid Build Coastguard Worker // OnLifecycleMessageDelivered will be called when a non-expired message has
453*d9f75844SAndroid Build Coastguard Worker // been acknowledged by the peer as delivered.
454*d9f75844SAndroid Build Coastguard Worker //
455*d9f75844SAndroid Build Coastguard Worker // Note that this will trigger only when the peer moves its cumulative TSN ack
456*d9f75844SAndroid Build Coastguard Worker // beyond this message, and will not fire for messages acked using
457*d9f75844SAndroid Build Coastguard Worker // gap-ack-blocks as those are renegable. This means that this may fire a bit
458*d9f75844SAndroid Build Coastguard Worker // later than the message was actually first "acked" by the peer, as -
459*d9f75844SAndroid Build Coastguard Worker // according to the protocol - those acks may be unacked later by the client.
460*d9f75844SAndroid Build Coastguard Worker //
461*d9f75844SAndroid Build Coastguard Worker // It's guaranteed that `OnLifecycleMessageExpired` is not called if this
462*d9f75844SAndroid Build Coastguard Worker // callback has triggered.
463*d9f75844SAndroid Build Coastguard Worker //
464*d9f75844SAndroid Build Coastguard Worker // This is a lifecycle event.
465*d9f75844SAndroid Build Coastguard Worker //
466*d9f75844SAndroid Build Coastguard Worker // Note that it's NOT ALLOWED to call into this library from within this
467*d9f75844SAndroid Build Coastguard Worker // callback.
OnLifecycleMessageDelivered(LifecycleId lifecycle_id)468*d9f75844SAndroid Build Coastguard Worker virtual void OnLifecycleMessageDelivered(LifecycleId lifecycle_id) {}
469*d9f75844SAndroid Build Coastguard Worker
470*d9f75844SAndroid Build Coastguard Worker // OnLifecycleEnd will be called when a lifecycle event has reached its end.
471*d9f75844SAndroid Build Coastguard Worker // It will be called when processing of a message is complete, no matter how
472*d9f75844SAndroid Build Coastguard Worker // it completed. It will be called after all other lifecycle events, if any.
473*d9f75844SAndroid Build Coastguard Worker //
474*d9f75844SAndroid Build Coastguard Worker // Note that it's possible that this callback triggers without any other
475*d9f75844SAndroid Build Coastguard Worker // lifecycle callbacks having been called before in case of errors, such as
476*d9f75844SAndroid Build Coastguard Worker // attempting to send an empty message or failing to enqueue a message if the
477*d9f75844SAndroid Build Coastguard Worker // send queue is full.
478*d9f75844SAndroid Build Coastguard Worker //
479*d9f75844SAndroid Build Coastguard Worker // NOTE: When the socket is deallocated, there will be no `OnLifecycleEnd`
480*d9f75844SAndroid Build Coastguard Worker // callbacks sent for messages that were enqueued. But as long as the socket
481*d9f75844SAndroid Build Coastguard Worker // is alive, `OnLifecycleEnd` callbacks are guaranteed to be sent as messages
482*d9f75844SAndroid Build Coastguard Worker // are either expired or successfully acknowledged.
483*d9f75844SAndroid Build Coastguard Worker //
484*d9f75844SAndroid Build Coastguard Worker // This is a lifecycle event.
485*d9f75844SAndroid Build Coastguard Worker //
486*d9f75844SAndroid Build Coastguard Worker // Note that it's NOT ALLOWED to call into this library from within this
487*d9f75844SAndroid Build Coastguard Worker // callback.
OnLifecycleEnd(LifecycleId lifecycle_id)488*d9f75844SAndroid Build Coastguard Worker virtual void OnLifecycleEnd(LifecycleId lifecycle_id) {}
489*d9f75844SAndroid Build Coastguard Worker };
490*d9f75844SAndroid Build Coastguard Worker
491*d9f75844SAndroid Build Coastguard Worker // The DcSctpSocket implementation implements the following interface.
492*d9f75844SAndroid Build Coastguard Worker // This class is thread-compatible.
493*d9f75844SAndroid Build Coastguard Worker class DcSctpSocketInterface {
494*d9f75844SAndroid Build Coastguard Worker public:
495*d9f75844SAndroid Build Coastguard Worker virtual ~DcSctpSocketInterface() = default;
496*d9f75844SAndroid Build Coastguard Worker
497*d9f75844SAndroid Build Coastguard Worker // To be called when an incoming SCTP packet is to be processed.
498*d9f75844SAndroid Build Coastguard Worker virtual void ReceivePacket(rtc::ArrayView<const uint8_t> data) = 0;
499*d9f75844SAndroid Build Coastguard Worker
500*d9f75844SAndroid Build Coastguard Worker // To be called when a timeout has expired. The `timeout_id` is provided
501*d9f75844SAndroid Build Coastguard Worker // when the timeout was initiated.
502*d9f75844SAndroid Build Coastguard Worker virtual void HandleTimeout(TimeoutID timeout_id) = 0;
503*d9f75844SAndroid Build Coastguard Worker
504*d9f75844SAndroid Build Coastguard Worker // Connects the socket. This is an asynchronous operation, and
505*d9f75844SAndroid Build Coastguard Worker // `DcSctpSocketCallbacks::OnConnected` will be called on success.
506*d9f75844SAndroid Build Coastguard Worker virtual void Connect() = 0;
507*d9f75844SAndroid Build Coastguard Worker
508*d9f75844SAndroid Build Coastguard Worker // Puts this socket to the state in which the original socket was when its
509*d9f75844SAndroid Build Coastguard Worker // `DcSctpSocketHandoverState` was captured by `GetHandoverStateAndClose`.
510*d9f75844SAndroid Build Coastguard Worker // `RestoreFromState` is allowed only on the closed socket.
511*d9f75844SAndroid Build Coastguard Worker // `DcSctpSocketCallbacks::OnConnected` will be called if a connected socket
512*d9f75844SAndroid Build Coastguard Worker // state is restored.
513*d9f75844SAndroid Build Coastguard Worker // `DcSctpSocketCallbacks::OnError` will be called on error.
514*d9f75844SAndroid Build Coastguard Worker virtual void RestoreFromState(const DcSctpSocketHandoverState& state) = 0;
515*d9f75844SAndroid Build Coastguard Worker
516*d9f75844SAndroid Build Coastguard Worker // Gracefully shutdowns the socket and sends all outstanding data. This is an
517*d9f75844SAndroid Build Coastguard Worker // asynchronous operation and `DcSctpSocketCallbacks::OnClosed` will be called
518*d9f75844SAndroid Build Coastguard Worker // on success.
519*d9f75844SAndroid Build Coastguard Worker virtual void Shutdown() = 0;
520*d9f75844SAndroid Build Coastguard Worker
521*d9f75844SAndroid Build Coastguard Worker // Closes the connection non-gracefully. Will send ABORT if the connection is
522*d9f75844SAndroid Build Coastguard Worker // not already closed. No callbacks will be made after Close() has returned.
523*d9f75844SAndroid Build Coastguard Worker virtual void Close() = 0;
524*d9f75844SAndroid Build Coastguard Worker
525*d9f75844SAndroid Build Coastguard Worker // The socket state.
526*d9f75844SAndroid Build Coastguard Worker virtual SocketState state() const = 0;
527*d9f75844SAndroid Build Coastguard Worker
528*d9f75844SAndroid Build Coastguard Worker // The options it was created with.
529*d9f75844SAndroid Build Coastguard Worker virtual const DcSctpOptions& options() const = 0;
530*d9f75844SAndroid Build Coastguard Worker
531*d9f75844SAndroid Build Coastguard Worker // Update the options max_message_size.
532*d9f75844SAndroid Build Coastguard Worker virtual void SetMaxMessageSize(size_t max_message_size) = 0;
533*d9f75844SAndroid Build Coastguard Worker
534*d9f75844SAndroid Build Coastguard Worker // Sets the priority of an outgoing stream. The initial value, when not set,
535*d9f75844SAndroid Build Coastguard Worker // is `DcSctpOptions::default_stream_priority`.
536*d9f75844SAndroid Build Coastguard Worker virtual void SetStreamPriority(StreamID stream_id,
537*d9f75844SAndroid Build Coastguard Worker StreamPriority priority) = 0;
538*d9f75844SAndroid Build Coastguard Worker
539*d9f75844SAndroid Build Coastguard Worker // Returns the currently set priority for an outgoing stream. The initial
540*d9f75844SAndroid Build Coastguard Worker // value, when not set, is `DcSctpOptions::default_stream_priority`.
541*d9f75844SAndroid Build Coastguard Worker virtual StreamPriority GetStreamPriority(StreamID stream_id) const = 0;
542*d9f75844SAndroid Build Coastguard Worker
543*d9f75844SAndroid Build Coastguard Worker // Sends the message `message` using the provided send options.
544*d9f75844SAndroid Build Coastguard Worker // Sending a message is an asynchronous operation, and the `OnError` callback
545*d9f75844SAndroid Build Coastguard Worker // may be invoked to indicate any errors in sending the message.
546*d9f75844SAndroid Build Coastguard Worker //
547*d9f75844SAndroid Build Coastguard Worker // The association does not have to be established before calling this method.
548*d9f75844SAndroid Build Coastguard Worker // If it's called before there is an established association, the message will
549*d9f75844SAndroid Build Coastguard Worker // be queued.
550*d9f75844SAndroid Build Coastguard Worker virtual SendStatus Send(DcSctpMessage message,
551*d9f75844SAndroid Build Coastguard Worker const SendOptions& send_options) = 0;
552*d9f75844SAndroid Build Coastguard Worker
553*d9f75844SAndroid Build Coastguard Worker // Resetting streams is an asynchronous operation and the results will
554*d9f75844SAndroid Build Coastguard Worker // be notified using `DcSctpSocketCallbacks::OnStreamsResetDone()` on success
555*d9f75844SAndroid Build Coastguard Worker // and `DcSctpSocketCallbacks::OnStreamsResetFailed()` on failure. Note that
556*d9f75844SAndroid Build Coastguard Worker // only outgoing streams can be reset.
557*d9f75844SAndroid Build Coastguard Worker //
558*d9f75844SAndroid Build Coastguard Worker // When it's known that the peer has reset its own outgoing streams,
559*d9f75844SAndroid Build Coastguard Worker // `DcSctpSocketCallbacks::OnIncomingStreamReset` is called.
560*d9f75844SAndroid Build Coastguard Worker //
561*d9f75844SAndroid Build Coastguard Worker // Note that resetting a stream will also remove all queued messages on those
562*d9f75844SAndroid Build Coastguard Worker // streams, but will ensure that the currently sent message (if any) is fully
563*d9f75844SAndroid Build Coastguard Worker // sent before closing the stream.
564*d9f75844SAndroid Build Coastguard Worker //
565*d9f75844SAndroid Build Coastguard Worker // Resetting streams can only be done on an established association that
566*d9f75844SAndroid Build Coastguard Worker // supports stream resetting. Calling this method on e.g. a closed association
567*d9f75844SAndroid Build Coastguard Worker // or streams that don't support resetting will not perform any operation.
568*d9f75844SAndroid Build Coastguard Worker virtual ResetStreamsStatus ResetStreams(
569*d9f75844SAndroid Build Coastguard Worker rtc::ArrayView<const StreamID> outgoing_streams) = 0;
570*d9f75844SAndroid Build Coastguard Worker
571*d9f75844SAndroid Build Coastguard Worker // Returns the number of bytes of data currently queued to be sent on a given
572*d9f75844SAndroid Build Coastguard Worker // stream.
573*d9f75844SAndroid Build Coastguard Worker virtual size_t buffered_amount(StreamID stream_id) const = 0;
574*d9f75844SAndroid Build Coastguard Worker
575*d9f75844SAndroid Build Coastguard Worker // Returns the number of buffered outgoing bytes that is considered "low" for
576*d9f75844SAndroid Build Coastguard Worker // a given stream. See `SetBufferedAmountLowThreshold`.
577*d9f75844SAndroid Build Coastguard Worker virtual size_t buffered_amount_low_threshold(StreamID stream_id) const = 0;
578*d9f75844SAndroid Build Coastguard Worker
579*d9f75844SAndroid Build Coastguard Worker // Used to specify the number of bytes of buffered outgoing data that is
580*d9f75844SAndroid Build Coastguard Worker // considered "low" for a given stream, which will trigger an
581*d9f75844SAndroid Build Coastguard Worker // OnBufferedAmountLow event. The default value is zero (0).
582*d9f75844SAndroid Build Coastguard Worker virtual void SetBufferedAmountLowThreshold(StreamID stream_id,
583*d9f75844SAndroid Build Coastguard Worker size_t bytes) = 0;
584*d9f75844SAndroid Build Coastguard Worker
585*d9f75844SAndroid Build Coastguard Worker // Retrieves the latest metrics. If the socket is not fully connected,
586*d9f75844SAndroid Build Coastguard Worker // `absl::nullopt` will be returned.
587*d9f75844SAndroid Build Coastguard Worker virtual absl::optional<Metrics> GetMetrics() const = 0;
588*d9f75844SAndroid Build Coastguard Worker
589*d9f75844SAndroid Build Coastguard Worker // Returns empty bitmask if the socket is in the state in which a snapshot of
590*d9f75844SAndroid Build Coastguard Worker // the state can be made by `GetHandoverStateAndClose()`. Return value is
591*d9f75844SAndroid Build Coastguard Worker // invalidated by a call to any non-const method.
592*d9f75844SAndroid Build Coastguard Worker virtual HandoverReadinessStatus GetHandoverReadiness() const = 0;
593*d9f75844SAndroid Build Coastguard Worker
594*d9f75844SAndroid Build Coastguard Worker // Collects a snapshot of the socket state that can be used to reconstruct
595*d9f75844SAndroid Build Coastguard Worker // this socket in another process. On success this socket object is closed
596*d9f75844SAndroid Build Coastguard Worker // synchronously and no callbacks will be made after the method has returned.
597*d9f75844SAndroid Build Coastguard Worker // The method fails if the socket is not in a state ready for handover.
598*d9f75844SAndroid Build Coastguard Worker // nullopt indicates the failure. `DcSctpSocketCallbacks::OnClosed` will be
599*d9f75844SAndroid Build Coastguard Worker // called on success.
600*d9f75844SAndroid Build Coastguard Worker virtual absl::optional<DcSctpSocketHandoverState>
601*d9f75844SAndroid Build Coastguard Worker GetHandoverStateAndClose() = 0;
602*d9f75844SAndroid Build Coastguard Worker
603*d9f75844SAndroid Build Coastguard Worker // Returns the detected SCTP implementation of the peer. As this is not
604*d9f75844SAndroid Build Coastguard Worker // explicitly signalled during the connection establishment, heuristics is
605*d9f75844SAndroid Build Coastguard Worker // used to analyze e.g. the state cookie in the INIT-ACK chunk.
606*d9f75844SAndroid Build Coastguard Worker //
607*d9f75844SAndroid Build Coastguard Worker // If this method is called too early (before
608*d9f75844SAndroid Build Coastguard Worker // `DcSctpSocketCallbacks::OnConnected` has triggered), this will likely
609*d9f75844SAndroid Build Coastguard Worker // return `SctpImplementation::kUnknown`.
610*d9f75844SAndroid Build Coastguard Worker ABSL_DEPRECATED("See Metrics::peer_implementation instead")
peer_implementation()611*d9f75844SAndroid Build Coastguard Worker virtual SctpImplementation peer_implementation() const {
612*d9f75844SAndroid Build Coastguard Worker return SctpImplementation::kUnknown;
613*d9f75844SAndroid Build Coastguard Worker }
614*d9f75844SAndroid Build Coastguard Worker };
615*d9f75844SAndroid Build Coastguard Worker } // namespace dcsctp
616*d9f75844SAndroid Build Coastguard Worker
617*d9f75844SAndroid Build Coastguard Worker #endif // NET_DCSCTP_PUBLIC_DCSCTP_SOCKET_H_
618