thread fixes

This commit is contained in:
Max-Wilhelm Bruker 2011-04-18 18:55:35 +02:00
parent e6fc20114e
commit 69fce1fb5d
7 changed files with 23 additions and 1 deletions

View file

@ -101,7 +101,9 @@ void Server::broadcastRoomUpdate()
QMutexLocker locker(&serverMutex);
Server_Room *room = static_cast<Server_Room *>(sender());
QList<ServerInfo_Room *> 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)

View file

@ -31,6 +31,8 @@
Server_Game::Server_Game(Server_ProtocolHandler *_creator, int _gameId, const QString &_description, const QString &_password, int _maxPlayers, const QList<int> &_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);

View file

@ -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<int> &_gameTypes, bool _onlyBuddies, bool _onlyRegistered, bool _spectatorsAllowed, bool _spectatorsNeedPassword, bool _spectatorsCanTalk, bool _spectatorsSeeEverything, Server_Room *parent);

View file

@ -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);

View file

@ -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(); }

View file

@ -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());

View file

@ -103,6 +103,8 @@ void Server_Room::removeGame()
QMutexLocker locker(&roomMutex);
Server_Game *game = static_cast<Server_Game *>(sender());
QMutexLocker gameLocker(&game->gameMutex);
broadcastGameListUpdate(game);
games.remove(game->getGameId());