xref: /aosp_15_r20/external/webrtc/net/dcsctp/socket/packet_sender.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2021 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 #include "net/dcsctp/socket/packet_sender.h"
11 
12 #include <utility>
13 #include <vector>
14 
15 #include "net/dcsctp/public/types.h"
16 
17 namespace dcsctp {
18 
PacketSender(DcSctpSocketCallbacks & callbacks,std::function<void (rtc::ArrayView<const uint8_t>,SendPacketStatus)> on_sent_packet)19 PacketSender::PacketSender(DcSctpSocketCallbacks& callbacks,
20                            std::function<void(rtc::ArrayView<const uint8_t>,
21                                               SendPacketStatus)> on_sent_packet)
22     : callbacks_(callbacks), on_sent_packet_(std::move(on_sent_packet)) {}
23 
Send(SctpPacket::Builder & builder)24 bool PacketSender::Send(SctpPacket::Builder& builder) {
25   if (builder.empty()) {
26     return false;
27   }
28 
29   std::vector<uint8_t> payload = builder.Build();
30 
31   SendPacketStatus status = callbacks_.SendPacketWithStatus(payload);
32   on_sent_packet_(payload, status);
33   switch (status) {
34     case SendPacketStatus::kSuccess: {
35       return true;
36     }
37     case SendPacketStatus::kTemporaryFailure: {
38       // TODO(boivie): Queue this packet to be retried to be sent later.
39       return false;
40     }
41 
42     case SendPacketStatus::kError: {
43       // Nothing that can be done.
44       return false;
45     }
46   }
47 }
48 }  // namespace dcsctp
49