webrtc_m130/net/dcsctp/timer/fake_timeout.h
Victor Boivie 06fbe63cbf dcsctp: Exit deferred stream reset on FORWARD-TSN
https://datatracker.ietf.org/doc/html/rfc6525#section-5.2.2:

E2:  If the Sender's Last Assigned TSN is greater than the cumulative
        acknowledgment point, then the endpoint MUST enter "deferred
        reset processing". ...  until the cumulative
        acknowledgment point reaches the Sender's Last Assigned TSN.

The cumulative acknowledgement point can not only be reached by
receiving DATA chunks, but also by receiving a FORWARD-TSN that
instructs the receiver to skip them. This was only done for DATA and not
for FORWARD-TSN, which is now corrected.

Additionally, an unnecessary implicit sending of SACK after having
received FORWARD-TSN was removed as this is done anyway every time a
packet has been received. This unifies the processing of DATA and
FORWARD-TSN more.

Bug: webrtc:14600
Change-Id: If797d3c46e741074fe05e322d0aebec765a87968
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/321400
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Victor Boivie <boivie@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#40811}
2023-09-26 07:30:24 +00:00

123 lines
3.7 KiB
C++

/*
* Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef NET_DCSCTP_TIMER_FAKE_TIMEOUT_H_
#define NET_DCSCTP_TIMER_FAKE_TIMEOUT_H_
#include <cstdint>
#include <functional>
#include <limits>
#include <memory>
#include <utility>
#include <vector>
#include "absl/types/optional.h"
#include "api/task_queue/task_queue_base.h"
#include "net/dcsctp/public/timeout.h"
#include "net/dcsctp/public/types.h"
#include "rtc_base/checks.h"
#include "rtc_base/containers/flat_set.h"
namespace dcsctp {
// A timeout used in tests.
class FakeTimeout : public Timeout {
public:
FakeTimeout(std::function<TimeMs()> get_time,
std::function<void(FakeTimeout*)> on_delete)
: get_time_(std::move(get_time)), on_delete_(std::move(on_delete)) {}
~FakeTimeout() override { on_delete_(this); }
void Start(DurationMs duration_ms, TimeoutID timeout_id) override {
RTC_DCHECK(expiry_ == TimeMs::InfiniteFuture());
timeout_id_ = timeout_id;
expiry_ = get_time_() + duration_ms;
}
void Stop() override {
RTC_DCHECK(expiry_ != TimeMs::InfiniteFuture());
expiry_ = TimeMs::InfiniteFuture();
}
bool EvaluateHasExpired(TimeMs now) {
if (now >= expiry_) {
expiry_ = TimeMs::InfiniteFuture();
return true;
}
return false;
}
TimeoutID timeout_id() const { return timeout_id_; }
TimeMs expiry() const { return expiry_; }
private:
const std::function<TimeMs()> get_time_;
const std::function<void(FakeTimeout*)> on_delete_;
TimeoutID timeout_id_ = TimeoutID(0);
TimeMs expiry_ = TimeMs::InfiniteFuture();
};
class FakeTimeoutManager {
public:
// The `get_time` function must return the current time, relative to any
// epoch.
explicit FakeTimeoutManager(std::function<TimeMs()> get_time)
: get_time_(std::move(get_time)) {}
std::unique_ptr<FakeTimeout> CreateTimeout() {
auto timer = std::make_unique<FakeTimeout>(
get_time_, [this](FakeTimeout* timer) { timers_.erase(timer); });
timers_.insert(timer.get());
return timer;
}
std::unique_ptr<FakeTimeout> CreateTimeout(
webrtc::TaskQueueBase::DelayPrecision precision) {
// FakeTimeout does not support implement |precision|.
return CreateTimeout();
}
// NOTE: This can't return a vector, as calling EvaluateHasExpired requires
// calling socket->HandleTimeout directly afterwards, as the owning Timer
// still believes it's running, and it needs to be updated to set
// Timer::is_running_ to false before you operate on the Timer or Timeout
// again.
absl::optional<TimeoutID> GetNextExpiredTimeout() {
TimeMs now = get_time_();
std::vector<TimeoutID> expired_timers;
for (auto& timer : timers_) {
if (timer->EvaluateHasExpired(now)) {
return timer->timeout_id();
}
}
return absl::nullopt;
}
DurationMs GetTimeToNextTimeout() const {
TimeMs next_expiry = TimeMs::InfiniteFuture();
for (const FakeTimeout* timer : timers_) {
if (timer->expiry() < next_expiry) {
next_expiry = timer->expiry();
}
}
TimeMs now = get_time_();
return next_expiry != TimeMs::InfiniteFuture() && next_expiry >= now
? next_expiry - now
: DurationMs::InfiniteDuration();
}
private:
const std::function<TimeMs()> get_time_;
webrtc::flat_set<FakeTimeout*> timers_;
};
} // namespace dcsctp
#endif // NET_DCSCTP_TIMER_FAKE_TIMEOUT_H_