From 4e6582c9640179cb195126d7f7ee363faf9fe42e Mon Sep 17 00:00:00 2001 From: "Jan Alexander Steffens (heftig)" Date: Sat, 19 May 2018 12:31:40 +0200 Subject: [PATCH] identd: Rework lowestSocketId handling Since we're appending only strictly increasing integers, the lowest socketId is always at the front of the list (if that exists). hasSocketsBelowId also had a off-by-one error. This had no effect on the code since a socketId equal to the current _socketId cannot be in the _waiting list. Reorder processWaiting's handling of requests: - If the socketId is one the request might have been waiting for, check responseAvailable. - Otherwise, if there is at least one socket we need to wait for, continue waiting. - Otherwise, responseUnavailable. The former order was: - If there are no sockets left from before this request was made, responseUnavailable. - If the socketId is one the request might have been waiting for, check responseAvailable. - Otherwise, continue waiting. The change was necessary as the lowestSocketId no longer includes the socketId that was just removed. In addition, the new order also matches the if-else chain in IdentServer::respond. --- src/core/identserver.cpp | 28 ++++++++++++---------------- src/core/identserver.h | 2 +- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/core/identserver.cpp b/src/core/identserver.cpp index 6d23a6ca..ea1295ef 100644 --- a/src/core/identserver.cpp +++ b/src/core/identserver.cpp @@ -120,7 +120,7 @@ void IdentServer::respond() { responseUnavailable(request); } else if (responseAvailable(request)) { // success - } else if (hasSocketsBelowId(transactionId)) { + } else if (lowestSocketId() < transactionId) { _requestQueue.emplace_back(request); } else { responseUnavailable(request); @@ -182,10 +182,12 @@ qint64 IdentServer::addWaitingSocket() { return newSocketId; } -bool IdentServer::hasSocketsBelowId(qint64 id) { - return std::any_of(_waiting.begin(), _waiting.end(), [=](qint64 socketId) { - return socketId <= id; - }); +qint64 IdentServer::lowestSocketId() { + if (_waiting.empty()) { + return std::numeric_limits::max(); + } + + return _waiting.front(); } void IdentServer::removeWaitingSocket(qint64 socketId) { @@ -193,21 +195,15 @@ void IdentServer::removeWaitingSocket(qint64 socketId) { } void IdentServer::processWaiting(qint64 socketId) { - qint64 lowestSocketId = std::numeric_limits::max(); - for (qint64 id : _waiting) { - if (id < lowestSocketId) { - lowestSocketId = id; - } - } removeWaitingSocket(socketId); _requestQueue.remove_if([=](Request request) { - if (request.transactionId < lowestSocketId) { - responseUnavailable(request); + if (socketId < request.transactionId && responseAvailable(request)) { return true; - } else if (request.transactionId > socketId) { - return responseAvailable(request); - } else { + } else if (lowestSocketId() < request.transactionId) { return false; + } else { + responseUnavailable(request); + return true; } }); } diff --git a/src/core/identserver.h b/src/core/identserver.h index 4d4aecc5..cff24b77 100644 --- a/src/core/identserver.h +++ b/src/core/identserver.h @@ -58,7 +58,7 @@ private: QString sysIdentForIdentity(const CoreIdentity *identity) const; - bool hasSocketsBelowId(qint64 socketId); + qint64 lowestSocketId(); void processWaiting(qint64 socketId); -- 2.20.1