Started to set token in the 'lower level' functions, removing the need

to set the token in each protocol. Started to remove some of the
unnecessary send* functions.
This commit is contained in:
hiker 2016-02-28 15:54:43 +11:00
parent 9a16595261
commit 58505eeb88
21 changed files with 58 additions and 84 deletions

View File

@ -59,7 +59,7 @@ Event::Event(ENetEvent* event)
}
m_peer = STKHost::get()->getPeer(event->peer);
if(m_type == EVENT_TYPE_MESSAGE &&
if(m_type == EVENT_TYPE_MESSAGE && m_peer->isClientServerTokenSet() &&
m_data->getToken()!=m_peer->getClientServerToken() )
{
Log::error("Event", "Received event with invalid token!");

View File

@ -160,9 +160,9 @@ int Network::receiveRawPacket(char *buffer, int buf_len,
/** \brief Broadcasts a packet to all peers.
* \param data : Data to send.
*/
void Network::broadcastPacket(const NetworkString& data, bool reliable)
void Network::broadcastPacket(NetworkString *data, bool reliable)
{
ENetPacket* packet = enet_packet_create(data.getData(), data.size() + 1,
ENetPacket* packet = enet_packet_create(data->getData(), data->size() + 1,
reliable ? ENET_PACKET_FLAG_RELIABLE
: ENET_PACKET_FLAG_UNSEQUENCED);
enet_host_broadcast(m_host, 0, packet);

View File

@ -65,7 +65,7 @@ public:
const TransportAddress& dst);
int receiveRawPacket(char *buffer, int buf_len,
TransportAddress* sender, int max_tries = -1);
void broadcastPacket(const NetworkString& data,
void broadcastPacket(NetworkString *data,
bool reliable = true);
// ------------------------------------------------------------------------
/** Returns a pointer to the ENet host object. */

View File

@ -173,7 +173,7 @@ void NetworkConsole::kickAllPlayers()
} // kickAllPlayers
// ----------------------------------------------------------------------------
void NetworkConsole::sendPacket(const NetworkString& data, bool reliable)
void NetworkConsole::sendPacket(NetworkString *data, bool reliable)
{
m_localhost->broadcastPacket(data, reliable);
} // sendPacket

View File

@ -44,7 +44,7 @@ public:
virtual void run();
void kickAllPlayers();
virtual void sendPacket(const NetworkString& data,
virtual void sendPacket(NetworkString *data,
bool reliable = true);
// ------------------------------------------------------------------------
void setMaxPlayers(uint8_t count) { m_max_players = count; }

View File

@ -134,21 +134,12 @@ void Protocol::sendMessageToPeersChangingToken(NetworkString *message)
const std::vector<STKPeer*> &peers = STKHost::get()->getPeers();
for (unsigned int i = 0; i < peers.size(); i++)
{
message->setToken(peers[i]->getClientServerToken());
ProtocolManager::getInstance()->sendMessage(peers[i], *message);
peers[i]->sendPacket(message);
}
} // sendMessageToPeersChangingToken
// ----------------------------------------------------------------------------
void Protocol::sendMessage(const NetworkString &message, bool reliable)
void Protocol::sendMessage(NetworkString *message, bool reliable)
{
ProtocolManager::getInstance()->sendMessage(message, reliable);
} // sendMessage
// ----------------------------------------------------------------------------
void Protocol::sendMessage(STKPeer* peer, const NetworkString &message,
bool reliable)
{
ProtocolManager::getInstance()->sendMessage(peer, message, reliable);
} // sendMessage

View File

@ -125,9 +125,7 @@ public:
bool checkDataSizeAndToken(Event* event, unsigned int minimum_size);
bool isByteCorrect(Event* event, int byte_nb, int value);
void sendMessageToPeersChangingToken(NetworkString *message);
void sendMessage(const NetworkString &message,
bool reliable = true);
void sendMessage(STKPeer* peer, const NetworkString &message,
void sendMessage(NetworkString *message,
bool reliable = true);
void requestStart();
void requestPause();

View File

@ -176,22 +176,14 @@ void ProtocolManager::propagateEvent(Event* event)
} // propagateEvent
// ----------------------------------------------------------------------------
void ProtocolManager::sendMessage(const NetworkString &message, bool reliable)
void ProtocolManager::sendMessage(NetworkString *message, bool reliable)
{
STKHost::get()->sendMessage(message, reliable);
} // sendMessage
// ----------------------------------------------------------------------------
void ProtocolManager::sendMessage(STKPeer *peer,
const NetworkString &message,
bool reliable)
{
peer->sendPacket(message, reliable);
} // sendMessage
// ----------------------------------------------------------------------------
void ProtocolManager::sendMessageExcept(STKPeer *peer,
const NetworkString &message,
NetworkString *message,
bool reliable)
{
STKHost::get()->sendPacketExcept(peer, message, reliable);

View File

@ -162,13 +162,10 @@ private:
public:
virtual void abort();
virtual void propagateEvent(Event* event);
virtual void sendMessage(const NetworkString &message,
bool reliable = true);
virtual void sendMessage(STKPeer* peer,
const NetworkString &message,
virtual void sendMessage(NetworkString *message,
bool reliable = true);
virtual void sendMessageExcept(STKPeer* peer,
const NetworkString &message,
NetworkString *message,
bool reliable = true);
virtual uint32_t requestStart(Protocol* protocol);
virtual void requestPause(Protocol* protocol);

View File

@ -68,7 +68,7 @@ void ClientLobbyRoomProtocol::requestKartSelection(const std::string &kart_name)
NetworkString *r = getNetworkString(7+kart_name.size());
r->setToken(m_server->getClientServerToken());
r->addUInt8(LE_KART_SELECTION).addUInt8(4).encodeString(kart_name);
sendMessage(*r, true);
sendMessage(r, true);
delete r;
} // requestKartSelection
@ -81,7 +81,7 @@ void ClientLobbyRoomProtocol::voteMajor(uint32_t major)
// size_token (4), token, size major(1),major
request->addUInt8(LE_VOTE_MAJOR).addUInt8(4)
.addUInt8(4).addUInt32(major);
sendMessage(*request, true);
sendMessage(request, true);
delete request;
} // voteMajor
@ -94,7 +94,7 @@ void ClientLobbyRoomProtocol::voteRaceCount(uint8_t count)
// size_token (4), token, size race count(1), count
request->addUInt8(LE_VOTE_RACE_COUNT).addUInt8(4)
.addUInt8(1).addUInt8(count);
sendMessage(*request, true);
sendMessage(request, true);
delete request;
} // voteRaceCount
@ -107,7 +107,7 @@ void ClientLobbyRoomProtocol::voteMinor(uint32_t minor)
// size_token (4), token, size minor(1),minor
request->addUInt8(LE_VOTE_MINOR).addUInt8(4)
.addUInt8(4).addUInt32(minor);
sendMessage(*request, true);
sendMessage(request, true);
delete request;
} // voteMinor
@ -121,7 +121,7 @@ void ClientLobbyRoomProtocol::voteTrack(const std::string &track,
// size_token (4), token, size track, track, size #track, #track
request->addUInt8(LE_VOTE_TRACK).addUInt8(4)
.encodeString(track).addUInt8(1).addUInt8(track_nb);
sendMessage(*request, true);
sendMessage(request, true);
delete request;
} // voteTrack
@ -134,7 +134,7 @@ void ClientLobbyRoomProtocol::voteReversed(bool reversed, uint8_t track_nb)
// size_token (4), token, size reversed(1),reversed, size #track, #track
request->addUInt8(LE_VOTE_REVERSE).addUInt8(4)
.addUInt8(1).addUInt8(reversed).addUInt8(1).addUInt8(track_nb);
sendMessage(*request, true);
sendMessage(request, true);
delete request;
} // voteReversed
@ -147,7 +147,7 @@ void ClientLobbyRoomProtocol::voteLaps(uint8_t laps, uint8_t track_nb)
// size_token (4), token, size laps(1),laps, size #track, #track
request->addUInt8(LE_VOTE_LAPS)
.addUInt8(1).addUInt8(laps).addUInt8(1).addUInt8(track_nb);
sendMessage(*request, true);
sendMessage(request, true);
} // voteLaps
//-----------------------------------------------------------------------------
@ -265,7 +265,7 @@ void ClientLobbyRoomProtocol::update()
// 4 (size of id), global id
ns->addUInt8(LE_CONNECTION_REQUESTED).encodeString(name)
.encodeString(NetworkConfig::get()->getPassword());
sendMessage(*ns);
sendMessage(ns);
delete ns;
m_state = REQUESTING_CONNECTION;
}
@ -634,7 +634,7 @@ void ClientLobbyRoomProtocol::startSelection(Event* event)
if (data.size() < 5 || data[0] != 4)
{
Log::error("ClientLobbyRoomProtocol", "A message notifying a kart "
"selection update wasn't formated as expected.");
"selection start wasn't formated as expected.");
return;
}
uint32_t token = data.getUInt32(1);

