Exemple #1
0
bool Server::readDisconnect(MsgData& msg, ServerPlayer& player) {
    std::cout << "Disconnect" << std::endl;
    auto idPlayer = static_cast<sf::Uint8> (player.getId());

    m_players.remove(player);

    MsgData msgDisconnect;
    msgDisconnect << MsgType::Disconnect << idPlayer;
    m_network.getTCPManager().send(msgDisconnect, m_players.getList());

    return true;
}
Exemple #2
0
bool Server::read(MsgData& message, ServerPlayer &player) {
    MsgType msgType;
    MsgData messageCopy(message);

    bool isRead(false);

    message >> msgType;
    std::cout << "[Serv][Read][Start] \t Read message(" << static_cast<int> (msgType) << ") from " << player.getName() << std::endl;
    switch (msgType) {
        case MsgType::Action:
            if (m_stateInWait)
                return readAction(message, player);
            break;
        case MsgType::ClientPlayerInfo:
        {
            // Receive info from Client
            std::cout << "[Serv][Read] \t Read ClientPlayerInfo" << std::endl;
            //            auto msgCPI = static_cast<MsgClientPlayerInfo> (*message);
            sf::Uint16 sfPort;
            std::string name;
            message >> sfPort >> name;
            //            std::cout << "[Serv][Read] \t cast done : " << msgCPI << std::endl;
            player.setUdpPort(sfPort);
            player.setName(name);
            std::cout << "[Serv][Read] \t Read info client(" << player.getName() << "@" << player.getAddress().toString() << ":" << player.getUdpPort() << ")" << std::endl;
            // Send info to Client

            unsigned short port = m_network.getUDPManager().getPort();
            MsgData msgServer;
            msgServer << MsgType::ServerPlayerInfo << sf::Uint16(port) << sf::Int16(player.getId());
            std::cout << "[Serv][Read] \t Send info serv. ServUdpPort(" << port << "), playerId(" << sf::Int16(player.getId()) << ")" << std::endl;
            m_network.getTCPManager().send(msgServer, player.getTCPSocket());
            m_players.setPlayerReady(player);
            isRead = true;
        }
            break;
        case MsgType::Acknowledgment:
        {
            std::cout << "Ack" << std::endl;
            m_acknowledgment.release();
            isRead = true;
        }
            break;
        case MsgType::Disconnect:
        {
            isRead |= readDisconnect(message, player);
        }
            break;
        case MsgType::Undef:
        {
            std::cout << "Undefined" << std::endl;
        }
            break;
        default:
        {
        }
    }

    if (m_game)
        isRead = m_game->read(messageCopy, player);

    return isRead;
}