Used enums instead of numbers for lobby protocol subcommands.

This commit is contained in:
hiker 2015-11-20 16:41:54 +11:00
parent 73e14a964a
commit 7a2c7125e5
5 changed files with 118 additions and 113 deletions

View File

@ -1,5 +1,5 @@
# Modify this file to change the last-modified date when you add/remove a file.
# This will then trigger a new cmake run automatically.
# This will then trigger a new cmake run automatically.
file(GLOB_RECURSE STK_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.hpp")
file(GLOB_RECURSE STK_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.cpp")
file(GLOB_RECURSE STK_SHADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "data/shaders/*")

View File

@ -57,9 +57,8 @@ void ClientLobbyRoomProtocol::setup()
void ClientLobbyRoomProtocol::requestKartSelection(const std::string &kart_name)
{
NetworkString request(6+1+kart_name.size());
// 0x02 : kart selection request, size_token (4), token, size kart name,
// kart name
request.ai8(0x02).ai8(4).ai32(m_server->getClientServerToken())
// size_token (4), token, size kart name, kart name
request.ai8(LE_KART_SELECTION).ai8(4).ai32(m_server->getClientServerToken())
.add(kart_name);
sendMessage(request, true);
} // requestKartSelection
@ -69,8 +68,9 @@ void ClientLobbyRoomProtocol::requestKartSelection(const std::string &kart_name)
void ClientLobbyRoomProtocol::voteMajor(uint8_t major)
{
NetworkString request(8);
// 0xc0 : major vote, size_token (4), token, size major(1),major
request.ai8(0xc0).ai8(4).ai32(m_server->getClientServerToken()).ai8(1).ai8(major);
// size_token (4), token, size major(1),major
request.ai8(LE_VOTE_MAJOR).ai8(4)
.ai32(m_server->getClientServerToken()).ai8(1).ai8(major);
sendMessage(request, true);
} // voteMajor
@ -79,8 +79,9 @@ void ClientLobbyRoomProtocol::voteMajor(uint8_t major)
void ClientLobbyRoomProtocol::voteRaceCount(uint8_t count)
{
NetworkString request(8);
// 0xc0 : race count vote, size_token (4), token, size race count(1), count
request.ai8(0xc1).ai8(4).ai32(m_server->getClientServerToken()).ai8(1).ai8(count);
// size_token (4), token, size race count(1), count
request.ai8(LE_VOTE_RACE_COUNT).ai8(4)
.ai32(m_server->getClientServerToken()).ai8(1).ai8(count);
sendMessage(request, true);
} // voteRaceCount
@ -89,8 +90,8 @@ void ClientLobbyRoomProtocol::voteRaceCount(uint8_t count)
void ClientLobbyRoomProtocol::voteMinor(uint8_t minor)
{
NetworkString request(8);
// 0xc0 : minor vote, size_token (4), token, size minor(1),minor
request.ai8(0xc2).ai8(4).ai32(m_server->getClientServerToken())
// size_token (4), token, size minor(1),minor
request.ai8(LE_VOTE_MINOR).ai8(4).ai32(m_server->getClientServerToken())
.ai8(1).ai8(minor);
sendMessage(request, true);
} // voteMinor
@ -101,10 +102,9 @@ void ClientLobbyRoomProtocol::voteTrack(const std::string &track,
uint8_t track_nb)
{
NetworkString request(8+1+track.size());
// 0xc0 : major vote, size_token (4), token, size track, track, size #track,
// #track
request.ai8(0xc3).ai8(4).ai32(m_server->getClientServerToken()).add(track)
.ai8(1).ai8(track_nb);
// size_token (4), token, size track, track, size #track, #track
request.ai8(LE_VOTE_TRACK).ai8(4).ai32(m_server->getClientServerToken())
.add(track).ai8(1).ai8(track_nb);
sendMessage(request, true);
} // voteTrack
@ -113,10 +113,9 @@ void ClientLobbyRoomProtocol::voteTrack(const std::string &track,
void ClientLobbyRoomProtocol::voteReversed(bool reversed, uint8_t track_nb)
{
NetworkString request(9);
// 0xc0 : major vote, size_token (4), token, size reversed(1),reversed,
// size #track, #track
request.ai8(0xc4).ai8(4).ai32(m_server->getClientServerToken()).ai8(1)
.ai8(reversed).ai8(1).ai8(track_nb);
// size_token (4), token, size reversed(1),reversed, size #track, #track
request.ai8(LE_VOTE_REVERSE).ai8(4).ai32(m_server->getClientServerToken())
.ai8(1).ai8(reversed).ai8(1).ai8(track_nb);
sendMessage(request, true);
} // voteReversed
@ -125,9 +124,8 @@ void ClientLobbyRoomProtocol::voteReversed(bool reversed, uint8_t track_nb)
void ClientLobbyRoomProtocol::voteLaps(uint8_t laps, uint8_t track_nb)
{
NetworkString request(10);
// 0xc0 : major vote, size_token (4), token, size laps(1),laps,
// size #track, #track
request.ai8(0xc5).ai8(4).ai32(m_server->getClientServerToken()).ai8(1)
// size_token (4), token, size laps(1),laps, size #track, #track
request.ai8(LE_VOTE_LAPS).ai8(4).ai32(m_server->getClientServerToken()).ai8(1)
.ai8(laps).ai8(1).ai8(track_nb);
sendMessage(request, true);
} // voteLaps
@ -150,16 +148,16 @@ bool ClientLobbyRoomProtocol::notifyEvent(Event* event)
const NetworkString &data = event->data();
assert(data.size()); // assert that data isn't empty
uint8_t message_type = data[0];
if (message_type != 0x03 &&
message_type != 0x06)
if (message_type != LE_KART_SELECTION_UPDATE &&
message_type != LE_RACE_FINISHED )
return false; // don't treat the event
event->removeFront(1);
Log::info("ClientLobbyRoomProtocol", "Synchronous message of type %d",
message_type);
if (message_type == 0x03) // kart selection update
if (message_type == LE_KART_SELECTION_UPDATE) // kart selection update
kartSelectionUpdate(event);
else if (message_type == 0x06) // end of race
else if (message_type == LE_RACE_FINISHED) // end of race
raceFinished(event);
return true;
@ -184,31 +182,31 @@ bool ClientLobbyRoomProtocol::notifyEventAsynchronous(Event* event)
event->removeFront(1);
Log::info("ClientLobbyRoomProtocol", "Asynchronous message of type %d",
message_type);
if (message_type == 0x01) // new player connected
if (message_type == LE_NEW_PLAYER_CONNECTED) // new player connected
newPlayer(event);
else if (message_type == 0x02) // player disconnected
else if (message_type == LE_PLAYER_DISCONNECTED) // player disconnected
disconnectedPlayer(event);
else if (message_type == 0x04) // start race
else if (message_type == LE_START_RACE) // start race
startGame(event);
else if (message_type == 0x05) // start selection phase
else if (message_type == LE_START_SELECTION) // start selection phase
startSelection(event);
else if (message_type == 0x80) // connection refused
else if (message_type == LE_CONNECTION_REFUSED) // connection refused
connectionRefused(event);
else if (message_type == 0x81) // connection accepted
else if (message_type == LE_CONNECTION_ACCEPTED) // connection accepted
connectionAccepted(event);
else if (message_type == 0x82) // kart selection refused
else if (message_type == LE_KART_SELECTION_REFUSED) // kart selection refused
kartSelectionRefused(event);
else if (message_type == 0xc0) // vote for major mode
else if (message_type == LE_VOTE_MAJOR) // vote for major mode
playerMajorVote(event);
else if (message_type == 0xc1) // vote for race count
else if (message_type == LE_VOTE_RACE_COUNT) // vote for race count
playerRaceCountVote(event);
else if (message_type == 0xc2) // vote for minor mode
else if (message_type == LE_VOTE_MINOR) // vote for minor mode
playerMinorVote(event);
else if (message_type == 0xc3) // vote for track
else if (message_type == LE_VOTE_TRACK) // vote for track
playerTrackVote(event);
else if (message_type == 0xc4) // vote for reversed mode
else if (message_type == LE_VOTE_REVERSE) // vote for reversed mode
playerReversedVote(event);
else if (message_type == 0xc5) // vote for laps
else if (message_type == LE_VOTE_LAPS) // vote for laps
playerLapsVote(event);
return true;
@ -247,8 +245,9 @@ void ClientLobbyRoomProtocol::update()
case LINKED:
{
NetworkString ns(6);
// 1 (connection request), 4 (size of id), global id
ns.ai8(1).ai8(4).ai32(PlayerManager::getCurrentOnlineId());
// 4 (size of id), global id
ns.ai8(LE_CONNECTION_REQUESTED).ai8(4)
.ai32(PlayerManager::getCurrentOnlineId());
sendMessage(ns);
m_state = REQUESTING_CONNECTION;
}

View File

@ -1,31 +0,0 @@
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2013-2015 SuperTuxKart-Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "network/protocols/lobby_room_protocol.hpp"
LobbyRoomProtocol::LobbyRoomProtocol(CallbackObject* callback_object)
: Protocol(PROTOCOL_LOBBY_ROOM, callback_object)
{
m_setup = NULL;
}
//-----------------------------------------------------------------------------
LobbyRoomProtocol::~LobbyRoomProtocol()
{
}

View File

@ -26,21 +26,50 @@
/*!
* \class LobbyRoomProtocol
* \brief Class used while the game is being prepared.
* This protocol starts when a server opens a game, or when a client joins a game.
* It is used to exchange data about the race settings, like kart selection.
* \brief Base class for both client and server lobby. The lobbies are started
* when a server opens a game, or when a client joins a game.
* It is used to exchange data about the race settings, like kart selection.
*/
class LobbyRoomProtocol : public Protocol
{
public:
LobbyRoomProtocol(CallbackObject* callback_object);
virtual ~LobbyRoomProtocol();
protected:
/** The game setup. */
GameSetup* m_setup;
virtual void setup() = 0;
virtual void update() = 0;
/** Lists all lobby events (LE). */
enum
{
LE_CONNECTION_REQUESTED = 1,
LE_NEW_PLAYER_CONNECTED = 0x01,
LE_KART_SELECTION = 2,
LE_PLAYER_DISCONNECTED = 0x02,
LE_KART_SELECTION_UPDATE = 0x03,
LE_START_RACE = 0x04,
LE_START_SELECTION = 0x05,
LE_RACE_FINISHED = 0x06,
LE_CONNECTION_REFUSED = 0x80,
LE_CONNECTION_ACCEPTED = 0x81,
LE_KART_SELECTION_REFUSED = 0x82,
LE_VOTE_MAJOR = 0xc0,
LE_VOTE_RACE_COUNT = 0xc1,
LE_VOTE_MINOR = 0xc2,
LE_VOTE_TRACK = 0xc3,
LE_VOTE_REVERSE = 0xc4,
LE_VOTE_LAPS = 0xc5,
};
protected:
GameSetup* m_setup; //!< The game setup.
};
public:
LobbyRoomProtocol(CallbackObject* callback_object)
: Protocol(PROTOCOL_LOBBY_ROOM, callback_object)
{
m_setup = NULL;
} // LobbyRoomProtocol
// ------------------------------------------------------------------------
virtual ~LobbyRoomProtocol() {}
// ------------------------------------------------------------------------
virtual void setup() = 0;
virtual void update() = 0;
}; // class LobbyRoomProtocol
#endif // LOBBY_ROOM_PROTOCOL_HPP

View File

@ -82,21 +82,21 @@ bool ServerLobbyRoomProtocol::notifyEventAsynchronous(Event* event)
event->removeFront(1);
Log::info("ServerLobbyRoomProtocol", "Message received with type %d.",
message_type);
if (message_type == 0x01) // player requesting connection
if (message_type == LE_CONNECTION_REQUESTED) // player requesting connection
connectionRequested(event);
else if (message_type == 0x02) // player requesting kart selection
else if (message_type == LE_KART_SELECTION) // player requesting kart selection
kartSelectionRequested(event);
else if (message_type == 0xc0) // vote for major mode
else if (message_type == LE_VOTE_MAJOR) // vote for major mode
playerMajorVote(event);
else if (message_type == 0xc1) // vote for race count
else if (message_type == LE_VOTE_RACE_COUNT) // vote for race count
playerRaceCountVote(event);
else if (message_type == 0xc2) // vote for minor mode
else if (message_type == LE_VOTE_MINOR) // vote for minor mode
playerMinorVote(event);
else if (message_type == 0xc3) // vote for track
else if (message_type == LE_VOTE_TRACK) // vote for track
playerTrackVote(event);
else if (message_type == 0xc4) // vote for reversed mode
else if (message_type == LE_VOTE_REVERSE) // vote for reversed mode
playerReversedVote(event);
else if (message_type == 0xc5) // vote for laps
else if (message_type == LE_VOTE_LAPS) // vote for laps
playerLapsVote(event);
} // if (event->getType() == EVENT_TYPE_MESSAGE)
else if (event->getType() == EVENT_TYPE_CONNECTED)
@ -223,7 +223,7 @@ void ServerLobbyRoomProtocol::startGame()
for (unsigned int i = 0; i < peers.size(); i++)
{
NetworkString ns(6);
ns.ai8(0x04).ai8(4).ai32(peers[i]->getClientServerToken()); // start game
ns.ai8(LE_START_RACE).ai8(4).ai32(peers[i]->getClientServerToken());
sendMessage(peers[i], ns, true); // reliably
}
Protocol *p = new StartGameProtocol(m_setup);
@ -240,7 +240,7 @@ void ServerLobbyRoomProtocol::startSelection()
{
NetworkString ns(6);
// start selection
ns.ai8(0x05).ai8(4).ai32(peers[i]->getClientServerToken());
ns.ai8(LE_START_SELECTION).ai8(4).ai32(peers[i]->getClientServerToken());
sendMessage(peers[i], ns, true); // reliably
}
m_selection_enabled = true;
@ -381,7 +381,8 @@ void ServerLobbyRoomProtocol::kartDisconnected(Event* event)
if (peer->getPlayerProfile() != NULL) // others knew him
{
NetworkString msg(3);
msg.ai8(0x02).ai8(1).ai8(peer->getPlayerProfile()->race_id);
msg.ai8(LE_PLAYER_DISCONNECTED).ai8(1)
.ai8(peer->getPlayerProfile()->race_id);
sendMessage(msg);
Log::info("ServerLobbyRoomProtocol", "Player disconnected : id %d",
peer->getPlayerProfile()->race_id);
@ -424,8 +425,9 @@ void ServerLobbyRoomProtocol::connectionRequested(Event* event)
m_next_id = m_setup->getPlayerCount();
// notify everybody that there is a new player
NetworkString message(8);
// new player (1) -- size of id -- id -- size of local id -- local id;
message.ai8(1).ai8(4).ai32(player_id).ai8(1).ai8(m_next_id);
// size of id -- id -- size of local id -- local id;
message.ai8(LE_NEW_PLAYER_CONNECTED).ai8(4).ai32(player_id)
.ai8(1).ai8(m_next_id);
ProtocolManager::getInstance()->sendMessageExcept(this, peer, message);
/// now answer to the peer that just connected
@ -441,8 +443,8 @@ void ServerLobbyRoomProtocol::connectionRequested(Event* event)
// Size is overestimated, probably one player's data will not be sent
NetworkString message_ack(13+players.size()*7);
// connection success (129) -- size of token -- token
message_ack.ai8(0x81).ai8(1).ai8(m_next_id).ai8(4).ai32(token).ai8(4)
.ai32(player_id);
message_ack.ai8(LE_CONNECTION_ACCEPTED).ai8(1).ai8(m_next_id).ai8(4)
.ai32(token).ai8(4).ai32(player_id);
// add all players so that this user knows
for (unsigned int i = 0; i < players.size(); i++)
{
@ -469,7 +471,7 @@ void ServerLobbyRoomProtocol::connectionRequested(Event* event)
else // refuse the connection with code 0 (too much players)
{
NetworkString message(3);
message.ai8(0x80); // 128 means connection refused
message.ai8(LE_CONNECTION_REFUSED);
message.ai8(1); // 1 bytes for the error code
message.ai8(0); // 0 = too much players
// send only to the peer that made the request
@ -509,7 +511,7 @@ void ServerLobbyRoomProtocol::kartSelectionRequested(Event* event)
if (!m_selection_enabled)
{
NetworkString answer(3);
answer.ai8(0x82).ai8(1).ai8(2); // selection still not started
answer.ai8(LE_KART_SELECTION_REFUSED).ai8(1).ai8(2); // selection still not started
sendMessage(peer, answer);
return;
}
@ -517,7 +519,7 @@ void ServerLobbyRoomProtocol::kartSelectionRequested(Event* event)
if (!m_setup->isKartAvailable(kart_name))
{
NetworkString answer(3);
answer.ai8(0x82).ai8(1).ai8(0); // kart is already taken
answer.ai8(LE_KART_SELECTION_REFUSED).ai8(1).ai8(0); // kart is already taken
sendMessage(peer, answer);
return;
}
@ -525,7 +527,7 @@ void ServerLobbyRoomProtocol::kartSelectionRequested(Event* event)
if (!m_setup->isKartAllowed(kart_name))
{
NetworkString answer(3);
answer.ai8(0x82).ai8(1).ai8(1); // kart is not authorized
answer.ai8(LE_KART_SELECTION_REFUSED).ai8(1).ai8(1); // kart is not authorized
sendMessage(peer, answer);
return;
}
@ -567,9 +569,10 @@ void ServerLobbyRoomProtocol::playerMajorVote(Event* event)
other.ai8(1).ai8(player_id); // add the player id
other += data; // add the data
NetworkString prefix(1);
prefix.ai8(0xc0); // prefix the token with the ype
prefix.ai8(LE_VOTE_MAJOR); // prefix the token with the type
sendMessageToPeersChangingToken(prefix, other);
}
} // playerMajorVote
//-----------------------------------------------------------------------------
/*! \brief Called when a player votes for the number of races in a GP.
@ -598,9 +601,10 @@ void ServerLobbyRoomProtocol::playerRaceCountVote(Event* event)
other.ai8(1).ai8(player_id); // add the player id
other += data; // add the data
NetworkString prefix(1);
prefix.ai8(0xc1); // prefix the token with the type
prefix.ai8(LE_VOTE_RACE_COUNT); // prefix the token with the type
sendMessageToPeersChangingToken(prefix, other);
}
} // playerRaceCountVote
//-----------------------------------------------------------------------------
/*! \brief Called when a player votes for a minor race mode.
@ -629,9 +633,10 @@ void ServerLobbyRoomProtocol::playerMinorVote(Event* event)
other.ai8(1).ai8(player_id); // add the player id
other += data; // add the data
NetworkString prefix(1);
prefix.ai8(0xc2); // prefix the token with the ype
prefix.ai8(LE_VOTE_MINOR); // prefix the token with the ype
sendMessageToPeersChangingToken(prefix, other);
}
} // playerMinorVote
//-----------------------------------------------------------------------------
/*! \brief Called when a player votes for a track.
@ -662,9 +667,10 @@ void ServerLobbyRoomProtocol::playerTrackVote(Event* event)
other.ai8(1).ai8(player_id); // add the player id
other += data; // add the data
NetworkString prefix(1);
prefix.ai8(0xc3); // prefix the token with the ype
prefix.ai8(LE_VOTE_TRACK); // prefix the token with the ype
sendMessageToPeersChangingToken(prefix, other);
}
} // playerTrackVote
//-----------------------------------------------------------------------------
/*! \brief Called when a player votes for the reverse mode of a race
@ -696,9 +702,10 @@ void ServerLobbyRoomProtocol::playerReversedVote(Event* event)
other.ai8(1).ai8(player_id); // add the player id
other += data; // add the data
NetworkString prefix(1);
prefix.ai8(0xc4); // prefix the token with the ype
prefix.ai8(LE_VOTE_REVERSE); // prefix the token with the ype
sendMessageToPeersChangingToken(prefix, other);
}
} // playerReversedVote
//-----------------------------------------------------------------------------
/*! \brief Called when a player votes for a major race mode.
@ -729,7 +736,8 @@ void ServerLobbyRoomProtocol::playerLapsVote(Event* event)
other.ai8(1).ai8(player_id); // add the player id
other += data; // add the data
NetworkString prefix(1);
prefix.ai8(0xc5); // prefix the token with the ype
prefix.ai8(LE_VOTE_LAPS); // prefix the token with the ype
sendMessageToPeersChangingToken(prefix, other);
}
} // playerLapsVote
//-----------------------------------------------------------------------------