Sequence Numbers helper class

This commit is contained in:
Atlante45 2015-07-23 15:00:32 -07:00
parent dce1fc6855
commit c5901c3f6b
2 changed files with 122 additions and 0 deletions

View file

@ -0,0 +1,38 @@
//
// SeqNum.cpp
//
//
// Created by Clement on 7/23/15.
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include "SeqNum.h"
namespace udt {
int seqcmp(const SeqNum& seq1, const SeqNum& seq2) {
return (glm::abs(seq1._value - seq2._value) < SeqNum::THRESHOLD) ? (seq1._value - seq2._value)
: (seq2._value - seq1._value);
}
int seqlen(const SeqNum& seq1, const SeqNum& seq2) {
return (seq1._value <= seq2._value) ? (seq2._value - seq1._value + 1)
: (seq2._value - seq1._value + SeqNum::MAX + 2);
}
int seqoff(const SeqNum& seq1, const SeqNum& seq2) {
if (glm::abs(seq1._value - seq2._value) < SeqNum::THRESHOLD) {
return seq2._value - seq1._value;
}
if (seq1._value < seq2._value) {
return seq2._value - seq1._value - SeqNum::MAX - 1;
}
return seq2._value - seq1._value + SeqNum::MAX + 1;
}
}

View file

@ -0,0 +1,84 @@
//
// SeqNum.h
// libraries/networking/src/udt
//
// Created by Clement on 7/23/15.
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifndef hifi_SeqNum_h
#define hifi_SeqNum_h
#include <glm/detail/func_common.hpp>
namespace udt {
class SeqNum {
public:
// Base type of sequence numbers
using Type = uint32_t;
// Values are for 29 bit SeqNum
static const Type THRESHOLD = 0x0FFFFFFF; // threshold for comparing sequence numbers
static const Type MAX = 0x1FFFFFFF; // maximum sequence number used in UDT
SeqNum() : _value(0) {}
// Only explicit conversions
explicit SeqNum(Type value) { *this = value; }
explicit operator Type() { return _value; }
inline SeqNum& operator++() {
_value = (_value == MAX) ? 0 : ++_value;
return *this;
}
inline SeqNum& operator--() {
_value = (_value == 0) ? MAX : --_value;
return *this;
}
inline SeqNum operator++(int) {
SeqNum before = *this;
(*this)++;
return before;
}
inline SeqNum operator--(int) {
SeqNum before = *this;
(*this)--;
return before;
}
inline SeqNum& operator=(Type value) {
_value = (value <= MAX) ? value : MAX;
return *this;
}
inline SeqNum& operator=(SeqNum& other) {
_value = other._value;
return *this;
}
inline SeqNum& operator+=(Type inc) {
_value = (_value + inc > MAX) ? _value + inc - (MAX + 1) : _value + inc;
return *this;
}
inline SeqNum& operator-=(Type dec) {
_value = (_value < dec) ? MAX - (dec - _value + 1) : _value - dec;
return *this;
}
friend int seqcmp(const SeqNum& seq1, const SeqNum& seq2);
friend int seqlen(const SeqNum& seq1, const SeqNum& seq2);
friend int seqoff(const SeqNum& seq1, const SeqNum& seq2);
private:
Type _value;
};
int seqcmp(const SeqNum& seq1, const SeqNum& seq2);
int seqlen(const SeqNum& seq1, const SeqNum& seq2);
int seqoff(const SeqNum& seq1, const SeqNum& seq2);
}
#endif // hifi_SeqNum_h