View File

@ -114,8 +114,7 @@ bool ControllerEventsProtocol::notifyEventAsynchronous(Event* event)
if(peer != event->getPeer())
{
pure_message.setToken(peer->getClientServerToken());
ProtocolManager::getInstance()->sendMessage(peer, pure_message,
false);
peer->sendPacket(&pure_message, false);
} // if peer != event->getPeer()
} // for i in peers
} // if server
@ -165,5 +164,5 @@ void ControllerEventsProtocol::controllerAction(Controller* controller,
ns->addUInt8((uint8_t)(action)).addUInt32(value);
Log::info("ControllerEventsProtocol", "Action %d value %d", action, value);
sendMessage(*ns, false); // send message to server
sendMessage(ns, false); // send message to server
} // controllerAction

View File

@ -98,8 +98,7 @@ void GameEventsProtocol::collectedItem(Item* item, AbstractKart* kart)
ns->addUInt8(GE_ITEM_COLLECTED).addUInt32(item->getItemId())
.addUInt8(powerup).addUInt8(kart->getWorldKartId());
ProtocolManager::getInstance()->sendMessage(peers[i], *ns,
/*reliable*/true);
peers[i]->sendPacket(ns, /*reliable*/true);
delete ns;
Log::info("GameEventsProtocol",
"Notified a peer that a kart collected item %d.",
@ -145,9 +144,7 @@ void GameEventsProtocol::kartFinishedRace(AbstractKart *kart, float time)
ns->addUInt32(peers[i]->getClientServerToken())
.addUInt8(GE_KART_FINISHED_RACE)
.addUInt8(kart->getWorldKartId()).addFloat(time);
ProtocolManager::getInstance()->sendMessage(peers[i], *ns,
/*reliable*/true);
peers[i]->sendPacket(ns, /*reliable*/true);
delete ns;
} // for i in peers
} // kartFinishedRace

