From aa7255c2e55d82cf8d56c4e222b5776887ebb847 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Fri, 20 Sep 2019 16:59:16 +0900 Subject: [PATCH] Fixes and improvements Fix use of accept-connections flag. Fix use of TSQueue (add top_and_pop_and_rsize fn). Reduce sleep time of threadedUpdate to 8 ms. --- cpp_impl/src/TSQueue.hpp | 15 +++++++++++++++ cpp_impl/src/UDPC_Defines.hpp | 1 - cpp_impl/src/UDPConnection.cpp | 10 +++------- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/cpp_impl/src/TSQueue.hpp b/cpp_impl/src/TSQueue.hpp index 714788f..2352cf8 100644 --- a/cpp_impl/src/TSQueue.hpp +++ b/cpp_impl/src/TSQueue.hpp @@ -27,6 +27,7 @@ class TSQueue { std::optional top(); bool pop(); std::optional top_and_pop(); + std::optional top_and_pop_and_rsize(unsigned int *rsize); void clear(); /* * status == @@ -117,6 +118,20 @@ std::optional TSQueue::top_and_pop() { return value; } +template +std::optional TSQueue::top_and_pop_and_rsize(unsigned int *rsize) { + std::lock_guard lock(mutex); + std::optional value = std::nullopt; + if(!rb.empty()) { + value = rb.top(); + rb.pop(); + } + if(rsize) { + *rsize = rb.getSize(); + } + return value; +} + template void TSQueue::clear() { std::lock_guard lock(mutex); diff --git a/cpp_impl/src/UDPC_Defines.hpp b/cpp_impl/src/UDPC_Defines.hpp index 63e0c15..ce038d7 100644 --- a/cpp_impl/src/UDPC_Defines.hpp +++ b/cpp_impl/src/UDPC_Defines.hpp @@ -237,7 +237,6 @@ public: /* * 0 - is threaded * 1 - is client - * 2 - is accepting new connections */ std::bitset<32> flags; std::atomic_bool isAcceptNewConnections; diff --git a/cpp_impl/src/UDPConnection.cpp b/cpp_impl/src/UDPConnection.cpp index 5af6ea8..f8b1c40 100644 --- a/cpp_impl/src/UDPConnection.cpp +++ b/cpp_impl/src/UDPConnection.cpp @@ -169,7 +169,6 @@ mutex() } else { flags.reset(0); } - flags.set(2); rng_engine.seed(std::chrono::system_clock::now().time_since_epoch().count()); @@ -611,7 +610,7 @@ void UDPC::Context::update_impl() { UDPC_ConnectionId identifier{receivedData.sin6_addr, receivedData.sin6_scope_id, ntohs(receivedData.sin6_port)}; - if(isConnect && flags.test(2)) { + if(isConnect && isAcceptNewConnections.load()) { // is connect packet and is accepting new connections if(!flags.test(1) && conMap.find(identifier) == conMap.end()) { @@ -936,7 +935,7 @@ void UDPC::threadedUpdate(Context *ctx) { ctx->update_impl(); ctx->mutex.unlock(); nextNow = std::chrono::steady_clock::now(); - std::this_thread::sleep_for(std::chrono::milliseconds(11) - (nextNow - now)); + std::this_thread::sleep_for(std::chrono::milliseconds(8) - (nextNow - now)); } } @@ -1279,10 +1278,7 @@ UDPC_PacketInfo UDPC_get_received(UDPC_HContext ctx, unsigned int *remaining) { return UDPC::get_empty_pinfo(); } - auto opt_pinfo = c->receivedPkts.top_and_pop(); - if(remaining) { - *remaining = c->receivedPkts.size(); - } + auto opt_pinfo = c->receivedPkts.top_and_pop_and_rsize(remaining); if(opt_pinfo) { return *opt_pinfo; }