webrtc_m130/net/dcsctp/common/sequence_numbers.h
Victor Boivie a865519e17 dcsctp: Add strong typed identifiers
There are numerous identifiers and sequences in SCTP, all of them being
unsigned 16 or 32-bit integers.

  * Stream identifiers
  * Payload Protocol Identifier (PPID)
  * Stream Sequence Numbers (SSN)
  * Message Identifiers (MID)
  * Fragment Sequence Numbers (FSN)
  * Transmission Sequence Numbers (TSN)

The first two of these are publicly exposed in the API, and the
remaining ones are never exposed to the client and are all part of SCTP
protocol.

Then there are some more not as common sequence numbers, and some
booleans. Not all will be in internal_types.h - it depends on if they
can be scoped to a specific component instead. And not all types will
likely become strong types.

The unwrapped sequence numbers have been renamed to not cause conflicts
and the current UnwrappedSequenceNumber class doesn't support wrapping
strongly typed integers as it can't reach into the type of the
underlying integer. That's something to explore later.

Bug: webrtc:12614
Change-Id: I4e0016be26d5d4826783d6e0962044f56cbfa97d
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/213422
Commit-Queue: Victor Boivie <boivie@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Reviewed-by: Tommi <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#33620}
2021-04-02 21:38:13 +00:00

151 lines
5.3 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_COMMON_SEQUENCE_NUMBERS_H_
#define NET_DCSCTP_COMMON_SEQUENCE_NUMBERS_H_
#include <cstdint>
#include <limits>
#include <utility>
namespace dcsctp {
// UnwrappedSequenceNumber handles wrapping sequence numbers and unwraps them to
// an int64_t value space, to allow wrapped sequence numbers to be easily
// compared for ordering.
//
// Sequence numbers are expected to be monotonically increasing, but they do not
// need to be unwrapped in order, as long as the difference to the previous one
// is not larger than half the range of the wrapped sequence number.
template <typename WrappedType>
class UnwrappedSequenceNumber {
public:
static_assert(!std::numeric_limits<WrappedType>::is_signed,
"The wrapped type must be unsigned");
static_assert(std::numeric_limits<WrappedType>::max() <
std::numeric_limits<int64_t>::max(),
"The wrapped type must be less than the int64_t value space");
// The unwrapper is a sort of factory and converts wrapped sequence numbers to
// unwrapped ones.
class Unwrapper {
public:
Unwrapper() : largest_(kValueLimit) {}
Unwrapper(const Unwrapper&) = default;
Unwrapper& operator=(const Unwrapper&) = default;
// Given a wrapped `value`, and with knowledge of its current last seen
// largest number, will return a value that can be compared using normal
// operators, such as less-than, greater-than etc.
//
// This will also update the Unwrapper's state, to track the last seen
// largest value.
UnwrappedSequenceNumber<WrappedType> Unwrap(WrappedType value) {
WrappedType wrapped_largest =
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.
UnwrappedSequenceNumber<WrappedType> PeekUnwrap(WrappedType value) const {
WrappedType uint32_largest =
static_cast<WrappedType>(largest_ % kValueLimit);
int64_t result = largest_ + Delta(value, uint32_largest);
return UnwrappedSequenceNumber<WrappedType>(result);
}
// Resets the Unwrapper to its pristine state. Used when a sequence number
// is to be reset to zero.
void Reset() { largest_ = kValueLimit; }
private:
static int64_t Delta(WrappedType value, WrappedType prev_value) {
static constexpr WrappedType kBreakpoint = kValueLimit / 2;
WrappedType 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.
WrappedType Wrap() const {
return static_cast<WrappedType>(value_ % kValueLimit);
}
template <typename H>
friend H AbslHashValue(H state,
const UnwrappedSequenceNumber<WrappedType>& hash) {
return H::combine(std::move(state), hash.value_);
}
bool operator==(const UnwrappedSequenceNumber<WrappedType>& other) const {
return value_ == other.value_;
}
bool operator!=(const UnwrappedSequenceNumber<WrappedType>& other) const {
return value_ != other.value_;
}
bool operator<(const UnwrappedSequenceNumber<WrappedType>& other) const {
return value_ < other.value_;
}
bool operator>(const UnwrappedSequenceNumber<WrappedType>& other) const {
return value_ > other.value_;
}
bool operator>=(const UnwrappedSequenceNumber<WrappedType>& other) const {
return value_ >= other.value_;
}
bool operator<=(const UnwrappedSequenceNumber<WrappedType>& other) const {
return value_ <= other.value_;
}
// Increments the value.
void Increment() { ++value_; }
UnwrappedSequenceNumber<WrappedType> next_value() const {
return UnwrappedSequenceNumber<WrappedType>(value_ + 1);
}
// Adds a delta to the current value.
UnwrappedSequenceNumber<WrappedType> AddTo(int delta) const {
return UnwrappedSequenceNumber<WrappedType>(value_ + delta);
}
// Compares the difference between two sequence numbers.
WrappedType Difference(UnwrappedSequenceNumber<WrappedType> other) const {
return value_ - other.value_;
}
private:
explicit UnwrappedSequenceNumber(int64_t value) : value_(value) {}
static constexpr int64_t kValueLimit =
static_cast<int64_t>(1) << std::numeric_limits<WrappedType>::digits;
int64_t value_;
};
// Unwrapped Transmission Sequence Numbers (TSN)
using UnwrappedTSN = UnwrappedSequenceNumber<uint32_t>;
// Unwrapped Stream Sequence Numbers (SSN)
using UnwrappedSSN = UnwrappedSequenceNumber<uint16_t>;
// Unwrapped Message Identifier (MID)
using UnwrappedMID = UnwrappedSequenceNumber<uint32_t>;
} // namespace dcsctp
#endif // NET_DCSCTP_COMMON_SEQUENCE_NUMBERS_H_