IslInterface::~IslInterface() { logger->logMessage("[ISL] session ended", this); flushOutputBuffer(); // As these signals are connected with Qt::QueuedConnection implicitly, // we don't need to worry about them modifying the lists while we're iterating. server->roomsLock.lockForRead(); QMapIterator<int, Server_Room *> roomIterator(server->getRooms()); while (roomIterator.hasNext()) { Server_Room *room = roomIterator.next().value(); room->usersLock.lockForRead(); QMapIterator<QString, ServerInfo_User_Container> roomUsers(room->getExternalUsers()); while (roomUsers.hasNext()) { roomUsers.next(); if (roomUsers.value().getUserInfo()->server_id() == serverId) emit externalRoomUserLeft(room->getId(), roomUsers.key()); } room->usersLock.unlock(); } server->roomsLock.unlock(); server->clientsLock.lockForRead(); QMapIterator<QString, Server_AbstractUserInterface *> extUsers(server->getExternalUsers()); while (extUsers.hasNext()) { extUsers.next(); if (extUsers.value()->getUserInfo()->server_id() == serverId) emit externalUserLeft(extUsers.key()); } server->clientsLock.unlock(); }
Response::ResponseCode Server_ProtocolHandler::cmdJoinRoom(const Command_JoinRoom &cmd, ResponseContainer &rc) { if (authState == NotLoggedIn) return Response::RespLoginNeeded; if (rooms.contains(cmd.room_id())) return Response::RespContextError; QReadLocker serverLocker(&server->roomsLock); Server_Room *r = server->getRooms().value(cmd.room_id(), 0); if (!r) return Response::RespNameNotFound; r->addClient(this); rooms.insert(r->getId(), r); Event_RoomSay joinMessageEvent; joinMessageEvent.set_message(r->getJoinMessage().toStdString()); rc.enqueuePostResponseItem(ServerMessage::ROOM_EVENT, r->prepareRoomEvent(joinMessageEvent)); Response_JoinRoom *re = new Response_JoinRoom; r->getInfo(*re->mutable_room_info(), true); rc.setResponseExtension(re); return Response::RespOk; }
void Server_AbstractUserInterface::joinPersistentGames(ResponseContainer &rc) { QList<PlayerReference> gamesToJoin = server->getPersistentPlayerReferences(QString::fromStdString(userInfo->name())); server->roomsLock.lockForRead(); for (int i = 0; i < gamesToJoin.size(); ++i) { const PlayerReference &pr = gamesToJoin.at(i); Server_Room *room = server->getRooms().value(pr.getRoomId()); if (!room) continue; QReadLocker roomGamesLocker(&room->gamesLock); Server_Game *game = room->getGames().value(pr.getGameId()); if (!game) continue; QMutexLocker gameLocker(&game->gameMutex); Server_Player *player = game->getPlayers().value(pr.getPlayerId()); player->setUserInterface(this); playerAddedToGame(game->getGameId(), room->getId(), player->getPlayerId()); game->createGameJoinedEvent(player, rc, true); } server->roomsLock.unlock(); }
Response::ResponseCode Server_ProtocolHandler::cmdJoinRoom(const Command_JoinRoom &cmd, ResponseContainer &rc) { if (authState == NotLoggedIn) return Response::RespLoginNeeded; if (rooms.contains(cmd.room_id())) return Response::RespContextError; QReadLocker serverLocker(&server->roomsLock); Server_Room *r = server->getRooms().value(cmd.room_id(), 0); if (!r) return Response::RespNameNotFound; QString roomPermission = r->getRoomPermission().toLower(); if (roomPermission != "none"){ if (roomPermission == "registered") { if (!(userInfo->user_level() & ServerInfo_User::IsRegistered)) return Response::RespUserLevelTooLow; } if (roomPermission == "moderator"){ if (!(userInfo->user_level() & ServerInfo_User::IsModerator)) return Response::RespUserLevelTooLow; } if (roomPermission == "administrator"){ if (!(userInfo->user_level() & ServerInfo_User::IsAdmin)) return Response::RespUserLevelTooLow; } } r->addClient(this); rooms.insert(r->getId(), r); Event_RoomSay joinMessageEvent; joinMessageEvent.set_message(r->getJoinMessage().toStdString()); joinMessageEvent.set_message_type(Event_RoomSay::Welcome); rc.enqueuePostResponseItem(ServerMessage::ROOM_EVENT, r->prepareRoomEvent(joinMessageEvent)); QReadLocker chatHistoryLocker(&r->historyLock); QList<ServerInfo_ChatMessage> 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()); roomChatHistory.set_message_type(Event_RoomSay::ChatHistory); roomChatHistory.set_time_of(QDateTime::fromString(QString::fromStdString(chatMessage.time())).toMSecsSinceEpoch()); rc.enqueuePostResponseItem(ServerMessage::ROOM_EVENT, r->prepareRoomEvent(roomChatHistory)); } Response_JoinRoom *re = new Response_JoinRoom; r->getInfo(*re->mutable_room_info(), true); rc.setResponseExtension(re); return Response::RespOk; }
ResponseCode Server_ProtocolHandler::cmdJoinRoom(Command_JoinRoom *cmd, CommandContainer *cont) { if (authState == PasswordWrong) return RespLoginNeeded; if (rooms.contains(cmd->getRoomId())) return RespContextError; Server_Room *r = server->getRooms().value(cmd->getRoomId(), 0); if (!r) return RespNameNotFound; r->addClient(this); rooms.insert(r->getId(), r); enqueueProtocolItem(new Event_RoomSay(r->getId(), QString(), r->getJoinMessage())); cont->setResponse(new Response_JoinRoom(cont->getCmdId(), RespOk, r->getInfo(true))); return RespNothing; }
Response::ResponseCode Server_ProtocolHandler::cmdJoinRoom(const Command_JoinRoom &cmd, ResponseContainer &rc) { if (authState == NotLoggedIn) return Response::RespLoginNeeded; if (rooms.contains(cmd.room_id())) return Response::RespContextError; QReadLocker serverLocker(&server->roomsLock); Server_Room *r = server->getRooms().value(cmd.room_id(), 0); if (!r) return Response::RespNameNotFound; QString roomPermission = r->getRoomPermission().toLower(); if (roomPermission != "none"){ if (roomPermission == "registered") { if (!(userInfo->user_level() & ServerInfo_User::IsRegistered)) return Response::RespUserLevelTooLow; } if (roomPermission == "moderator"){ if (!(userInfo->user_level() & ServerInfo_User::IsModerator)) return Response::RespUserLevelTooLow; } if (roomPermission == "administrator"){ if (!(userInfo->user_level() & ServerInfo_User::IsAdmin)) return Response::RespUserLevelTooLow; } } r->addClient(this); rooms.insert(r->getId(), r); Event_RoomSay joinMessageEvent; joinMessageEvent.set_message(r->getJoinMessage().toStdString()); rc.enqueuePostResponseItem(ServerMessage::ROOM_EVENT, r->prepareRoomEvent(joinMessageEvent)); Response_JoinRoom *re = new Response_JoinRoom; r->getInfo(*re->mutable_room_info(), true); rc.setResponseExtension(re); return Response::RespOk; }
void Server::externalRoomSay(int roomId, const QString &userName, const QString &message) { // This function is always called from the main thread via signal/slot. QReadLocker locker(&roomsLock); Server_Room *room = rooms.value(roomId); if (!room) { qDebug() << "externalRoomSay: room id=" << roomId << "not found"; return; } room->say(userName, message, false); getDatabaseInterface()->logMessage(0, userName, "ISL", message, Server_DatabaseInterface::MessageTargetIslRoom, room->getId(), room->getName()); }