diff --git a/common/server.cpp b/common/server.cpp index b48e8f93..d19d2edb 100644 --- a/common/server.cpp +++ b/common/server.cpp @@ -26,7 +26,7 @@ #include Server::Server(QObject *parent) - : QObject(parent), nextGameId(0) + : QObject(parent), serverMutex(QMutex::Recursive), nextGameId(0) { } @@ -46,12 +46,8 @@ AuthenticationResult Server::loginUser(Server_ProtocolHandler *session, QString return authState; if (authState == PasswordRight) { - Server_ProtocolHandler *oldSession = users.value(name); - if (oldSession) { - if (!(oldSession->getUserInfo()->getUserLevel() & ServerInfo_User::IsRegistered)) - return WouldOverwriteOldSession; - delete oldSession; // ~Server_ProtocolHandler() will call Server::removeClient - } + if (users.contains(name)) + return WouldOverwriteOldSession; } else if (authState == UnknownUser) { // Change user name so that no two users have the same names, // don't interfere with registered user names though. @@ -100,11 +96,6 @@ void Server::removeClient(Server_ProtocolHandler *client) qDebug() << "Server::removeClient: " << clients.size() << "clients; " << users.size() << "users left"; } -Server_Game *Server::getGame(int gameId) const -{ - return games.value(gameId); -} - void Server::broadcastRoomUpdate() { QMutexLocker locker(&serverMutex); diff --git a/common/server.h b/common/server.h index 324ae860..70ef8b02 100644 --- a/common/server.h +++ b/common/server.h @@ -28,7 +28,6 @@ public: ~Server(); AuthenticationResult loginUser(Server_ProtocolHandler *session, QString &name, const QString &password); QList getGames() const { return games.values(); } - Server_Game *getGame(int gameId) const; const QMap &getRooms() { return rooms; } int getNextGameId() { return nextGameId++; } diff --git a/common/server_game.cpp b/common/server_game.cpp index 3f0d67bb..2c1e52b2 100644 --- a/common/server_game.cpp +++ b/common/server_game.cpp @@ -64,8 +64,11 @@ void Server_Game::pingClockTimeout() QList pingList; QMapIterator playerIterator(players); bool allPlayersInactive = true; + int playerCount = 0; while (playerIterator.hasNext()) { Server_Player *player = playerIterator.next().value(); + if (!player->getSpectator()) + ++playerCount; int pingTime; if (player->getProtocolHandler()) { pingTime = player->getProtocolHandler()->getLastCommandTime(); @@ -78,7 +81,7 @@ void Server_Game::pingClockTimeout() const int maxTime = static_cast(parent())->getServer()->getMaxGameInactivityTime(); if (allPlayersInactive) { - if ((++inactivityCounter >= maxTime) && (maxTime > 0)) + if (((++inactivityCounter >= maxTime) && (maxTime > 0)) || (playerCount < maxPlayers)) deleteLater(); } else inactivityCounter = 0; diff --git a/common/server_protocolhandler.cpp b/common/server_protocolhandler.cpp index e0351a3a..e7a430bc 100644 --- a/common/server_protocolhandler.cpp +++ b/common/server_protocolhandler.cpp @@ -263,8 +263,10 @@ ResponseCode Server_ProtocolHandler::cmdLogin(Command_Login *cmd, CommandContain ignoreList = server->getIgnoreList(userInfo->getName()); // This might not scale very well. Use an extra QMap if it becomes a problem. + QMutexLocker serverLocker(&server->serverMutex); const QList &serverGames = server->getGames(); for (int i = 0; i < serverGames.size(); ++i) { + QMutexLocker gameLocker(&serverGames[i]->gameMutex); const QList &gamePlayers = serverGames[i]->getPlayers().values(); for (int j = 0; j < gamePlayers.size(); ++j) if (gamePlayers[j]->getUserInfo()->getName() == userInfo->getName()) {