xref: /aosp_15_r20/external/webrtc/net/dcsctp/tx/retransmission_error_counter.h (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 #ifndef NET_DCSCTP_TX_RETRANSMISSION_ERROR_COUNTER_H_
11 #define NET_DCSCTP_TX_RETRANSMISSION_ERROR_COUNTER_H_
12 
13 #include <functional>
14 #include <string>
15 #include <utility>
16 
17 #include "absl/strings/string_view.h"
18 #include "net/dcsctp/public/dcsctp_options.h"
19 
20 namespace dcsctp {
21 
22 // The RetransmissionErrorCounter is a simple counter with a limit, and when
23 // the limit is exceeded, the counter is exhausted and the connection will
24 // be closed. It's incremented on retransmission errors, such as the T3-RTX
25 // timer expiring, but also missing heartbeats and stream reset requests.
26 class RetransmissionErrorCounter {
27  public:
RetransmissionErrorCounter(absl::string_view log_prefix,const DcSctpOptions & options)28   RetransmissionErrorCounter(absl::string_view log_prefix,
29                              const DcSctpOptions& options)
30       : log_prefix_(std::string(log_prefix) + "rtx-errors: "),
31         limit_(options.max_retransmissions) {}
32 
33   // Increments the retransmission timer. If the maximum error count has been
34   // reached, `false` will be returned.
35   bool Increment(absl::string_view reason);
IsExhausted()36   bool IsExhausted() const { return limit_.has_value() && counter_ > *limit_; }
37 
38   // Clears the retransmission errors.
39   void Clear();
40 
41   // Returns its current value
value()42   int value() const { return counter_; }
43 
44  private:
45   const std::string log_prefix_;
46   const absl::optional<int> limit_;
47   int counter_ = 0;
48 };
49 }  // namespace dcsctp
50 
51 #endif  // NET_DCSCTP_TX_RETRANSMISSION_ERROR_COUNTER_H_
52