From 00029eeeb4505f2279017a5e353f6f90545e5b89 Mon Sep 17 00:00:00 2001 From: Max-Wilhelm Bruker Date: Mon, 11 Feb 2013 15:29:19 +0100 Subject: [PATCH] Server_Room optimisation --- common/server_protocolhandler.cpp | 2 -- common/server_room.cpp | 24 ++++++++++++++---------- common/server_room.h | 2 +- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/common/server_protocolhandler.cpp b/common/server_protocolhandler.cpp index 858550a4..c33a5545 100644 --- a/common/server_protocolhandler.cpp +++ b/common/server_protocolhandler.cpp @@ -552,8 +552,6 @@ Response::ResponseCode Server_ProtocolHandler::cmdCreateGame(const Command_Creat if (gameId == -1) return Response::RespInternalError; - QMutexLocker roomLocker(&room->gamesMutex); - if (server->getMaxGamesPerUser() > 0) if (room->getGamesCreatedByUser(QString::fromStdString(userInfo->name())) >= server->getMaxGamesPerUser()) return Response::RespContextError; diff --git a/common/server_room.cpp b/common/server_room.cpp index 5538a121..43b1b54a 100644 --- a/common/server_room.cpp +++ b/common/server_room.cpp @@ -30,7 +30,7 @@ Server_Room::~Server_Room() gamesMutex.unlock(); usersLock.lockForWrite(); - userList.clear(); + users.clear(); usersLock.unlock(); } @@ -64,10 +64,11 @@ const ServerInfo_Room &Server_Room::getInfo(ServerInfo_Room &result, bool comple gamesMutex.unlock(); usersLock.lockForRead(); - result.set_player_count(userList.size() + externalUsers.size()); + result.set_player_count(users.size() + externalUsers.size()); if (complete) { - for (int i = 0; i < userList.size(); ++i) - result.add_user_list()->CopyFrom(userList[i]->copyUserInfo(false)); + QMapIterator userIterator(users); + while (userIterator.hasNext()) + result.add_user_list()->CopyFrom(userIterator.next().value()->copyUserInfo(false)); if (includeExternalData) { QMapIterator externalUserIterator(externalUsers); while (externalUserIterator.hasNext()) @@ -101,7 +102,7 @@ void Server_Room::addClient(Server_ProtocolHandler *client) sendRoomEvent(prepareRoomEvent(event)); usersLock.lockForWrite(); - userList.append(client); + users.insert(QString::fromStdString(client->getUserInfo()->name()), client); usersLock.unlock(); ServerInfo_Room roomInfo; @@ -111,7 +112,7 @@ void Server_Room::addClient(Server_ProtocolHandler *client) void Server_Room::removeClient(Server_ProtocolHandler *client) { usersLock.lockForWrite(); - userList.removeAt(userList.indexOf(client)); + users.remove(QString::fromStdString(client->getUserInfo()->name())); usersLock.unlock(); Event_LeaveRoom event; @@ -210,8 +211,11 @@ void Server_Room::say(const QString &userName, const QString &s, bool sendToIsl) void Server_Room::sendRoomEvent(RoomEvent *event, bool sendToIsl) { usersLock.lockForRead(); - for (int i = 0; i < userList.size(); ++i) - userList[i]->sendProtocolItem(*event); + { + QMapIterator userIterator(users); + while (userIterator.hasNext()) + userIterator.next().value()->sendProtocolItem(*event); + } usersLock.unlock(); if (sendToIsl) @@ -229,8 +233,7 @@ void Server_Room::broadcastGameListUpdate(const ServerInfo_Game &gameInfo, bool void Server_Room::addGame(Server_Game *game) { - // Lock gamesMutex before calling this - + gamesMutex.lock(); connect(game, SIGNAL(gameInfoChanged(ServerInfo_Game)), this, SLOT(broadcastGameListUpdate(ServerInfo_Game))); game->gameMutex.lock(); @@ -238,6 +241,7 @@ void Server_Room::addGame(Server_Game *game) ServerInfo_Game gameInfo; game->getInfo(gameInfo); game->gameMutex.unlock(); + gamesMutex.unlock(); emit gameListChanged(gameInfo); ServerInfo_Room roomInfo; diff --git a/common/server_room.h b/common/server_room.h index 421a9836..bd15982d 100644 --- a/common/server_room.h +++ b/common/server_room.h @@ -37,7 +37,7 @@ private: QStringList gameTypes; QMap games; QMap externalGames; - QList userList; + QMap users; QMap externalUsers; private slots: void broadcastGameListUpdate(const ServerInfo_Game &gameInfo, bool sendToIsl = true);