diff --git a/cockatrice/src/remoteclient.cpp b/cockatrice/src/remoteclient.cpp index 76f4854c..2bb43b99 100644 --- a/cockatrice/src/remoteclient.cpp +++ b/cockatrice/src/remoteclient.cpp @@ -6,7 +6,7 @@ #include "protocol_items.h" RemoteClient::RemoteClient(QObject *parent) - : AbstractClient(parent), topLevelItem(0) + : AbstractClient(parent), timeRunning(0), lastDataReceived(0), topLevelItem(0) { ProtocolItem::initializeHash(); @@ -38,6 +38,7 @@ void RemoteClient::slotSocketError(QAbstractSocket::SocketError /*error*/) void RemoteClient::slotConnected() { + timeRunning = lastDataReceived = 0; timer->start(); setStatus(StatusAwaitingWelcome); } @@ -65,6 +66,7 @@ void RemoteClient::readData() QByteArray data = socket->readAll(); qDebug() << data; xmlReader->addData(data); + lastDataReceived = timeRunning; while (!xmlReader->atEnd()) { xmlReader->readNext(); @@ -130,17 +132,21 @@ void RemoteClient::disconnectFromServer() void RemoteClient::ping() { - int maxTime = 0; - QMapIterator i(pendingCommands); - while (i.hasNext()) { - int time = i.next().value()->tick(); - if (time > maxTime) - maxTime = time; - } + QMutableMapIterator i(pendingCommands); + while (i.hasNext()) + if (i.next().value()->tick() > maxTimeout) { + CommandContainer *cont = i.value(); + i.remove(); + cont->deleteLater(); + } + + int maxTime = timeRunning - lastDataReceived; emit maxPingTime(maxTime, maxTimeout); if (maxTime >= maxTimeout) { - emit serverTimeout(); disconnectFromServer(); - } else + emit serverTimeout(); + } else { sendCommand(new Command_Ping); + ++timeRunning; + } } diff --git a/cockatrice/src/remoteclient.h b/cockatrice/src/remoteclient.h index 30313fbb..5e89a96e 100644 --- a/cockatrice/src/remoteclient.h +++ b/cockatrice/src/remoteclient.h @@ -24,6 +24,7 @@ private slots: void loginResponse(ProtocolResponse *response); private: static const int maxTimeout = 10; + int timeRunning, lastDataReceived; QTimer *timer; QTcpSocket *socket; diff --git a/common/server_game.cpp b/common/server_game.cpp index b89370c4..704b23c2 100644 --- a/common/server_game.cpp +++ b/common/server_game.cpp @@ -58,7 +58,6 @@ void Server_Game::pingClockTimeout() { ++secondsElapsed; - QDateTime now = QDateTime::currentDateTime(); QList pingList; QMapIterator playerIterator(players); bool allPlayersInactive = true; @@ -66,7 +65,7 @@ void Server_Game::pingClockTimeout() Server_Player *player = playerIterator.next().value(); int pingTime; if (player->getProtocolHandler()) { - pingTime = player->getProtocolHandler()->getLastCommandTime().secsTo(now); + pingTime = player->getProtocolHandler()->getLastCommandTime(); allPlayersInactive = false; } else pingTime = -1; diff --git a/common/server_protocolhandler.cpp b/common/server_protocolhandler.cpp index 1e6520a3..69bca146 100644 --- a/common/server_protocolhandler.cpp +++ b/common/server_protocolhandler.cpp @@ -14,7 +14,7 @@ #include Server_ProtocolHandler::Server_ProtocolHandler(Server *_server, QObject *parent) - : QObject(parent), server(_server), authState(PasswordWrong), acceptsUserListChanges(false), acceptsRoomListChanges(false), userInfo(0), lastCommandTime(QDateTime::currentDateTime()) + : QObject(parent), server(_server), authState(PasswordWrong), acceptsUserListChanges(false), acceptsRoomListChanges(false), userInfo(0), timeRunning(0), lastDataReceived(0) { connect(server, SIGNAL(pingClockTimeout()), this, SLOT(pingClockTimeout())); } @@ -58,8 +58,6 @@ void Server_ProtocolHandler::playerRemovedFromGame(Server_Game *game) ResponseCode Server_ProtocolHandler::processCommandHelper(Command *command, CommandContainer *cont) { - lastCommandTime = QDateTime::currentDateTime(); - RoomCommand *roomCommand = qobject_cast(command); if (roomCommand) { qDebug() << "received RoomCommand: roomId =" << roomCommand->getRoomId(); @@ -159,6 +157,8 @@ ResponseCode Server_ProtocolHandler::processCommandHelper(Command *command, Comm void Server_ProtocolHandler::processCommandContainer(CommandContainer *cont) { + lastDataReceived = timeRunning; + const QList &cmdList = cont->getCommandList(); ResponseCode finalResponseCode = RespOk; for (int i = 0; i < cmdList.size(); ++i) { @@ -219,8 +219,9 @@ void Server_ProtocolHandler::pingClockTimeout() messageCountOverTime.removeLast(); } - if (lastCommandTime.secsTo(QDateTime::currentDateTime()) > server->getMaxPlayerInactivityTime()) + if (timeRunning - lastDataReceived > server->getMaxPlayerInactivityTime()) deleteLater(); + ++timeRunning; } void Server_ProtocolHandler::enqueueProtocolItem(ProtocolItem *item) diff --git a/common/server_protocolhandler.h b/common/server_protocolhandler.h index 3cd60503..10360631 100644 --- a/common/server_protocolhandler.h +++ b/common/server_protocolhandler.h @@ -31,7 +31,7 @@ protected: private: QList itemQueue; QList messageSizeOverTime, messageCountOverTime; - QDateTime lastCommandTime; + int timeRunning, lastDataReceived; QTimer *pingClock; virtual DeckList *getDeckFromDatabase(int deckId) = 0; @@ -101,8 +101,8 @@ public: void setUserInfo(ServerInfo_User *_userInfo) { userInfo = _userInfo; } const QMap &getBuddyList() const { return buddyList; } const QMap &getIgnoreList() const { return ignoreList; } - const QDateTime &getLastCommandTime() const { return lastCommandTime; } + int getLastCommandTime() const { return timeRunning - lastDataReceived; } void processCommandContainer(CommandContainer *cont); virtual void sendProtocolItem(ProtocolItem *item, bool deleteItem = true) = 0; void enqueueProtocolItem(ProtocolItem *item); diff --git a/servatrice/src/serversocketinterface.cpp b/servatrice/src/serversocketinterface.cpp index f4b13d02..602a1b8f 100644 --- a/servatrice/src/serversocketinterface.cpp +++ b/servatrice/src/serversocketinterface.cpp @@ -432,7 +432,7 @@ ResponseCode ServerSocketInterface::cmdUpdateServerMessage(Command_UpdateServerM return RespOk; } -ResponseCode ServerSocketInterface::cmdBanFromServer(Command_BanFromServer *cmd, CommandContainer *cont) +ResponseCode ServerSocketInterface::cmdBanFromServer(Command_BanFromServer *cmd, CommandContainer * /*cont*/) { QString userName = cmd->getUserName(); if (!server->getUsers().contains(userName))