Victor Boivie | f8476cc | 2021-04-05 19:53:56 | [diff] [blame] | 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: |
| 28 | RetransmissionErrorCounter(absl::string_view log_prefix, |
| 29 | const DcSctpOptions& options) |
Victor Boivie | 4fbf555 | 2022-09-09 10:25:32 | [diff] [blame] | 30 | : log_prefix_(log_prefix), limit_(options.max_retransmissions) {} |
Victor Boivie | f8476cc | 2021-04-05 19:53:56 | [diff] [blame] | 31 | |
| 32 | // Increments the retransmission timer. If the maximum error count has been |
| 33 | // reached, `false` will be returned. |
| 34 | bool Increment(absl::string_view reason); |
Victor Boivie | 9680d29 | 2021-08-30 08:23:49 | [diff] [blame] | 35 | bool IsExhausted() const { return limit_.has_value() && counter_ > *limit_; } |
Victor Boivie | f8476cc | 2021-04-05 19:53:56 | [diff] [blame] | 36 | |
| 37 | // Clears the retransmission errors. |
| 38 | void Clear(); |
| 39 | |
| 40 | // Returns its current value |
| 41 | int value() const { return counter_; } |
| 42 | |
| 43 | private: |
Victor Boivie | 4fbf555 | 2022-09-09 10:25:32 | [diff] [blame] | 44 | const absl::string_view log_prefix_; |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 45 | const std::optional<int> limit_; |
Victor Boivie | f8476cc | 2021-04-05 19:53:56 | [diff] [blame] | 46 | int counter_ = 0; |
| 47 | }; |
| 48 | } // namespace dcsctp |
| 49 | |
| 50 | #endif // NET_DCSCTP_TX_RETRANSMISSION_ERROR_COUNTER_H_ |