diff --git a/common/server.cpp b/common/server.cpp index d19d2edb..064f7519 100644 --- a/common/server.cpp +++ b/common/server.cpp @@ -101,7 +101,9 @@ void Server::broadcastRoomUpdate() QMutexLocker locker(&serverMutex); Server_Room *room = static_cast(sender()); QList eventRoomList; + room->roomMutex.lock(); eventRoomList.append(new ServerInfo_Room(room->getId(), room->getName(), room->getDescription(), room->getGames().size(), room->size(), room->getAutoJoin())); + room->roomMutex.unlock(); Event_ListRooms *event = new Event_ListRooms(eventRoomList); for (int i = 0; i < clients.size(); ++i) diff --git a/common/server_game.cpp b/common/server_game.cpp index cf65553e..3ef78a0f 100644 --- a/common/server_game.cpp +++ b/common/server_game.cpp @@ -31,6 +31,8 @@ Server_Game::Server_Game(Server_ProtocolHandler *_creator, int _gameId, const QString &_description, const QString &_password, int _maxPlayers, const QList &_gameTypes, bool _onlyBuddies, bool _onlyRegistered, bool _spectatorsAllowed, bool _spectatorsNeedPassword, bool _spectatorsCanTalk, bool _spectatorsSeeEverything, Server_Room *_room) : QObject(), room(_room), creatorInfo(new ServerInfo_User(_creator->getUserInfo())), gameStarted(false), gameId(_gameId), description(_description), password(_password), maxPlayers(_maxPlayers), gameTypes(_gameTypes), activePlayer(-1), activePhase(-1), onlyBuddies(_onlyBuddies), onlyRegistered(_onlyRegistered), spectatorsAllowed(_spectatorsAllowed), spectatorsNeedPassword(_spectatorsNeedPassword), spectatorsCanTalk(_spectatorsCanTalk), spectatorsSeeEverything(_spectatorsSeeEverything), inactivityCounter(0), secondsElapsed(0), gameMutex(QMutex::Recursive) { + connect(this, SIGNAL(sigStartGameIfReady()), this, SLOT(doStartGameIfReady()), Qt::QueuedConnection); + addPlayer(_creator, false, false); if (room->getServer()->getGameShouldPing()) { @@ -111,7 +113,7 @@ int Server_Game::getSpectatorCount() const return result; } -void Server_Game::startGameIfReady() +void Server_Game::doStartGameIfReady() { QMutexLocker locker(&gameMutex); @@ -159,6 +161,11 @@ void Server_Game::startGameIfReady() nextTurn(); } +void Server_Game::startGameIfReady() +{ + emit sigStartGameIfReady(); +} + void Server_Game::stopGameIfFinished() { QMutexLocker locker(&gameMutex); @@ -214,6 +221,7 @@ Server_Player *Server_Game::addPlayer(Server_ProtocolHandler *handler, bool spec int playerId = keyList.isEmpty() ? 0 : (keyList.last() + 1); Server_Player *newPlayer = new Server_Player(this, playerId, handler->getUserInfo(), spectator, handler); + newPlayer->moveToThread(thread()); sendGameEvent(new Event_Join(newPlayer->getProperties())); players.insert(playerId, newPlayer); diff --git a/common/server_game.h b/common/server_game.h index 15e85968..d2af449b 100644 --- a/common/server_game.h +++ b/common/server_game.h @@ -54,8 +54,10 @@ private: QTimer *pingClock; signals: void gameClosing(); + void sigStartGameIfReady(); private slots: void pingClockTimeout(); + void doStartGameIfReady(); public: mutable QMutex gameMutex; Server_Game(Server_ProtocolHandler *_creator, int _gameId, const QString &_description, const QString &_password, int _maxPlayers, const QList &_gameTypes, bool _onlyBuddies, bool _onlyRegistered, bool _spectatorsAllowed, bool _spectatorsNeedPassword, bool _spectatorsCanTalk, bool _spectatorsSeeEverything, Server_Room *parent); diff --git a/common/server_player.cpp b/common/server_player.cpp index f71c4cd5..9dd42eb5 100644 --- a/common/server_player.cpp +++ b/common/server_player.cpp @@ -29,6 +29,12 @@ Server_Player::~Server_Player() clearZones(); } +void Server_Player::moveToThread(QThread *thread) +{ + QObject::moveToThread(thread); + userInfo->moveToThread(thread); +} + int Server_Player::newCardId() { QMutexLocker locker(&game->gameMutex); diff --git a/common/server_player.h b/common/server_player.h index ccb43d0f..e0f7de49 100644 --- a/common/server_player.h +++ b/common/server_player.h @@ -43,6 +43,7 @@ private: public: Server_Player(Server_Game *_game, int _playerId, ServerInfo_User *_userInfo, bool _spectator, Server_ProtocolHandler *_handler); ~Server_Player(); + void moveToThread(QThread *thread); Server_ProtocolHandler *getProtocolHandler() const { return handler; } void setProtocolHandler(Server_ProtocolHandler *_handler) { playerMutex.lock(); handler = _handler; playerMutex.unlock(); } diff --git a/common/server_protocolhandler.cpp b/common/server_protocolhandler.cpp index 05ef513b..dc74ad62 100644 --- a/common/server_protocolhandler.cpp +++ b/common/server_protocolhandler.cpp @@ -821,6 +821,7 @@ ResponseCode Server_ProtocolHandler::cmdCreateToken(Command_CreateToken *cmd, Co y = 0; Server_Card *card = new Server_Card(cmd->getCardName(), player->newCardId(), x, y); + card->moveToThread(player->thread()); card->setPT(cmd->getPt()); card->setColor(cmd->getColor()); card->setAnnotation(cmd->getAnnotation()); diff --git a/common/server_room.cpp b/common/server_room.cpp index 32953427..f0251709 100644 --- a/common/server_room.cpp +++ b/common/server_room.cpp @@ -103,6 +103,8 @@ void Server_Room::removeGame() QMutexLocker locker(&roomMutex); Server_Game *game = static_cast(sender()); + QMutexLocker gameLocker(&game->gameMutex); + broadcastGameListUpdate(game); games.remove(game->getGameId());