diff --git a/cpp_impl/src/UDPC_Defines.hpp b/cpp_impl/src/UDPC_Defines.hpp index d43abd1..c6263b3 100644 --- a/cpp_impl/src/UDPC_Defines.hpp +++ b/cpp_impl/src/UDPC_Defines.hpp @@ -2,14 +2,9 @@ #define UDPC_DEFINES_HPP #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_QUEUED_PKTS_MAX_SIZE 32 #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_PING 0x40000000 @@ -31,10 +26,17 @@ 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 const auto INIT_PKT_INTERVAL_DT = std::chrono::seconds(5); 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 struct Context; @@ -74,10 +76,10 @@ struct ConnectionData { uint32_t lseq; uint32_t rseq; uint32_t ack; - float timer; - float toggleT; - float toggleTimer; - float toggledTimer; + std::chrono::steady_clock::duration timer; + std::chrono::steady_clock::duration toggleT; + std::chrono::steady_clock::duration toggleTimer; + std::chrono::steady_clock::duration toggledTimer; uint32_t addr; // in network order uint16_t port; // in native order std::deque sentPkts; @@ -88,7 +90,7 @@ struct ConnectionData { std::unordered_map sentInfoMap; std::chrono::steady_clock::time_point received; std::chrono::steady_clock::time_point sent; - float rtt; + std::chrono::steady_clock::duration rtt; }; // struct ConnectionData struct Context { diff --git a/cpp_impl/src/UDPConnection.cpp b/cpp_impl/src/UDPConnection.cpp index 7a2611b..ba2b685 100644 --- a/cpp_impl/src/UDPConnection.cpp +++ b/cpp_impl/src/UDPConnection.cpp @@ -14,17 +14,17 @@ sentTime(std::chrono::steady_clock::now()) UDPC::ConnectionData::ConnectionData() : flags(), -timer(0.0f), -toggleT(30.0f), -toggleTimer(0.0f), -toggledTimer(0.0f), +timer(std::chrono::steady_clock::duration::zero()), +toggleT(std::chrono::seconds(30)), +toggleTimer(std::chrono::steady_clock::duration::zero()), +toggledTimer(std::chrono::steady_clock::duration::zero()), sentPkts(), sendPkts(UDPC_QUEUED_PKTS_MAX_SIZE), priorityPkts(UDPC_QUEUED_PKTS_MAX_SIZE), receivedPkts(UDPC_RECEIVED_PKTS_MAX_SIZE), received(std::chrono::steady_clock::now()), sent(std::chrono::steady_clock::now()), -rtt(0.0f) +rtt(std::chrono::steady_clock::duration::zero()) { flags.set(0); } @@ -227,13 +227,13 @@ void UDPC_update(void *ctx) { const auto now = std::chrono::steady_clock::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 std::vector removed; for(auto iter = c->conMap.begin(); iter != c->conMap.end(); ++iter) { - temp_dt_fs = UDPC::durationToFSec(iter->second.received, now); - if(temp_dt_fs >= UDPC_TIMEOUT_SECONDS) { + temp_dt_fs = now - iter->second.received; + if(temp_dt_fs >= UDPC::CONNECTION_TIMEOUT) { removed.push_back(iter->first); continue; // TODO log timed out connection @@ -246,38 +246,38 @@ void UDPC_update(void *ctx) { // good mode, bad rtt // TODO log switching to bad mode iter->second.flags.reset(1); - if(iter->second.toggledTimer <= 10.0f) { - iter->second.toggleT *= 2.0f; + if(iter->second.toggledTimer <= UDPC::TEN_SECONDS) { + 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)) { // good mode, good rtt - if(iter->second.toggleTimer >= 10.0f) { - iter->second.toggleTimer = 0.0f; - iter->second.toggleT /= 2.0f; - if(iter->second.toggleT < 1.0f) { - iter->second.toggleT = 1.0f; + if(iter->second.toggleTimer >= UDPC::TEN_SECONDS) { + iter->second.toggleTimer = std::chrono::steady_clock::duration::zero(); + iter->second.toggleT /= 2; + if(iter->second.toggleT < UDPC::ONE_SECOND) { + iter->second.toggleT = UDPC::ONE_SECOND; } } } else if(!iter->second.flags.test(1) && iter->second.flags.test(2)) { // bad mode, good rtt if(iter->second.toggledTimer >= iter->second.toggleT) { - iter->second.toggleTimer = 0.0f; - iter->second.toggledTimer = 0.0f; + iter->second.toggleTimer = std::chrono::steady_clock::duration::zero(); + iter->second.toggledTimer = std::chrono::steady_clock::duration::zero(); // TODO log switching to good mode iter->second.flags.set(1); } } else { // bad mode, bad rtt - iter->second.toggledTimer = 0.0f; + iter->second.toggledTimer = std::chrono::steady_clock::duration::zero(); } iter->second.timer += temp_dt_fs; if(iter->second.timer >= (iter->second.flags.test(1) - ? UDPC_GOOD_MODE_SEND_INTERVAL - : UDPC_BAD_MODE_SEND_INTERVAL)) { - iter->second.timer = 0.0f; + ? UDPC::GOOD_MODE_SEND_RATE + : UDPC::BAD_MODE_SEND_RATE)) { + iter->second.timer = std::chrono::steady_clock::duration::zero(); iter->second.flags.set(0); } } @@ -572,14 +572,14 @@ void UDPC_update(void *ctx) { auto sentInfoIter = iter->second.sentInfoMap.find(id); assert(sentInfoIter != iter->second.sentInfoMap.end() && "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) { - iter->second.rtt += (diff - iter->second.rtt) / 10.0f; + iter->second.rtt += (diff - iter->second.rtt) / 10; } 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 break; @@ -607,8 +607,8 @@ void UDPC_update(void *ctx) { auto sentInfoIter = iter->second.sentInfoMap.find(sentID); assert(sentInfoIter != iter->second.sentInfoMap.end() && "Every entry in sentPkts must have a corresponding entry in sentInfoMap"); - float seconds = UDPC::durationToFSec(sentInfoIter->second->sentTime, now); - if(seconds > UDPC_PACKET_TIMEOUT_SEC) { + auto duration = now - sentInfoIter->second->sentTime; + if(duration > UDPC::PACKET_TIMEOUT_TIME) { if(sentIter->dataSize <= 20) { // TODO log error: timed out packet has no data sentIter->flags |= 0x8;