From 25d77fb64a27701e589c40e45d00ec0b237b2ed2 Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Tue, 15 Dec 2015 16:58:44 +0100 Subject: [PATCH] Possibly fix #1598 --- CMakeLists.txt | 4 ++++ common/server_protocolhandler.cpp | 7 +++++-- common/server_room.cpp | 3 ++- common/server_room.h | 5 +++-- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b8781a23..1bbb7b84 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,10 @@ if(POLICY CMP0048) cmake_policy(SET CMP0048 OLD) endif() +if(POLICY CMP0064) + cmake_policy(SET CMP0064 OLD) +endif() + set(PROJECT_NAME "Cockatrice") # Default to "Release" build type diff --git a/common/server_protocolhandler.cpp b/common/server_protocolhandler.cpp index 8cf1f260..406e20e7 100644 --- a/common/server_protocolhandler.cpp +++ b/common/server_protocolhandler.cpp @@ -603,8 +603,11 @@ Response::ResponseCode Server_ProtocolHandler::cmdJoinRoom(const Command_JoinRoo joinMessageEvent.set_message_type(Event_RoomSay::Welcome); rc.enqueuePostResponseItem(ServerMessage::ROOM_EVENT, r->prepareRoomEvent(joinMessageEvent)); - ServerInfo_ChatMessage chatMessage; for (int i = 0; i < r->chatHistory.size(); ++i) { - chatMessage = r->chatHistory.at(i); + QReadLocker chatHistoryLocker(&r->historyLock); + QList chatHistory = r->getChatHistory(); + ServerInfo_ChatMessage chatMessage; + for (int i = 0; i < chatHistory.size(); ++i) { + chatMessage = chatHistory.at(i); qDebug() << QString::fromStdString(chatMessage.message()).simplified(); Event_RoomSay roomChatHistory; roomChatHistory.set_message(chatMessage.sender_name() + ": " + chatMessage.message()); diff --git a/common/server_room.cpp b/common/server_room.cpp index a53fdd97..e9338627 100644 --- a/common/server_room.cpp +++ b/common/server_room.cpp @@ -236,7 +236,6 @@ void Server_Room::say(const QString &userName, const QString &s, bool sendToIsl) sendRoomEvent(prepareRoomEvent(event), sendToIsl); if (chatHistorySize != 0) { - ServerInfo_ChatMessage chatMessage; QDateTime dateTime = dateTime.currentDateTimeUtc(); QString dateTimeString = dateTime.toString(); @@ -244,10 +243,12 @@ void Server_Room::say(const QString &userName, const QString &s, bool sendToIsl) chatMessage.set_sender_name(userName.toStdString()); chatMessage.set_message(s.simplified().toStdString()); + historyLock.lockForWrite(); if (chatHistory.size() >= chatHistorySize) chatHistory.removeAt(0); chatHistory << chatMessage; + historyLock.unlock(); } } diff --git a/common/server_room.h b/common/server_room.h index 6e79ba6b..af652944 100644 --- a/common/server_room.h +++ b/common/server_room.h @@ -42,12 +42,13 @@ private: QMap externalGames; QMap users; QMap externalUsers; + QList chatHistory; private slots: void broadcastGameListUpdate(const ServerInfo_Game &gameInfo, bool sendToIsl = true); public: mutable QReadWriteLock usersLock; mutable QReadWriteLock gamesLock; - QList chatHistory; + mutable QReadWriteLock historyLock; Server_Room(int _id, int _chatHistorySize, const QString &_name, const QString &_description, const QString &_permissionLevel, bool _autoJoin, const QString &_joinMessage, const QStringList &_gameTypes, Server *parent ); ~Server_Room(); int getId() const { return id; } @@ -63,7 +64,7 @@ public: const ServerInfo_Room &getInfo(ServerInfo_Room &result, bool complete, bool showGameTypes = false, bool includeExternalData = true) const; int getGamesCreatedByUser(const QString &name) const; QList getGamesOfUser(const QString &name) const; - QList getChatHistory() { return chatHistory; } + QList & getChatHistory() { return chatHistory; } void addClient(Server_ProtocolHandler *client); void removeClient(Server_ProtocolHandler *client);