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.
This commit is contained in:
Stephen Seo 2019-09-20 16:59:16 +09:00
parent 27528bfbc5
commit aa7255c2e5
3 changed files with 18 additions and 8 deletions

View file

@ -27,6 +27,7 @@ class TSQueue {
std::optional<T> top(); std::optional<T> top();
bool pop(); bool pop();
std::optional<T> top_and_pop(); std::optional<T> top_and_pop();
std::optional<T> top_and_pop_and_rsize(unsigned int *rsize);
void clear(); void clear();
/* /*
* status == * status ==
@ -117,6 +118,20 @@ std::optional<T> TSQueue<T>::top_and_pop() {
return value; return value;
} }
template <typename T>
std::optional<T> TSQueue<T>::top_and_pop_and_rsize(unsigned int *rsize) {
std::lock_guard<std::mutex> lock(mutex);
std::optional<T> value = std::nullopt;
if(!rb.empty()) {
value = rb.top();
rb.pop();
}
if(rsize) {
*rsize = rb.getSize();
}
return value;
}
template <typename T> template <typename T>
void TSQueue<T>::clear() { void TSQueue<T>::clear() {
std::lock_guard<std::mutex> lock(mutex); std::lock_guard<std::mutex> lock(mutex);

View file

@ -237,7 +237,6 @@ public:
/* /*
* 0 - is threaded * 0 - is threaded
* 1 - is client * 1 - is client
* 2 - is accepting new connections
*/ */
std::bitset<32> flags; std::bitset<32> flags;
std::atomic_bool isAcceptNewConnections; std::atomic_bool isAcceptNewConnections;

View file

@ -169,7 +169,6 @@ mutex()
} else { } else {
flags.reset(0); flags.reset(0);
} }
flags.set(2);
rng_engine.seed(std::chrono::system_clock::now().time_since_epoch().count()); 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)}; 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 // is connect packet and is accepting new connections
if(!flags.test(1) if(!flags.test(1)
&& conMap.find(identifier) == conMap.end()) { && conMap.find(identifier) == conMap.end()) {
@ -936,7 +935,7 @@ void UDPC::threadedUpdate(Context *ctx) {
ctx->update_impl(); ctx->update_impl();
ctx->mutex.unlock(); ctx->mutex.unlock();
nextNow = std::chrono::steady_clock::now(); 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(); return UDPC::get_empty_pinfo();
} }
auto opt_pinfo = c->receivedPkts.top_and_pop(); auto opt_pinfo = c->receivedPkts.top_and_pop_and_rsize(remaining);
if(remaining) {
*remaining = c->receivedPkts.size();
}
if(opt_pinfo) { if(opt_pinfo) {
return *opt_pinfo; return *opt_pinfo;
} }