View File

@ -93,7 +93,7 @@ void KartUpdateProtocol::update()
"Sending %d's positions %f %f %f",
kart->getWorldKartId(), xyz[0], xyz[1], xyz[2]);
}
sendMessage(*ns, /*reliable*/false);
sendMessage(ns, /*reliable*/false);
delete ns;
}
else
@ -112,7 +112,7 @@ void KartUpdateProtocol::update()
"Sending %d's positions %f %f %f",
kart->getWorldKartId(), xyz[0], xyz[1], xyz[2]);
}
sendMessage(*ns, /*reliable*/false);
sendMessage(ns, /*reliable*/false);
delete ns;
} // if server
} // if (current_time > time + 0.1)

View File

@ -229,7 +229,7 @@ void ServerLobbyRoomProtocol::startGame()
NetworkString *ns = getNetworkString(6);
ns->setToken(peers[i]->getClientServerToken());
ns->addUInt8(LE_START_RACE).addUInt8(4);
sendMessage(peers[i], *ns, true); // reliably
peers[i]->sendPacket(ns, /*reliable*/true);
delete ns;
}
Protocol *p = new StartGameProtocol(m_setup);
@ -251,15 +251,15 @@ void ServerLobbyRoomProtocol::startSelection(const Event *event)
return;
}
const std::vector<STKPeer*> &peers = STKHost::get()->getPeers();
NetworkString *ns = getNetworkString(6);
// start selection
ns->addUInt8(LE_START_SELECTION);
for (unsigned int i = 0; i < peers.size(); i++)
{
NetworkString *ns = getNetworkString(6);
ns->setToken(peers[i]->getClientServerToken());
// start selection
ns->addUInt8(LE_START_SELECTION).addUInt8(4);
sendMessage(peers[i], *ns, true); // reliably
delete ns;
peers[i]->sendPacket(ns, /*reliable*/true);
}
delete ns;
m_selection_enabled = true;
m_state = SELECTING_KARTS;
@ -356,7 +356,7 @@ void ServerLobbyRoomProtocol::checkRaceFinished()
total->setToken(peers[i]->getClientServerToken());
total->addUInt8(LE_RACE_FINISHED).addUInt8(4);
*total += *queue;
sendMessage(peers[i], *total, /*reliable*/true);
peers[i]->sendPacket(total, /*reliable*/true);
delete total;
}
Log::info("ServerLobbyRoomProtocol", "End of game message sent");
@ -405,7 +405,7 @@ void ServerLobbyRoomProtocol::kartDisconnected(Event* event)
NetworkString *msg = getNetworkString(3);
msg->addUInt8(LE_PLAYER_DISCONNECTED).addUInt8(1)
.addUInt8(peer->getPlayerProfile()->getGlobalPlayerId());
sendMessage(*msg);
sendMessage(msg);
delete msg;
Log::info("ServerLobbyRoomProtocol", "Player disconnected : id %d",
peer->getPlayerProfile()->getGlobalPlayerId());
@ -445,7 +445,7 @@ void ServerLobbyRoomProtocol::connectionRequested(Event* event)
.addUInt8(m_state!=ACCEPTING_CLIENTS ? 2 : 0);
// send only to the peer that made the request
sendMessage(peer, *message);
peer->sendPacket(message);
delete message;
Log::verbose("ServerLobbyRoomProtocol", "Player refused");
return;
@ -478,7 +478,7 @@ void ServerLobbyRoomProtocol::connectionRequested(Event* event)
// size of id -- id -- size of local id -- local id;
message->addUInt8(LE_NEW_PLAYER_CONNECTED).addUInt8(1).addUInt8(new_player_id)
.encodeString(name_u8).addUInt8(new_host_id);
ProtocolManager::getInstance()->sendMessageExcept(peer, *message);
ProtocolManager::getInstance()->sendMessageExcept(peer, message);
delete message;
// Now answer to the peer that just connected
@ -506,7 +506,7 @@ void ServerLobbyRoomProtocol::connectionRequested(Event* event)
.addUInt8(players[i]->getHostId())
.encodeString(players[i]->getName());
}
sendMessage(peer, *message_ack);
peer->sendPacket(message_ack);
delete message_ack;
NetworkPlayerProfile* profile = new NetworkPlayerProfile(new_player_id, name);
@ -555,7 +555,7 @@ void ServerLobbyRoomProtocol::kartSelectionRequested(Event* event)
NetworkString *answer = getNetworkString(3);
// selection still not started
answer->addUInt8(LE_KART_SELECTION_REFUSED).addUInt8(1).addUInt8(2);
sendMessage(peer, *answer);
peer->sendPacket(answer);
delete answer;
return;
}
@ -565,7 +565,7 @@ void ServerLobbyRoomProtocol::kartSelectionRequested(Event* event)
NetworkString *answer = getNetworkString(3);
// kart is already taken
answer->addUInt8(LE_KART_SELECTION_REFUSED).addUInt8(1).addUInt8(0);
sendMessage(peer, *answer);
peer->sendPacket(answer);
delete answer;
return;
}
@ -575,7 +575,7 @@ void ServerLobbyRoomProtocol::kartSelectionRequested(Event* event)
NetworkString *answer = getNetworkString(3);
// kart is not authorized
answer->addUInt8(LE_KART_SELECTION_REFUSED).addUInt8(1).addUInt8(1);
sendMessage(peer, *answer);
peer->sendPacket(answer);
delete answer;
return;
}
@ -587,7 +587,7 @@ void ServerLobbyRoomProtocol::kartSelectionRequested(Event* event)
uint8_t player_id = peer->getPlayerProfile()->getGlobalPlayerId();
answer->addUInt8(LE_KART_SELECTION_UPDATE).addUInt8(1).addUInt8(player_id)
.encodeString(kart_name);
sendMessage(*answer);
sendMessage(answer);
delete answer;
m_setup->setPlayerKart(player_id, kart_name);
} // kartSelectionRequested

