[Unwrap] Migrate dcsctp sequence numbers to SeqNumUnwrapper

Bug: webrtc:13982
Change-Id: Ic900a967d1b8e96a2b1ec99424674ccb33eb7165
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/288940
Commit-Queue: Harald Alvestrand <hta@webrtc.org>
Reviewed-by: Victor Boivie <boivie@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Auto-Submit: Evan Shrubsole <eshr@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#39084}
This commit is contained in:
Evan Shrubsole 2023-01-09 13:28:53 +00:00 committed by WebRTC LUCI CQ
parent 17f783eee8
commit 4387ad6cdc
5 changed files with 11 additions and 59 deletions

View File

@ -22,7 +22,10 @@ rtc_source_set("math") {
} }
rtc_source_set("sequence_numbers") { rtc_source_set("sequence_numbers") {
deps = [ ":internal_types" ] deps = [
":internal_types",
"../../../rtc_base:rtc_numerics",
]
sources = [ "sequence_numbers.h" ] sources = [ "sequence_numbers.h" ]
} }

View File

@ -15,6 +15,7 @@
#include <utility> #include <utility>
#include "net/dcsctp/common/internal_types.h" #include "net/dcsctp/common/internal_types.h"
#include "rtc_base/numerics/sequence_number_unwrapper.h"
namespace dcsctp { namespace dcsctp {
@ -42,7 +43,7 @@ class UnwrappedSequenceNumber {
// unwrapped ones. // unwrapped ones.
class Unwrapper { class Unwrapper {
public: public:
Unwrapper() : largest_(kValueLimit) {} Unwrapper() = default;
Unwrapper(const Unwrapper&) = default; Unwrapper(const Unwrapper&) = default;
Unwrapper& operator=(const Unwrapper&) = default; Unwrapper& operator=(const Unwrapper&) = default;
@ -53,40 +54,21 @@ class UnwrappedSequenceNumber {
// This will also update the Unwrapper's state, to track the last seen // This will also update the Unwrapper's state, to track the last seen
// largest value. // largest value.
UnwrappedSequenceNumber<WrappedType> Unwrap(WrappedType value) { UnwrappedSequenceNumber<WrappedType> Unwrap(WrappedType value) {
WrappedType wrapped_largest = return UnwrappedSequenceNumber<WrappedType>(unwrapper_.Unwrap(*value));
static_cast<WrappedType>(largest_ % kValueLimit);
int64_t result = largest_ + Delta(value, wrapped_largest);
if (largest_ < result) {
largest_ = result;
}
return UnwrappedSequenceNumber<WrappedType>(result);
} }
// Similar to `Unwrap`, but will not update the Unwrappers's internal state. // Similar to `Unwrap`, but will not update the Unwrappers's internal state.
UnwrappedSequenceNumber<WrappedType> PeekUnwrap(WrappedType value) const { UnwrappedSequenceNumber<WrappedType> PeekUnwrap(WrappedType value) const {
WrappedType uint32_largest = return UnwrappedSequenceNumber<WrappedType>(
static_cast<WrappedType>(largest_ % kValueLimit); unwrapper_.PeekUnwrap(*value));
int64_t result = largest_ + Delta(value, uint32_largest);
return UnwrappedSequenceNumber<WrappedType>(result);
} }
// Resets the Unwrapper to its pristine state. Used when a sequence number // Resets the Unwrapper to its pristine state. Used when a sequence number
// is to be reset to zero. // is to be reset to zero.
void Reset() { largest_ = kValueLimit; } void Reset() { unwrapper_.Reset(); }
private: private:
static int64_t Delta(WrappedType value, WrappedType prev_value) { webrtc::SeqNumUnwrapper<typename WrappedType::UnderlyingType> unwrapper_;
static constexpr typename WrappedType::UnderlyingType kBreakpoint =
kValueLimit / 2;
typename WrappedType::UnderlyingType diff = *value - *prev_value;
diff %= kValueLimit;
if (diff < kBreakpoint) {
return static_cast<int64_t>(diff);
}
return static_cast<int64_t>(diff) - kValueLimit;
}
int64_t largest_;
}; };
// Returns the wrapped value this type represents. // Returns the wrapped value this type represents.

View File

@ -2140,7 +2140,6 @@ if (rtc_include_tests) {
":strong_alias", ":strong_alias",
":timeutils", ":timeutils",
"../modules:module_api_public", "../modules:module_api_public",
"../net/dcsctp/common:sequence_numbers",
"../test:test_main", "../test:test_main",
"../test:test_support", "../test:test_support",
] ]

View File

@ -12,7 +12,4 @@ specific_include_rules = {
"gunit\.h": [ "gunit\.h": [
"+testing/base/public/gunit.h" "+testing/base/public/gunit.h"
], ],
"sequence_numbers_conformance_test\.cc": [
"+net/dcsctp/common/sequence_numbers.h",
],
} }

View File

@ -13,9 +13,7 @@
#include <type_traits> #include <type_traits>
#include "modules/include/module_common_types_public.h" #include "modules/include/module_common_types_public.h"
#include "net/dcsctp/common/sequence_numbers.h"
#include "rtc_base/numerics/sequence_number_unwrapper.h" #include "rtc_base/numerics/sequence_number_unwrapper.h"
#include "rtc_base/strong_alias.h"
#include "rtc_base/time_utils.h" #include "rtc_base/time_utils.h"
#include "test/gmock.h" #include "test/gmock.h"
#include "test/gtest.h" #include "test/gtest.h"
@ -25,27 +23,6 @@ namespace {
using ::testing::Test; using ::testing::Test;
using dcsctp::UnwrappedSequenceNumber;
using Wrapped = StrongAlias<class WrappedTag, uint32_t>;
using TestSequence = UnwrappedSequenceNumber<Wrapped>;
template <typename T>
class UnwrapperHelper;
template <>
class UnwrapperHelper<TestSequence::Unwrapper> {
public:
int64_t Unwrap(uint32_t val) {
TestSequence s = unwrapper_.Unwrap(Wrapped(val));
// UnwrappedSequenceNumber starts counting at 2^32.
constexpr int64_t kDcsctpUnwrapStart = int64_t{1} << 32;
return s.value() - kDcsctpUnwrapStart;
}
private:
TestSequence::Unwrapper unwrapper_;
};
// MaxVal is the max of the wrapped space, ie MaxVal + 1 = 0 when wrapped. // MaxVal is the max of the wrapped space, ie MaxVal + 1 = 0 when wrapped.
template <typename U, int64_t MaxVal = std::numeric_limits<uint32_t>::max()> template <typename U, int64_t MaxVal = std::numeric_limits<uint32_t>::max()>
struct FixtureParams { struct FixtureParams {
@ -138,8 +115,6 @@ TYPED_TEST_P(UnwrapperConformanceFixture, MultipleNegativeWrapArounds) {
// SequenceNumberUnwrapper can only wrap negative once. // SequenceNumberUnwrapper can only wrap negative once.
// rtc::TimestampWrapAroundHandler does not wrap around correctly. // rtc::TimestampWrapAroundHandler does not wrap around correctly.
if constexpr (std::is_same<UnwrapperT, TimestampUnwrapper>() || if constexpr (std::is_same<UnwrapperT, TimestampUnwrapper>() ||
std::is_same<UnwrapperT,
UnwrapperHelper<TestSequence::Unwrapper>>() ||
std::is_same<UnwrapperT, rtc::TimestampWrapAroundHandler>()) { std::is_same<UnwrapperT, rtc::TimestampWrapAroundHandler>()) {
return; return;
} }
@ -166,7 +141,6 @@ using UnwrapperTypes = ::testing::Types<
FixtureParams<rtc::TimestampWrapAroundHandler>, FixtureParams<rtc::TimestampWrapAroundHandler>,
FixtureParams<TimestampUnwrapper>, FixtureParams<TimestampUnwrapper>,
FixtureParams<RtpTimestampUnwrapper>, FixtureParams<RtpTimestampUnwrapper>,
FixtureParams<UnwrapperHelper<TestSequence::Unwrapper>>,
// SeqNumUnwrapper supports arbitrary limits. // SeqNumUnwrapper supports arbitrary limits.
FixtureParams<SeqNumUnwrapper<uint32_t, k15BitMax + 1>, k15BitMax>>; FixtureParams<SeqNumUnwrapper<uint32_t, k15BitMax + 1>, k15BitMax>>;
@ -185,9 +159,6 @@ class TestNames {
if constexpr (std::is_same<typename T::Unwrapper, if constexpr (std::is_same<typename T::Unwrapper,
SeqNumUnwrapper<uint32_t, k15BitMax + 1>>()) SeqNumUnwrapper<uint32_t, k15BitMax + 1>>())
return "SeqNumUnwrapper15bit"; return "SeqNumUnwrapper15bit";
if constexpr (std::is_same<typename T::Unwrapper,
UnwrapperHelper<TestSequence::Unwrapper>>())
return "UnwrappedSequenceNumber";
} }
}; };