Server_Room optimisation
This commit is contained in:
parent
de3055be6a
commit
00029eeeb4
3 changed files with 15 additions and 13 deletions
|
@ -552,8 +552,6 @@ Response::ResponseCode Server_ProtocolHandler::cmdCreateGame(const Command_Creat
|
||||||
if (gameId == -1)
|
if (gameId == -1)
|
||||||
return Response::RespInternalError;
|
return Response::RespInternalError;
|
||||||
|
|
||||||
QMutexLocker roomLocker(&room->gamesMutex);
|
|
||||||
|
|
||||||
if (server->getMaxGamesPerUser() > 0)
|
if (server->getMaxGamesPerUser() > 0)
|
||||||
if (room->getGamesCreatedByUser(QString::fromStdString(userInfo->name())) >= server->getMaxGamesPerUser())
|
if (room->getGamesCreatedByUser(QString::fromStdString(userInfo->name())) >= server->getMaxGamesPerUser())
|
||||||
return Response::RespContextError;
|
return Response::RespContextError;
|
||||||
|
|
|
@ -30,7 +30,7 @@ Server_Room::~Server_Room()
|
||||||
gamesMutex.unlock();
|
gamesMutex.unlock();
|
||||||
|
|
||||||
usersLock.lockForWrite();
|
usersLock.lockForWrite();
|
||||||
userList.clear();
|
users.clear();
|
||||||
usersLock.unlock();
|
usersLock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,10 +64,11 @@ const ServerInfo_Room &Server_Room::getInfo(ServerInfo_Room &result, bool comple
|
||||||
gamesMutex.unlock();
|
gamesMutex.unlock();
|
||||||
|
|
||||||
usersLock.lockForRead();
|
usersLock.lockForRead();
|
||||||
result.set_player_count(userList.size() + externalUsers.size());
|
result.set_player_count(users.size() + externalUsers.size());
|
||||||
if (complete) {
|
if (complete) {
|
||||||
for (int i = 0; i < userList.size(); ++i)
|
QMapIterator<QString, Server_ProtocolHandler *> userIterator(users);
|
||||||
result.add_user_list()->CopyFrom(userList[i]->copyUserInfo(false));
|
while (userIterator.hasNext())
|
||||||
|
result.add_user_list()->CopyFrom(userIterator.next().value()->copyUserInfo(false));
|
||||||
if (includeExternalData) {
|
if (includeExternalData) {
|
||||||
QMapIterator<QString, ServerInfo_User_Container> externalUserIterator(externalUsers);
|
QMapIterator<QString, ServerInfo_User_Container> externalUserIterator(externalUsers);
|
||||||
while (externalUserIterator.hasNext())
|
while (externalUserIterator.hasNext())
|
||||||
|
@ -101,7 +102,7 @@ void Server_Room::addClient(Server_ProtocolHandler *client)
|
||||||
sendRoomEvent(prepareRoomEvent(event));
|
sendRoomEvent(prepareRoomEvent(event));
|
||||||
|
|
||||||
usersLock.lockForWrite();
|
usersLock.lockForWrite();
|
||||||
userList.append(client);
|
users.insert(QString::fromStdString(client->getUserInfo()->name()), client);
|
||||||
usersLock.unlock();
|
usersLock.unlock();
|
||||||
|
|
||||||
ServerInfo_Room roomInfo;
|
ServerInfo_Room roomInfo;
|
||||||
|
@ -111,7 +112,7 @@ void Server_Room::addClient(Server_ProtocolHandler *client)
|
||||||
void Server_Room::removeClient(Server_ProtocolHandler *client)
|
void Server_Room::removeClient(Server_ProtocolHandler *client)
|
||||||
{
|
{
|
||||||
usersLock.lockForWrite();
|
usersLock.lockForWrite();
|
||||||
userList.removeAt(userList.indexOf(client));
|
users.remove(QString::fromStdString(client->getUserInfo()->name()));
|
||||||
usersLock.unlock();
|
usersLock.unlock();
|
||||||
|
|
||||||
Event_LeaveRoom event;
|
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)
|
void Server_Room::sendRoomEvent(RoomEvent *event, bool sendToIsl)
|
||||||
{
|
{
|
||||||
usersLock.lockForRead();
|
usersLock.lockForRead();
|
||||||
for (int i = 0; i < userList.size(); ++i)
|
{
|
||||||
userList[i]->sendProtocolItem(*event);
|
QMapIterator<QString, Server_ProtocolHandler *> userIterator(users);
|
||||||
|
while (userIterator.hasNext())
|
||||||
|
userIterator.next().value()->sendProtocolItem(*event);
|
||||||
|
}
|
||||||
usersLock.unlock();
|
usersLock.unlock();
|
||||||
|
|
||||||
if (sendToIsl)
|
if (sendToIsl)
|
||||||
|
@ -229,8 +233,7 @@ void Server_Room::broadcastGameListUpdate(const ServerInfo_Game &gameInfo, bool
|
||||||
|
|
||||||
void Server_Room::addGame(Server_Game *game)
|
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)));
|
connect(game, SIGNAL(gameInfoChanged(ServerInfo_Game)), this, SLOT(broadcastGameListUpdate(ServerInfo_Game)));
|
||||||
|
|
||||||
game->gameMutex.lock();
|
game->gameMutex.lock();
|
||||||
|
@ -238,6 +241,7 @@ void Server_Room::addGame(Server_Game *game)
|
||||||
ServerInfo_Game gameInfo;
|
ServerInfo_Game gameInfo;
|
||||||
game->getInfo(gameInfo);
|
game->getInfo(gameInfo);
|
||||||
game->gameMutex.unlock();
|
game->gameMutex.unlock();
|
||||||
|
gamesMutex.unlock();
|
||||||
|
|
||||||
emit gameListChanged(gameInfo);
|
emit gameListChanged(gameInfo);
|
||||||
ServerInfo_Room roomInfo;
|
ServerInfo_Room roomInfo;
|
||||||
|
|
|
@ -37,7 +37,7 @@ private:
|
||||||
QStringList gameTypes;
|
QStringList gameTypes;
|
||||||
QMap<int, Server_Game *> games;
|
QMap<int, Server_Game *> games;
|
||||||
QMap<int, ServerInfo_Game> externalGames;
|
QMap<int, ServerInfo_Game> externalGames;
|
||||||
QList<Server_ProtocolHandler *> userList;
|
QMap<QString, Server_ProtocolHandler *> users;
|
||||||
QMap<QString, ServerInfo_User_Container> externalUsers;
|
QMap<QString, ServerInfo_User_Container> externalUsers;
|
||||||
private slots:
|
private slots:
|
||||||
void broadcastGameListUpdate(const ServerInfo_Game &gameInfo, bool sendToIsl = true);
|
void broadcastGameListUpdate(const ServerInfo_Game &gameInfo, bool sendToIsl = true);
|
||||||
|
|
Loading…
Reference in a new issue