View File

@ -223,7 +223,7 @@ void StartGameProtocol::ready()
ns->setToken(STKHost::get()->getPeers()[0]->getClientServerToken());
ns->addUInt8(1);
Log::info("StartGameProtocol", "Player ready, notifying server.");
sendMessage(*ns, /*reliable*/true);
sendMessage(ns, /*reliable*/true);
delete ns;
m_state = READY;
m_ready = true;

View File

@ -83,7 +83,7 @@ bool SynchronizationProtocol::notifyEventAsynchronous(Event* event)
response->setToken(token);
// The '0' indicates a response to a ping request
response->addUInt8(0).addUInt32(sequence);
sendMessage(event->getPeer(), *response, false);
event->getPeer()->sendPacket(response, false);
delete response;
Log::verbose("SynchronizationProtocol", "Answering sequence %u at %lf",
sequence, StkTime::getRealTime());
@ -172,7 +172,6 @@ void SynchronizationProtocol::asynchronousUpdate()
for (unsigned int i = 0; i < peers.size(); i++)
{
NetworkString *ping_request = getNetworkString(13);
ping_request->setToken(peers[i]->getClientServerToken());
ping_request->addUInt8(1).addUInt32(m_pings[i].size());
// Server adds the countdown if it has started. This will indicate
// to the client to start the countdown as well (first time the
@ -187,7 +186,7 @@ void SynchronizationProtocol::asynchronousUpdate()
"Added sequence number %u for peer %d at %lf",
m_pings[i].size(), i, StkTime::getRealTime());
m_pings[i] [ m_pings_count ] = current_time;
sendMessage(peers[i], *ping_request, false);
peers[i]->sendPacket(ping_request, false);
delete ping_request;
} // for i M peers
m_last_time = current_time;

