Removed many unnecessary copies of NetworkString.

This commit is contained in:
hiker 2015-10-22 21:43:06 +11:00
parent c59f958c2f
commit e6ae208b4f
10 changed files with 50 additions and 41 deletions

View File

@ -82,11 +82,16 @@ public:
/** Returns the peer of this event. */
STKPeer* getPeer() const { return m_peer; }
// ------------------------------------------------------------------------
/** \brief Get a copy of the data.
* \return A copy of the message data. This is empty for events like
/** \brief Get a const reference to the received data.
* This is empty for events like connection or disconnections.
*/
const NetworkString& data() const { return m_data; }
// ------------------------------------------------------------------------
/** \brief Get a non-const reference to the received data.
* A copy of the message data. This is empty for events like
* connection or disconnections. */
NetworkString data() const { return m_data; }
NetworkString& data() { return m_data; }
// ------------------------------------------------------------------------
}; // class Event

View File

@ -39,10 +39,11 @@ typedef unsigned char uchar;
class NetworkString
{
private:
union {
union FloatAsInt
{
float f;
uint8_t i[4];
} f_as_i; // float as integer
}; // float as integer
// ------------------------------------------------------------------------
union {
@ -165,6 +166,7 @@ public:
NetworkString& addFloat(const float& value) //!< BEWARE OF PRECISION
{
assert(sizeof(float) == 4);
FloatAsInt f_as_i;
f_as_i.f = value;
m_string.push_back(f_as_i.i[0]);
m_string.push_back(f_as_i.i[1]);
@ -339,8 +341,9 @@ public:
// ------------------------------------------------------------------------
/** Returns a 4-byte floating point value. */
float getFloat(int pos = 0) //!< BEWARE OF PRECISION
float getFloat(int pos = 0) const //!< BEWARE OF PRECISION
{
FloatAsInt f_as_i;
for (int i = 0; i < 4; i++)
f_as_i.i[i] = m_string[pos + i];
return f_as_i.f;
@ -426,6 +429,7 @@ public:
/** Get and remove a 4 byte floating point value. */
float getAndRemoveFloat(int pos = 0) //!< BEWARE OF PRECISION
{
FloatAsInt f_as_i;
for (int i = 0; i < 4; i++)
f_as_i.i[i] = m_string[pos + i];
return f_as_i.f;

View File

@ -57,7 +57,7 @@ ProtocolType Protocol::getProtocolType()
bool Protocol::checkDataSizeAndToken(Event* event, int minimum_size)
{
NetworkString data = event->data();
const NetworkString &data = event->data();
if (data.size() < minimum_size || data[0] != 4)
{
Log::warn("Protocol", "Receiving a badly "
@ -78,7 +78,7 @@ bool Protocol::checkDataSizeAndToken(Event* event, int minimum_size)
bool Protocol::isByteCorrect(Event* event, int byte_nb, int value)
{
NetworkString data = event->data();
const NetworkString &data = event->data();
if (data[byte_nb] != value)
{
Log::info("Protocol", "Bad byte at pos %d. %d "

View File

@ -136,7 +136,7 @@ bool ClientLobbyRoomProtocol::notifyEvent(Event* event)
assert(m_setup); // assert that the setup exists
if (event->getType() == EVENT_TYPE_MESSAGE)
{
NetworkString data = event->data();
const NetworkString &data = event->data();
assert(data.size()); // assert that data isn't empty
uint8_t message_type = data[0];
if (message_type != 0x03 &&
@ -162,7 +162,7 @@ bool ClientLobbyRoomProtocol::notifyEventAsynchronous(Event* event)
assert(m_setup); // assert that the setup exists
if (event->getType() == EVENT_TYPE_MESSAGE)
{
NetworkString data = event->data();
const NetworkString &data = event->data();
assert(data.size()); // assert that data isn't empty
uint8_t message_type = data[0];
if (message_type == 0x03 ||
@ -286,7 +286,7 @@ void ClientLobbyRoomProtocol::update()
*/
void ClientLobbyRoomProtocol::newPlayer(Event* event)
{
NetworkString data = event->data();
const NetworkString &data = event->data();
if (data.size() != 7 || data[0] != 4 || data[5] != 1) // 7 bytes remains now
{
Log::error("ClientLobbyRoomProtocol", "A message notifying a new player wasn't formated as expected.");
@ -329,7 +329,7 @@ void ClientLobbyRoomProtocol::newPlayer(Event* event)
*/
void ClientLobbyRoomProtocol::disconnectedPlayer(Event* event)
{
NetworkString data = event->data();
const NetworkString &data = event->data();
if (data.size() != 2 || data[0] != 1)
{
Log::error("ClientLobbyRoomProtocol", "A message notifying a new player wasn't formated as expected.");
@ -360,7 +360,7 @@ void ClientLobbyRoomProtocol::disconnectedPlayer(Event* event)
*/
void ClientLobbyRoomProtocol::connectionAccepted(Event* event)
{
NetworkString data = event->data();
NetworkString &data = event->data();
if (data.size() < 12 || data[0] != 1 || data[2] != 4 || data[7] != 4) // 12 bytes remains now
{
Log::error("ClientLobbyRoomProtocol", "A message notifying an accepted connection wasn't formated as expected.");
@ -429,7 +429,7 @@ void ClientLobbyRoomProtocol::connectionAccepted(Event* event)
*/
void ClientLobbyRoomProtocol::connectionRefused(Event* event)
{
NetworkString data = event->data();
const NetworkString &data = event->data();
if (data.size() != 2 || data[0] != 1) // 2 bytes remains now
{
Log::error("ClientLobbyRoomProtocol", "A message notifying a refused connection wasn't formated as expected.");
@ -464,7 +464,7 @@ void ClientLobbyRoomProtocol::connectionRefused(Event* event)
*/
void ClientLobbyRoomProtocol::kartSelectionRefused(Event* event)
{
NetworkString data = event->data();
const NetworkString &data = event->data();
if (data.size() != 2 || data[0] != 1)
{
Log::error("ClientLobbyRoomProtocol", "A message notifying a refused kart selection wasn't formated as expected.");
@ -499,7 +499,7 @@ void ClientLobbyRoomProtocol::kartSelectionRefused(Event* event)
*/
void ClientLobbyRoomProtocol::kartSelectionUpdate(Event* event)
{
NetworkString data = event->data();
const NetworkString &data = event->data();
if (data.size() < 3 || data[0] != 1)
{
Log::error("ClientLobbyRoomProtocol", "A message notifying a kart selection update wasn't formated as expected.");
@ -535,7 +535,7 @@ void ClientLobbyRoomProtocol::kartSelectionUpdate(Event* event)
*/
void ClientLobbyRoomProtocol::startGame(Event* event)
{
NetworkString data = event->data();
const NetworkString &data = event->data();
if (data.size() < 5 || data[0] != 4)
{
Log::error("ClientLobbyRoomProtocol", "A message notifying a kart "
@ -568,7 +568,7 @@ void ClientLobbyRoomProtocol::startGame(Event* event)
*/
void ClientLobbyRoomProtocol::startSelection(Event* event)
{
NetworkString data = event->data();
const NetworkString &data = event->data();
if (data.size() < 5 || data[0] != 4)
{
Log::error("ClientLobbyRoomProtocol", "A message notifying a kart "
@ -600,12 +600,12 @@ void ClientLobbyRoomProtocol::startSelection(Event* event)
*/
void ClientLobbyRoomProtocol::raceFinished(Event* event)
{
if (event->data().size() < 5)
NetworkString &data = event->data();
if (data.size() < 5)
{
Log::error("ClientLobbyRoomProtocol", "Not enough data provided.");
return;
}
NetworkString data = event->data();
if (event->getPeer()->getClientServerToken() != data.gui32(1))
{
Log::error("ClientLobbyRoomProtocol", "Bad token");
@ -676,7 +676,7 @@ void ClientLobbyRoomProtocol::raceFinished(Event* event)
*/
void ClientLobbyRoomProtocol::playerMajorVote(Event* event)
{
NetworkString data = event->data();
const NetworkString &data = event->data();
if (!checkDataSizeAndToken(event, 9))
return;
if (!isByteCorrect(event, 5, 1))
@ -699,7 +699,7 @@ void ClientLobbyRoomProtocol::playerMajorVote(Event* event)
*/
void ClientLobbyRoomProtocol::playerRaceCountVote(Event* event)
{
NetworkString data = event->data();
const NetworkString &data = event->data();
if (!checkDataSizeAndToken(event, 9))
return;
if (!isByteCorrect(event, 5, 1))
@ -722,7 +722,7 @@ void ClientLobbyRoomProtocol::playerRaceCountVote(Event* event)
*/
void ClientLobbyRoomProtocol::playerMinorVote(Event* event)
{
NetworkString data = event->data();
const NetworkString &data = event->data();
if (!checkDataSizeAndToken(event, 9))
return;
if (!isByteCorrect(event, 5, 1))
@ -746,7 +746,7 @@ void ClientLobbyRoomProtocol::playerMinorVote(Event* event)
*/
void ClientLobbyRoomProtocol::playerTrackVote(Event* event)
{
NetworkString data = event->data();
const NetworkString &data = event->data();
if (!checkDataSizeAndToken(event, 10))
return;
if (!isByteCorrect(event, 5, 1))
@ -772,7 +772,7 @@ void ClientLobbyRoomProtocol::playerTrackVote(Event* event)
*/
void ClientLobbyRoomProtocol::playerReversedVote(Event* event)
{
NetworkString data = event->data();
const NetworkString &data = event->data();
if (!checkDataSizeAndToken(event, 11))
return;
if (!isByteCorrect(event, 5, 1))
@ -798,7 +798,7 @@ void ClientLobbyRoomProtocol::playerReversedVote(Event* event)
*/
void ClientLobbyRoomProtocol::playerLapsVote(Event* event)
{
NetworkString data = event->data();
const NetworkString &data = event->data();
if (!checkDataSizeAndToken(event, 9))
return;
if (!isByteCorrect(event, 5, 1))

View File

@ -66,7 +66,7 @@ void ControllerEventsProtocol::setup()
bool ControllerEventsProtocol::notifyEventAsynchronous(Event* event)
{
NetworkString data = event->data();
const NetworkString &data = event->data();
if (data.size() < 17)
{
Log::error("ControllerEventsProtocol", "The data supplied was not complete. Size was %d.", data.size());

View File

@ -24,7 +24,7 @@ bool GameEventsProtocol::notifyEvent(Event* event)
{
if (event->getType() != EVENT_TYPE_MESSAGE)
return true;
NetworkString data = event->data();
NetworkString &data = event->data();
if (data.size() < 5) // for token and type
{
Log::warn("GameEventsProtocol", "Too short message.");

View File

@ -34,7 +34,7 @@ bool KartUpdateProtocol::notifyEventAsynchronous(Event* event)
{
if (event->getType() != EVENT_TYPE_MESSAGE)
return true;
NetworkString ns = event->data();
NetworkString &ns = event->data();
if (ns.size() < 36)
{
Log::info("KartUpdateProtocol", "Message too short.");

View File

@ -66,7 +66,7 @@ bool ServerLobbyRoomProtocol::notifyEventAsynchronous(Event* event)
assert(m_setup); // assert that the setup exists
if (event->getType() == EVENT_TYPE_MESSAGE)
{
NetworkString data = event->data();
const NetworkString &data = event->data();
assert(data.size()); // message not empty
uint8_t message_type;
message_type = data[0];
@ -336,7 +336,7 @@ void ServerLobbyRoomProtocol::kartDisconnected(Event* event)
void ServerLobbyRoomProtocol::connectionRequested(Event* event)
{
STKPeer* peer = event->getPeer();
NetworkString data = event->data();
const NetworkString &data = event->data();
if (data.size() != 5 || data[0] != 4)
{
Log::warn("ServerLobbyRoomProtocol", "Receiving badly formated message. Size is %d and first byte %d", data.size(), data[0]);
@ -415,7 +415,7 @@ void ServerLobbyRoomProtocol::connectionRequested(Event* event)
*/
void ServerLobbyRoomProtocol::kartSelectionRequested(Event* event)
{
NetworkString data = event->data();
const NetworkString &data = event->data();
STKPeer* peer = event->getPeer();
if (!checkDataSizeAndToken(event, 6))
return;
@ -476,7 +476,7 @@ void ServerLobbyRoomProtocol::kartSelectionRequested(Event* event)
*/
void ServerLobbyRoomProtocol::playerMajorVote(Event* event)
{
NetworkString data = event->data();
NetworkString &data = event->data();
STKPeer* peer = event->getPeer();
if (!checkDataSizeAndToken(event, 7))
return;
@ -507,7 +507,7 @@ void ServerLobbyRoomProtocol::playerMajorVote(Event* event)
*/
void ServerLobbyRoomProtocol::playerRaceCountVote(Event* event)
{
NetworkString data = event->data();
NetworkString &data = event->data();
STKPeer* peer = event->getPeer();
if (!checkDataSizeAndToken(event, 7))
return;
@ -538,7 +538,7 @@ void ServerLobbyRoomProtocol::playerRaceCountVote(Event* event)
*/
void ServerLobbyRoomProtocol::playerMinorVote(Event* event)
{
NetworkString data = event->data();
NetworkString &data = event->data();
STKPeer* peer = event->getPeer();
if (!checkDataSizeAndToken(event, 7))
return;
@ -569,7 +569,7 @@ void ServerLobbyRoomProtocol::playerMinorVote(Event* event)
*/
void ServerLobbyRoomProtocol::playerTrackVote(Event* event)
{
NetworkString data = event->data();
NetworkString &data = event->data();
STKPeer* peer = event->getPeer();
if (!checkDataSizeAndToken(event, 8))
return;
@ -602,7 +602,7 @@ void ServerLobbyRoomProtocol::playerTrackVote(Event* event)
*/
void ServerLobbyRoomProtocol::playerReversedVote(Event* event)
{
NetworkString data = event->data();
NetworkString &data = event->data();
STKPeer* peer = event->getPeer();
if (!checkDataSizeAndToken(event, 9))
return;
@ -635,7 +635,7 @@ void ServerLobbyRoomProtocol::playerReversedVote(Event* event)
*/
void ServerLobbyRoomProtocol::playerLapsVote(Event* event)
{
NetworkString data = event->data();
NetworkString &data = event->data();
STKPeer* peer = event->getPeer();
if (!checkDataSizeAndToken(event, 9))
return;

View File

@ -37,7 +37,7 @@ StartGameProtocol::~StartGameProtocol()
bool StartGameProtocol::notifyEventAsynchronous(Event* event)
{
NetworkString data = event->data();
const NetworkString &data = event->data();
if (data.size() < 5)
{
Log::error("StartGameProtocol", "Too short message.");

View File

@ -36,7 +36,7 @@ bool SynchronizationProtocol::notifyEventAsynchronous(Event* event)
{
if (event->getType() != EVENT_TYPE_MESSAGE)
return true;
NetworkString data = event->data();
const NetworkString &data = event->data();
if (data.size() < 10)
{
Log::warn("SynchronizationProtocol", "Received a message too short.");