mutex and ghost games fixes

This commit is contained in:
Max-Wilhelm Bruker 2011-03-29 23:54:08 +02:00
parent 8e0f7dcf03
commit 6d5b29395c
4 changed files with 9 additions and 14 deletions

View file

@ -26,7 +26,7 @@
#include <QDebug>
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);

View file

@ -28,7 +28,6 @@ public:
~Server();
AuthenticationResult loginUser(Server_ProtocolHandler *session, QString &name, const QString &password);
QList<Server_Game *> getGames() const { return games.values(); }
Server_Game *getGame(int gameId) const;
const QMap<int, Server_Room *> &getRooms() { return rooms; }
int getNextGameId() { return nextGameId++; }

View file

@ -64,8 +64,11 @@ void Server_Game::pingClockTimeout()
QList<ServerInfo_PlayerPing *> pingList;
QMapIterator<int, Server_Player *> 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<Server_Room *>(parent())->getServer()->getMaxGameInactivityTime();
if (allPlayersInactive) {
if ((++inactivityCounter >= maxTime) && (maxTime > 0))
if (((++inactivityCounter >= maxTime) && (maxTime > 0)) || (playerCount < maxPlayers))
deleteLater();
} else
inactivityCounter = 0;

View file

@ -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<Server_Game *> &serverGames = server->getGames();
for (int i = 0; i < serverGames.size(); ++i) {
QMutexLocker gameLocker(&serverGames[i]->gameMutex);
const QList<Server_Player *> &gamePlayers = serverGames[i]->getPlayers().values();
for (int j = 0; j < gamePlayers.size(); ++j)
if (gamePlayers[j]->getUserInfo()->getName() == userInfo->getName()) {