blob: 589e66b472f92a24b6abdee4ccb7dcf88f9fb5b4 [file] [log] [blame]
Victor Boivief8476cc2021-04-05 19:53:561/*
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
20namespace 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.
26class RetransmissionErrorCounter {
27 public:
28 RetransmissionErrorCounter(absl::string_view log_prefix,
29 const DcSctpOptions& options)
Victor Boivie4fbf5552022-09-09 10:25:3230 : log_prefix_(log_prefix), limit_(options.max_retransmissions) {}
Victor Boivief8476cc2021-04-05 19:53:5631
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 Boivie9680d292021-08-30 08:23:4935 bool IsExhausted() const { return limit_.has_value() && counter_ > *limit_; }
Victor Boivief8476cc2021-04-05 19:53:5636
37 // Clears the retransmission errors.
38 void Clear();
39
40 // Returns its current value
41 int value() const { return counter_; }
42
43 private:
Victor Boivie4fbf5552022-09-09 10:25:3244 const absl::string_view log_prefix_;
Florent Castelli8037fc62024-08-29 13:00:4045 const std::optional<int> limit_;
Victor Boivief8476cc2021-04-05 19:53:5646 int counter_ = 0;
47};
48} // namespace dcsctp
49
50#endif // NET_DCSCTP_TX_RETRANSMISSION_ERROR_COUNTER_H_