| 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/timer/timer.h" |
| 11 |
| 12 | #include <algorithm> |
| 13 | #include <cstdint> |
| 14 | #include <limits> |
| 15 | #include <memory> |
| 16 | #include <utility> |
| 17 |
| 18 | #include "absl/memory/memory.h" |
| 19 | #include "absl/strings/string_view.h" |
| 20 | #include "net/dcsctp/public/timeout.h" |
| 21 | #include "rtc_base/checks.h" |
| 22 |
| 23 | namespace dcsctp { |
| 24 | namespace { |
| 25 | using ::webrtc::TimeDelta; |
| 26 |
| 27 | TimeoutID MakeTimeoutId(TimerID timer_id, TimerGeneration generation) { |
| 28 | return TimeoutID(static_cast<uint64_t>(*timer_id) << 32 | *generation); |
| 29 | } |
| 30 |
| 31 | TimeDelta GetBackoffDuration(const TimerOptions& options, |
| 32 | TimeDelta base_duration, |
| 33 | int expiration_count) { |
| 34 | switch (options.backoff_algorithm) { |
| 35 | case TimerBackoffAlgorithm::kFixed: |
| 36 | return base_duration; |
| 37 | case TimerBackoffAlgorithm::kExponential: { |
| 38 | TimeDelta duration = base_duration; |
| 39 |
| 40 | while (expiration_count > 0 && duration < Timer::kMaxTimerDuration) { |
| 41 | duration = duration * 2; |
| 42 | --expiration_count; |
| 43 |
| 44 | if (duration > options.max_backoff_duration) { |
| 45 | return options.max_backoff_duration; |
| 46 | } |
| 47 | } |
| 48 |
| 49 | return TimeDelta(std::min(duration, Timer::kMaxTimerDuration)); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | } // namespace |
| 54 |
| 55 | constexpr TimeDelta Timer::kMaxTimerDuration; |
| 56 |
| 57 | Timer::Timer(TimerID id, |
| 58 | absl::string_view name, |
| 59 | OnExpired on_expired, |
| 60 | UnregisterHandler unregister_handler, |
| 61 | std::unique_ptr<Timeout> timeout, |
| 62 | const TimerOptions& options) |
| 63 | : id_(id), |
| 64 | name_(name), |
| 65 | options_(options), |
| 66 | on_expired_(std::move(on_expired)), |
| 67 | unregister_handler_(std::move(unregister_handler)), |
| 68 | timeout_(std::move(timeout)), |
| 69 | duration_(options.duration) {} |
| 70 |
| 71 | Timer::~Timer() { |
| 72 | Stop(); |
| 73 | unregister_handler_(); |
| 74 | } |
| 75 |
| 76 | void Timer::Start() { |
| 77 | expiration_count_ = 0; |
| 78 | if (!is_running()) { |
| 79 | is_running_ = true; |
| 80 | generation_ = TimerGeneration(*generation_ + 1); |
| 81 | timeout_->Start(DurationMs(duration_), MakeTimeoutId(id_, generation_)); |
| 82 | } else { |
| 83 | // Timer was running - stop and restart it, to make it expire in `duration_` |
| 84 | // from now. |
| 85 | generation_ = TimerGeneration(*generation_ + 1); |
| 86 | timeout_->Restart(DurationMs(duration_), MakeTimeoutId(id_, generation_)); |
| 87 | } |
| 88 | } |
| 89 |
| 90 | void Timer::Stop() { |
| 91 | if (is_running()) { |
| 92 | timeout_->Stop(); |
| 93 | expiration_count_ = 0; |
| 94 | is_running_ = false; |
| 95 | } |
| 96 | } |
| 97 |
| 98 | void Timer::Trigger(TimerGeneration generation) { |
| 99 | if (is_running_ && generation == generation_) { |
| 100 | ++expiration_count_; |
| 101 | is_running_ = false; |
| 102 | if (!options_.max_restarts.has_value() || |
| 103 | expiration_count_ <= *options_.max_restarts) { |
| 104 | // The timer should still be running after this triggers. Start a new |
| 105 | // timer. Note that it might be very quickly restarted again, if the |
| 106 | // `on_expired_` callback returns a new duration. |
| 107 | is_running_ = true; |
| 108 | TimeDelta duration = |
| 109 | GetBackoffDuration(options_, duration_, expiration_count_); |
| 110 | generation_ = TimerGeneration(*generation_ + 1); |
| 111 | timeout_->Start(DurationMs(duration), MakeTimeoutId(id_, generation_)); |
| 112 | } |
| 113 |
| 114 | TimeDelta new_duration = on_expired_(); |
| 115 | RTC_DCHECK(new_duration != TimeDelta::PlusInfinity()); |
| 116 | if (new_duration > TimeDelta::Zero() && new_duration != duration_) { |
| 117 | duration_ = new_duration; |
| 118 | if (is_running_) { |
| 119 | // Restart it with new duration. |
| 120 | timeout_->Stop(); |
| 121 |
| 122 | TimeDelta duration = |
| 123 | GetBackoffDuration(options_, duration_, expiration_count_); |
| 124 | generation_ = TimerGeneration(*generation_ + 1); |
| 125 | timeout_->Start(DurationMs(duration), MakeTimeoutId(id_, generation_)); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 |
| 131 | void TimerManager::HandleTimeout(TimeoutID timeout_id) { |
| 132 | TimerID timer_id(*timeout_id >> 32); |
| 133 | TimerGeneration generation(*timeout_id); |
| 134 | auto it = timers_.find(timer_id); |
| 135 | if (it != timers_.end()) { |
| 136 | it->second->Trigger(generation); |
| 137 | } |
| 138 | } |
| 139 |
| 140 | std::unique_ptr<Timer> TimerManager::CreateTimer(absl::string_view name, |
| 141 | Timer::OnExpired on_expired, |
| 142 | const TimerOptions& options) { |
| 143 | next_id_ = TimerID(*next_id_ + 1); |
| 144 | TimerID id = next_id_; |
| 145 | // This would overflow after 4 billion timers created, which in SCTP would be |
| 146 | // after 800 million reconnections on a single socket. Ensure this will never |
| 147 | // happen. |
| 148 | RTC_CHECK_NE(*id, std::numeric_limits<uint32_t>::max()); |
| 149 | std::unique_ptr<Timeout> timeout = create_timeout_(options.precision); |
| 150 | RTC_CHECK(timeout != nullptr); |
| 151 | auto timer = absl::WrapUnique(new Timer( |
| 152 | id, name, std::move(on_expired), [this, id]() { timers_.erase(id); }, |
| 153 | std::move(timeout), options)); |
| 154 | timers_[id] = timer.get(); |
| 155 | return timer; |
| 156 | } |
| 157 |
| 158 | } // namespace dcsctp |
| 159 |