View File

@ -440,7 +440,7 @@ bool STKHost::connect(const TransportAddress& address)
* \param data Message to sent.
* \param reliable If the message is to be sent reliable.
*/
void STKHost::sendMessage(const NetworkString& data, bool reliable)
void STKHost::sendMessage(NetworkString *data, bool reliable)
{
if (NetworkConfig::get()->isServer())
broadcastPacket(data, reliable);
@ -743,7 +743,7 @@ uint16_t STKHost::getPort() const
* \param data Data to sent.
* \param reliable If the data should be sent reliable or now.
*/
void STKHost::sendPacketExcept(STKPeer* peer, const NetworkString &data,
void STKHost::sendPacketExcept(STKPeer* peer, NetworkString *data,
bool reliable)
{
for (unsigned int i = 0; i < m_peers.size(); i++)

View File

@ -145,9 +145,9 @@ public:
void requestShutdown();
void shutdown();
void sendMessage(const NetworkString& data, bool reliable = true);
void sendMessage(NetworkString *data, bool reliable = true);
void sendPacketExcept(STKPeer* peer,
const NetworkString& data,
NetworkString *data,
bool reliable = true);
void setupClient(int peer_count, int channel_limit,
uint32_t max_incoming_bandwidth,
@ -181,7 +181,7 @@ public:
} // receiveRawPacket
// --------------------------------------------------------------------
void broadcastPacket(const NetworkString& data,
void broadcastPacket(NetworkString *data,
bool reliable = true)
{
m_network->broadcastPacket(data, reliable);

View File

@ -62,13 +62,14 @@ void STKPeer::disconnect()
* \param data The data to send.
* \param reliable If the data is sent reliable or not.
*/
void STKPeer::sendPacket(NetworkString const& data, bool reliable)
void STKPeer::sendPacket(NetworkString *data, bool reliable)
{
data->setToken(m_client_server_token);
TransportAddress a(m_enet_peer->address);
Log::verbose("STKPeer", "sending packet of size %d to %s",
data.size(), a.toString().c_str());
data->size(), a.toString().c_str());
ENetPacket* packet = enet_packet_create(data.getData(), data.size(),
ENetPacket* packet = enet_packet_create(data->getData(), data->size(),
(reliable ? ENET_PACKET_FLAG_RELIABLE
: ENET_PACKET_FLAG_UNSEQUENCED));
enet_peer_send(m_enet_peer, 0, packet);

View File

@ -59,7 +59,7 @@ public:
STKPeer(ENetPeer *enet_peer);
virtual ~STKPeer();
virtual void sendPacket(const NetworkString &data,
virtual void sendPacket(NetworkString *data,
bool reliable = true);
void disconnect();
bool isConnected() const;

View File

@ -143,7 +143,7 @@ void NetworkingLobby::eventCallback(Widget* widget, const std::string& name,
// Send a message to the server to start
NetworkString start(PROTOCOL_LOBBY_ROOM);
start.addUInt8(LobbyRoomProtocol::LE_REQUEST_BEGIN);
STKHost::get()->sendMessage(start, true);
STKHost::get()->sendMessage(&start, true);
}
RibbonWidget* ribbon = dynamic_cast<RibbonWidget*>(widget);