Ejemplo n.º 1
0
Response::ResponseCode Server_ProtocolHandler::cmdMessage(const Command_Message &cmd, ResponseContainer &rc)
{
	if (authState == NotLoggedIn)
		return Response::RespLoginNeeded;
	
	QReadLocker locker(&server->clientsLock);
	
	QString receiver = QString::fromStdString(cmd.user_name());
	Server_AbstractUserInterface *userInterface = server->findUser(receiver);
	if (!userInterface)
		return Response::RespNameNotFound;
	if (databaseInterface->isInIgnoreList(receiver, QString::fromStdString(userInfo->name())))
		return Response::RespInIgnoreList;
	
	Event_UserMessage event;
	event.set_sender_name(userInfo->name());
	event.set_receiver_name(cmd.user_name());
	event.set_message(cmd.message());
	
	SessionEvent *se = prepareSessionEvent(event);
	userInterface->sendProtocolItem(*se);
	rc.enqueuePreResponseItem(ServerMessage::SESSION_EVENT, se);
	
	return Response::RespOk;
}
Ejemplo n.º 2
0
void Server::externalJoinGameCommandReceived(const Command_JoinGame &cmd, int cmdId, int roomId, int serverId, qint64 sessionId)
{
    // This function is always called from the main thread via signal/slot.

    try {
        QReadLocker roomsLocker(&roomsLock);
        QReadLocker clientsLocker(&clientsLock);

        Server_Room *room = rooms.value(roomId);
        if (!room) {
            qDebug() << "externalJoinGameCommandReceived: room id=" << roomId << "not found";
            throw Response::RespNotInRoom;
        }
        Server_AbstractUserInterface *userInterface = externalUsersBySessionId.value(sessionId);
        if (!userInterface) {
            qDebug() << "externalJoinGameCommandReceived: session id=" << sessionId << "not found";
            throw Response::RespNotInRoom;
        }

        ResponseContainer responseContainer(cmdId);
        Response::ResponseCode responseCode = room->processJoinGameCommand(cmd, responseContainer, userInterface);
        userInterface->sendResponseContainer(responseContainer, responseCode);
    } catch (Response::ResponseCode code) {
        Response response;
        response.set_cmd_id(cmdId);
        response.set_response_code(code);

        sendIsl_Response(response, serverId, sessionId);
    }
}
Ejemplo n.º 3
0
void IslInterface::processSessionEvent(const SessionEvent &event, qint64 sessionId)
{
	switch (getPbExtension(event)) {
		case SessionEvent::SERVER_COMPLETE_LIST: sessionEvent_ServerCompleteList(event.GetExtension(Event_ServerCompleteList::ext)); break;
		case SessionEvent::USER_JOINED: sessionEvent_UserJoined(event.GetExtension(Event_UserJoined::ext)); break;
		case SessionEvent::USER_LEFT: sessionEvent_UserLeft(event.GetExtension(Event_UserLeft::ext)); break;
		case SessionEvent::GAME_JOINED: {
			QReadLocker clientsLocker(&server->clientsLock);
			Server_AbstractUserInterface *client = server->getUsersBySessionId().value(sessionId);
			if (!client) {
				qDebug() << "IslInterface::processSessionEvent: session id" << sessionId << "not found";
				break;
			}
			const Event_GameJoined &gameJoined = event.GetExtension(Event_GameJoined::ext);
			client->playerAddedToGame(gameJoined.game_info().game_id(), gameJoined.game_info().room_id(), gameJoined.player_id());
			client->sendProtocolItem(event);
			break;
		}
		case SessionEvent::USER_MESSAGE:
		case SessionEvent::REPLAY_ADDED: {
			QReadLocker clientsLocker(&server->clientsLock);
			Server_AbstractUserInterface *client = server->getUsersBySessionId().value(sessionId);
			if (!client) {
				qDebug() << "IslInterface::processSessionEvent: session id" << sessionId << "not found";
				break;
			}
			
			client->sendProtocolItem(event);
			break;
		}
		default: ;
	}
}
Ejemplo n.º 4
0
void Server::externalUserLeft(const QString &userName)
{
    // This function is always called from the main thread via signal/slot.

    clientsLock.lockForWrite();
    Server_AbstractUserInterface *user = externalUsers.take(userName);
    externalUsersBySessionId.remove(user->getUserInfo()->session_id());
    clientsLock.unlock();

    QMap<int, QPair<int, int> > userGames(user->getGames());
    QMapIterator<int, QPair<int, int> > userGamesIterator(userGames);
    roomsLock.lockForRead();
    while (userGamesIterator.hasNext()) {
        userGamesIterator.next();
        Server_Room *room = rooms.value(userGamesIterator.value().first);
        if (!room)
            continue;

        QReadLocker roomGamesLocker(&room->gamesLock);
        Server_Game *game = room->getGames().value(userGamesIterator.key());
        if (!game)
            continue;

        QMutexLocker gameLocker(&game->gameMutex);
        Server_Player *player = game->getPlayers().value(userGamesIterator.value().second);
        if (!player)
            continue;

        player->disconnectClient();
    }
    roomsLock.unlock();

    delete user;

    Event_UserLeft event;
    event.set_name(userName.toStdString());

    SessionEvent *se = Server_ProtocolHandler::prepareSessionEvent(event);
    clientsLock.lockForRead();
    for (int i = 0; i < clients.size(); ++i)
        if (clients[i]->getAcceptsUserListChanges())
            clients[i]->sendProtocolItem(*se);
    clientsLock.unlock();
    delete se;
}