Replace all uses of float with chrono::duration
This commit is contained in:
parent
f5cfbe41d5
commit
57e270eb15
2 changed files with 41 additions and 39 deletions
|
@ -2,14 +2,9 @@
|
||||||
#define UDPC_DEFINES_HPP
|
#define UDPC_DEFINES_HPP
|
||||||
|
|
||||||
#define UDPC_CONTEXT_IDENTIFIER 0x902F4DB3
|
#define UDPC_CONTEXT_IDENTIFIER 0x902F4DB3
|
||||||
#define UDPC_TIMEOUT_SECONDS 10.0f
|
|
||||||
#define UDPC_GOOD_MODE_SEND_INTERVAL (1.0f / 30.0f)
|
|
||||||
#define UDPC_BAD_MODE_SEND_INTERVAL (1.0f / 10.0f)
|
|
||||||
#define UDPC_SENT_PKTS_MAX_SIZE 33
|
#define UDPC_SENT_PKTS_MAX_SIZE 33
|
||||||
#define UDPC_QUEUED_PKTS_MAX_SIZE 32
|
#define UDPC_QUEUED_PKTS_MAX_SIZE 32
|
||||||
#define UDPC_RECEIVED_PKTS_MAX_SIZE 50
|
#define UDPC_RECEIVED_PKTS_MAX_SIZE 50
|
||||||
#define UDPC_GOOD_RTT_LIMIT_SEC 0.25f
|
|
||||||
#define UDPC_PACKET_TIMEOUT_SEC 1.0f
|
|
||||||
|
|
||||||
#define UDPC_ID_CONNECT 0x80000000
|
#define UDPC_ID_CONNECT 0x80000000
|
||||||
#define UDPC_ID_PING 0x40000000
|
#define UDPC_ID_PING 0x40000000
|
||||||
|
@ -31,10 +26,17 @@
|
||||||
|
|
||||||
namespace UDPC {
|
namespace UDPC {
|
||||||
|
|
||||||
|
static const auto ONE_SECOND = std::chrono::seconds(1);
|
||||||
|
static const auto TEN_SECONDS = std::chrono::seconds(10);
|
||||||
|
|
||||||
static uint32_t LOCAL_ADDR = 0;
|
static uint32_t LOCAL_ADDR = 0;
|
||||||
static const auto INIT_PKT_INTERVAL_DT = std::chrono::seconds(5);
|
static const auto INIT_PKT_INTERVAL_DT = std::chrono::seconds(5);
|
||||||
static const auto HEARTBEAT_PKT_INTERVAL_DT = std::chrono::milliseconds(150);
|
static const auto HEARTBEAT_PKT_INTERVAL_DT = std::chrono::milliseconds(150);
|
||||||
static const auto PACKET_TIMEOUT_TIME = std::chrono::seconds(1);
|
static const auto PACKET_TIMEOUT_TIME = ONE_SECOND;
|
||||||
|
static const auto GOOD_RTT_LIMIT = std::chrono::milliseconds(250);
|
||||||
|
static const auto CONNECTION_TIMEOUT = TEN_SECONDS;
|
||||||
|
static const auto GOOD_MODE_SEND_RATE = std::chrono::microseconds(33333);
|
||||||
|
static const auto BAD_MODE_SEND_RATE = std::chrono::milliseconds(100);
|
||||||
|
|
||||||
// forward declaration
|
// forward declaration
|
||||||
struct Context;
|
struct Context;
|
||||||
|
@ -74,10 +76,10 @@ struct ConnectionData {
|
||||||
uint32_t lseq;
|
uint32_t lseq;
|
||||||
uint32_t rseq;
|
uint32_t rseq;
|
||||||
uint32_t ack;
|
uint32_t ack;
|
||||||
float timer;
|
std::chrono::steady_clock::duration timer;
|
||||||
float toggleT;
|
std::chrono::steady_clock::duration toggleT;
|
||||||
float toggleTimer;
|
std::chrono::steady_clock::duration toggleTimer;
|
||||||
float toggledTimer;
|
std::chrono::steady_clock::duration toggledTimer;
|
||||||
uint32_t addr; // in network order
|
uint32_t addr; // in network order
|
||||||
uint16_t port; // in native order
|
uint16_t port; // in native order
|
||||||
std::deque<UDPC_PacketInfo> sentPkts;
|
std::deque<UDPC_PacketInfo> sentPkts;
|
||||||
|
@ -88,7 +90,7 @@ struct ConnectionData {
|
||||||
std::unordered_map<uint32_t, SentPktInfo::Ptr> sentInfoMap;
|
std::unordered_map<uint32_t, SentPktInfo::Ptr> sentInfoMap;
|
||||||
std::chrono::steady_clock::time_point received;
|
std::chrono::steady_clock::time_point received;
|
||||||
std::chrono::steady_clock::time_point sent;
|
std::chrono::steady_clock::time_point sent;
|
||||||
float rtt;
|
std::chrono::steady_clock::duration rtt;
|
||||||
}; // struct ConnectionData
|
}; // struct ConnectionData
|
||||||
|
|
||||||
struct Context {
|
struct Context {
|
||||||
|
|
|
@ -14,17 +14,17 @@ sentTime(std::chrono::steady_clock::now())
|
||||||
|
|
||||||
UDPC::ConnectionData::ConnectionData() :
|
UDPC::ConnectionData::ConnectionData() :
|
||||||
flags(),
|
flags(),
|
||||||
timer(0.0f),
|
timer(std::chrono::steady_clock::duration::zero()),
|
||||||
toggleT(30.0f),
|
toggleT(std::chrono::seconds(30)),
|
||||||
toggleTimer(0.0f),
|
toggleTimer(std::chrono::steady_clock::duration::zero()),
|
||||||
toggledTimer(0.0f),
|
toggledTimer(std::chrono::steady_clock::duration::zero()),
|
||||||
sentPkts(),
|
sentPkts(),
|
||||||
sendPkts(UDPC_QUEUED_PKTS_MAX_SIZE),
|
sendPkts(UDPC_QUEUED_PKTS_MAX_SIZE),
|
||||||
priorityPkts(UDPC_QUEUED_PKTS_MAX_SIZE),
|
priorityPkts(UDPC_QUEUED_PKTS_MAX_SIZE),
|
||||||
receivedPkts(UDPC_RECEIVED_PKTS_MAX_SIZE),
|
receivedPkts(UDPC_RECEIVED_PKTS_MAX_SIZE),
|
||||||
received(std::chrono::steady_clock::now()),
|
received(std::chrono::steady_clock::now()),
|
||||||
sent(std::chrono::steady_clock::now()),
|
sent(std::chrono::steady_clock::now()),
|
||||||
rtt(0.0f)
|
rtt(std::chrono::steady_clock::duration::zero())
|
||||||
{
|
{
|
||||||
flags.set(0);
|
flags.set(0);
|
||||||
}
|
}
|
||||||
|
@ -227,13 +227,13 @@ void UDPC_update(void *ctx) {
|
||||||
const auto now = std::chrono::steady_clock::now();
|
const auto now = std::chrono::steady_clock::now();
|
||||||
c->lastUpdated = now;
|
c->lastUpdated = now;
|
||||||
|
|
||||||
float temp_dt_fs;
|
std::chrono::steady_clock::duration temp_dt_fs;
|
||||||
{
|
{
|
||||||
// check timed out, check good/bad mode with rtt, remove timed out
|
// check timed out, check good/bad mode with rtt, remove timed out
|
||||||
std::vector<uint32_t> removed;
|
std::vector<uint32_t> removed;
|
||||||
for(auto iter = c->conMap.begin(); iter != c->conMap.end(); ++iter) {
|
for(auto iter = c->conMap.begin(); iter != c->conMap.end(); ++iter) {
|
||||||
temp_dt_fs = UDPC::durationToFSec(iter->second.received, now);
|
temp_dt_fs = now - iter->second.received;
|
||||||
if(temp_dt_fs >= UDPC_TIMEOUT_SECONDS) {
|
if(temp_dt_fs >= UDPC::CONNECTION_TIMEOUT) {
|
||||||
removed.push_back(iter->first);
|
removed.push_back(iter->first);
|
||||||
continue;
|
continue;
|
||||||
// TODO log timed out connection
|
// TODO log timed out connection
|
||||||
|
@ -246,38 +246,38 @@ void UDPC_update(void *ctx) {
|
||||||
// good mode, bad rtt
|
// good mode, bad rtt
|
||||||
// TODO log switching to bad mode
|
// TODO log switching to bad mode
|
||||||
iter->second.flags.reset(1);
|
iter->second.flags.reset(1);
|
||||||
if(iter->second.toggledTimer <= 10.0f) {
|
if(iter->second.toggledTimer <= UDPC::TEN_SECONDS) {
|
||||||
iter->second.toggleT *= 2.0f;
|
iter->second.toggleT *= 2;
|
||||||
}
|
}
|
||||||
iter->second.toggledTimer = 0.0f;
|
iter->second.toggledTimer = std::chrono::steady_clock::duration::zero();
|
||||||
} else if(iter->second.flags.test(1)) {
|
} else if(iter->second.flags.test(1)) {
|
||||||
// good mode, good rtt
|
// good mode, good rtt
|
||||||
if(iter->second.toggleTimer >= 10.0f) {
|
if(iter->second.toggleTimer >= UDPC::TEN_SECONDS) {
|
||||||
iter->second.toggleTimer = 0.0f;
|
iter->second.toggleTimer = std::chrono::steady_clock::duration::zero();
|
||||||
iter->second.toggleT /= 2.0f;
|
iter->second.toggleT /= 2;
|
||||||
if(iter->second.toggleT < 1.0f) {
|
if(iter->second.toggleT < UDPC::ONE_SECOND) {
|
||||||
iter->second.toggleT = 1.0f;
|
iter->second.toggleT = UDPC::ONE_SECOND;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if(!iter->second.flags.test(1) &&
|
} else if(!iter->second.flags.test(1) &&
|
||||||
iter->second.flags.test(2)) {
|
iter->second.flags.test(2)) {
|
||||||
// bad mode, good rtt
|
// bad mode, good rtt
|
||||||
if(iter->second.toggledTimer >= iter->second.toggleT) {
|
if(iter->second.toggledTimer >= iter->second.toggleT) {
|
||||||
iter->second.toggleTimer = 0.0f;
|
iter->second.toggleTimer = std::chrono::steady_clock::duration::zero();
|
||||||
iter->second.toggledTimer = 0.0f;
|
iter->second.toggledTimer = std::chrono::steady_clock::duration::zero();
|
||||||
// TODO log switching to good mode
|
// TODO log switching to good mode
|
||||||
iter->second.flags.set(1);
|
iter->second.flags.set(1);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// bad mode, bad rtt
|
// bad mode, bad rtt
|
||||||
iter->second.toggledTimer = 0.0f;
|
iter->second.toggledTimer = std::chrono::steady_clock::duration::zero();
|
||||||
}
|
}
|
||||||
|
|
||||||
iter->second.timer += temp_dt_fs;
|
iter->second.timer += temp_dt_fs;
|
||||||
if(iter->second.timer >= (iter->second.flags.test(1)
|
if(iter->second.timer >= (iter->second.flags.test(1)
|
||||||
? UDPC_GOOD_MODE_SEND_INTERVAL
|
? UDPC::GOOD_MODE_SEND_RATE
|
||||||
: UDPC_BAD_MODE_SEND_INTERVAL)) {
|
: UDPC::BAD_MODE_SEND_RATE)) {
|
||||||
iter->second.timer = 0.0f;
|
iter->second.timer = std::chrono::steady_clock::duration::zero();
|
||||||
iter->second.flags.set(0);
|
iter->second.flags.set(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -572,14 +572,14 @@ void UDPC_update(void *ctx) {
|
||||||
auto sentInfoIter = iter->second.sentInfoMap.find(id);
|
auto sentInfoIter = iter->second.sentInfoMap.find(id);
|
||||||
assert(sentInfoIter != iter->second.sentInfoMap.end()
|
assert(sentInfoIter != iter->second.sentInfoMap.end()
|
||||||
&& "sentInfoMap should have known stored id");
|
&& "sentInfoMap should have known stored id");
|
||||||
float diff = UDPC::durationToFSec(sentInfoIter->second->sentTime, now);
|
auto diff = now - sentInfoIter->second->sentTime;
|
||||||
if(diff > iter->second.rtt) {
|
if(diff > iter->second.rtt) {
|
||||||
iter->second.rtt += (diff - iter->second.rtt) / 10.0f;
|
iter->second.rtt += (diff - iter->second.rtt) / 10;
|
||||||
} else {
|
} else {
|
||||||
iter->second.rtt -= (iter->second.rtt - diff) / 10.0f;
|
iter->second.rtt -= (iter->second.rtt - diff) / 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
iter->second.flags.set(2, iter->second.rtt <= UDPC_GOOD_RTT_LIMIT_SEC);
|
iter->second.flags.set(2, iter->second.rtt <= UDPC::GOOD_RTT_LIMIT);
|
||||||
|
|
||||||
// TODO verbose log rtt
|
// TODO verbose log rtt
|
||||||
break;
|
break;
|
||||||
|
@ -607,8 +607,8 @@ void UDPC_update(void *ctx) {
|
||||||
auto sentInfoIter = iter->second.sentInfoMap.find(sentID);
|
auto sentInfoIter = iter->second.sentInfoMap.find(sentID);
|
||||||
assert(sentInfoIter != iter->second.sentInfoMap.end()
|
assert(sentInfoIter != iter->second.sentInfoMap.end()
|
||||||
&& "Every entry in sentPkts must have a corresponding entry in sentInfoMap");
|
&& "Every entry in sentPkts must have a corresponding entry in sentInfoMap");
|
||||||
float seconds = UDPC::durationToFSec(sentInfoIter->second->sentTime, now);
|
auto duration = now - sentInfoIter->second->sentTime;
|
||||||
if(seconds > UDPC_PACKET_TIMEOUT_SEC) {
|
if(duration > UDPC::PACKET_TIMEOUT_TIME) {
|
||||||
if(sentIter->dataSize <= 20) {
|
if(sentIter->dataSize <= 20) {
|
||||||
// TODO log error: timed out packet has no data
|
// TODO log error: timed out packet has no data
|
||||||
sentIter->flags |= 0x8;
|
sentIter->flags |= 0x8;
|
||||||
|
|
Loading…
Reference in a new issue