Another refactoring of NetworkString, this time added support for sequential

reads, so that you don't need to specify the offset anymore (and no more
removeFromFront).
This commit is contained in:
hiker 2016-03-13 13:49:43 +11:00
parent 96bf567c2e
commit f2c26f06f1
13 changed files with 157 additions and 149 deletions

View File

@ -46,11 +46,9 @@ void NetworkString::unitTesting()
// Append some values from the message // Append some values from the message
s.addUInt16(12345); s.addUInt16(12345);
s.addFloat(1.2345f); s.addFloat(1.2345f);
// Ignore message type and token
s.removeFront(5);
assert(s.getUInt16(0) == 12345); assert(s.getUInt16() == 12345);
float f = s.getFloat(2); float f = s.getFloat();
assert(f==1.2345f); assert(f==1.2345f);
// Check modifying a token in an already assembled message // Check modifying a token in an already assembled message
@ -98,27 +96,26 @@ BareNetworkString& BareNetworkString::encodeString(const irr::core::stringw &val
* \param[out] out The decoded string. * \param[out] out The decoded string.
* \return number of bytes read = 1+length of string * \return number of bytes read = 1+length of string
*/ */
int BareNetworkString::decodeString(int pos, std::string *out) const int BareNetworkString::decodeString(std::string *out) const
{ {
uint8_t len = get<uint8_t>(m_current_offset+pos); uint8_t len = get<uint8_t>();
*out = getString(pos+1, len); *out = getString(len);
return len+1; return len+1;
} // decodeString } // decodeString
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** Returns an irrlicht wide string from the utf8 encoded string at the /** Returns an irrlicht wide string from the utf8 encoded string at the
* given position. * given position.
* \param[in] pos Buffer position where the encoded string starts.
* \param[out] out The decoded string. * \param[out] out The decoded string.
* \return number of bytes read. If there are no special characters in the * \return number of bytes read. If there are no special characters in the
* string that will be 1+length of string, but multi-byte encoded * string that will be 1+length of string, but multi-byte encoded
* characters can mean that the length of the returned string is * characters can mean that the length of the returned string is
* less than the number of bytes read. * less than the number of bytes read.
*/ */
int BareNetworkString::decodeStringW(int pos, irr::core::stringw *out) const int BareNetworkString::decodeStringW(irr::core::stringw *out) const
{ {
std::string s; std::string s;
int len = decodeString(pos, &s); int len = decodeString(&s);
*out = StringUtils::utf8ToWide(s); *out = StringUtils::utf8ToWide(s);
return len; return len;
} // decodeString } // decodeString

View File

@ -64,7 +64,7 @@ protected:
* should be left as signed, otherwise certain arithmetic (e.g. * should be left as signed, otherwise certain arithmetic (e.g.
* 1-m_current_offset in checkToken() will be done unsigned). * 1-m_current_offset in checkToken() will be done unsigned).
*/ */
int m_current_offset; mutable int m_current_offset;
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Returns a part of the network string as a std::string. This is an /** Returns a part of the network string as a std::string. This is an
@ -72,10 +72,12 @@ protected:
* \param pos First position to be in the string. * \param pos First position to be in the string.
* \param len Number of bytes to copy. * \param len Number of bytes to copy.
*/ */
std::string getString(int pos, int len) const std::string getString(int len) const
{ {
return std::string(m_buffer.begin() + (m_current_offset+pos ), std::string a(m_buffer.begin() + (m_current_offset ),
m_buffer.begin() + (m_current_offset+pos + len) ); m_buffer.begin() + (m_current_offset + len));
m_current_offset += len;
return a;
} // getString } // getString
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Adds a std::string. Internal use only. */ /** Adds a std::string. Internal use only. */
@ -89,11 +91,12 @@ protected:
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Template to get n bytes from a buffer into a single data type. */ /** Template to get n bytes from a buffer into a single data type. */
template<typename T, size_t n> template<typename T, size_t n>
T get(int pos) const T get() const
{ {
int a = n; int a = n;
T result = 0; T result = 0;
int offset = m_current_offset + pos + n -1; m_current_offset += n;
int offset = m_current_offset -1;
while (a--) while (a--)
{ {
result <<= 8; // offset one byte result <<= 8; // offset one byte
@ -105,9 +108,9 @@ protected:
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Another function for n == 1 to surpress warnings in clang. */ /** Another function for n == 1 to surpress warnings in clang. */
template<typename T> template<typename T>
T get(int pos) const T get() const
{ {
return m_buffer[pos]; return m_buffer[m_current_offset++];
} // get } // get
public: public:
@ -137,8 +140,8 @@ public:
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
BareNetworkString& encodeString(const std::string &value); BareNetworkString& encodeString(const std::string &value);
BareNetworkString& encodeString(const irr::core::stringw &value); BareNetworkString& encodeString(const irr::core::stringw &value);
int decodeString(int n, std::string *out) const; int decodeString(std::string *out) const;
int decodeStringW(int n, irr::core::stringw *out) const; int decodeStringW(irr::core::stringw *out) const;
std::string getLogMessage(const std::string &indent="") const; std::string getLogMessage(const std::string &indent="") const;
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Returns a byte pointer to the content of the network string. */ /** Returns a byte pointer to the content of the network string. */
@ -152,6 +155,14 @@ public:
/** Returns the remaining length of the network string. */ /** Returns the remaining length of the network string. */
unsigned int size() const { return (int)m_buffer.size()-m_current_offset; } unsigned int size() const { return (int)m_buffer.size()-m_current_offset; }
// ------------------------------------------------------------------------
/** Skips the specified number of bytes when reading. */
void skip(int n)
{
m_current_offset += n;
assert(m_current_offset >=0 &&
m_current_offset < (int)m_buffer.size());
} // skip
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Returns the send size, which is the full length of the buffer. A /** Returns the send size, which is the full length of the buffer. A
* difference to size() happens if the string to be sent was previously * difference to size() happens if the string to be sent was previously
@ -229,40 +240,24 @@ public:
} // add } // add
// Functions related to getting data from a network string // Functions related to getting data from a network string
// ------------------------------------------------------------------------
/** Ignore the next num_bytes from the message. */
void removeFront(unsigned int num_bytes)
{
assert(m_current_offset + num_bytes <= m_buffer.size());
m_current_offset += num_bytes;
} // removeFront
// ------------------------------------------------------------------------
/** Gets the byte at the specified position (taking current offset into
* account). */
uint8_t operator[](const int pos) const
{
return m_buffer[m_current_offset + pos];
} // operator[]
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Returns a unsigned 32 bit integer. */ /** Returns a unsigned 32 bit integer. */
inline uint32_t getUInt32(int pos = 0) const { return get<uint32_t, 4>(pos); } inline uint32_t getUInt32() const { return get<uint32_t, 4>(); }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Returns an unsigned 16 bit integer. */ /** Returns an unsigned 16 bit integer. */
inline uint16_t getUInt16(int pos=0) const { return get<uint16_t, 2>(pos); } inline uint16_t getUInt16() const { return get<uint16_t, 2>(); }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Returns an unsigned 8-bit integer. */ /** Returns an unsigned 8-bit integer. */
inline uint8_t getUInt8(int pos = 0) const inline uint8_t getUInt8() const
{ {
return m_buffer[m_current_offset + pos]; return m_buffer[m_current_offset++];
} // getUInt8 } // getUInt8
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Gets a 4 byte floating point value. */ /** Gets a 4 byte floating point value. */
float getFloat(int pos=0) const float getFloat() const
{ {
uint32_t u = getUInt32(pos); uint32_t u = getUInt32();
float f; float f;
// Doig a "return *(float*)&u;" appears to be more efficient, // Doig a "return *(float*)&u;" appears to be more efficient,
// but it can create incorrect code on higher optimisation: c++ // but it can create incorrect code on higher optimisation: c++
@ -282,17 +277,25 @@ public:
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Gets a Vec3. */ /** Gets a Vec3. */
Vec3 getVec3(int pos=0) const Vec3 getVec3() const
{ {
return Vec3(getFloat(pos), getFloat(pos+4), getFloat(pos+8)); Vec3 r;
r.setX(getFloat());
r.setY(getFloat());
r.setZ(getFloat());
return r;
} // getVec3 } // getVec3
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Gets a Vec3. */ /** Gets a bullet quaternion. */
btQuaternion getQuat(int pos=0) const btQuaternion getQuat() const
{ {
return btQuaternion(getFloat(pos), getFloat(pos+ 4), btQuaternion q;
getFloat(pos+8), getFloat(pos+12) ); q.setX(getFloat());
q.setY(getFloat());
q.setZ(getFloat());
q.setW(getFloat());
return q;
} // getQuat } // getQuat
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -333,7 +336,7 @@ public:
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Constructor for a received message. It automatically ignored the first /** Constructor for a received message. It automatically ignored the first
* 5 bytes which contain the type and token. Those will be access using * 5 bytes which contain the type and token. Those will be accessed using
* special functions. */ * special functions. */
NetworkString(const uint8_t *data, int len) NetworkString(const uint8_t *data, int len)
: BareNetworkString((char*)data, len) : BareNetworkString((char*)data, len)
@ -385,9 +388,13 @@ public:
* specified token. */ * specified token. */
uint32_t getToken() const uint32_t getToken() const
{ {
// Since m_current_offset might be set, we need to make sure // We need to reset the current position to 1 so that we can use the
// to really access bytes 1-4 // existing read 4 byte integer function to get the token.
return getUInt32(1-m_current_offset); int save_pos = m_current_offset;
m_current_offset = 1;
uint32_t token = getUInt32();
m_current_offset = save_pos;
return token;
} // getToken } // getToken
}; // class NetworkString }; // class NetworkString

View File

@ -192,12 +192,11 @@ bool ClientLobbyRoomProtocol::notifyEvent(Event* event)
{ {
NetworkString &data = event->data(); NetworkString &data = event->data();
assert(data.size()); // assert that data isn't empty assert(data.size()); // assert that data isn't empty
uint8_t message_type = data[0]; uint8_t message_type = data.getUInt8();
if (message_type != LE_KART_SELECTION_UPDATE && if (message_type != LE_KART_SELECTION_UPDATE &&
message_type != LE_RACE_FINISHED ) message_type != LE_RACE_FINISHED )
return false; // don't treat the event return false; // don't treat the event
data.removeFront(1);
Log::info("ClientLobbyRoomProtocol", "Synchronous message of type %d", Log::info("ClientLobbyRoomProtocol", "Synchronous message of type %d",
message_type); message_type);
if (message_type == LE_KART_SELECTION_UPDATE) // kart selection update if (message_type == LE_KART_SELECTION_UPDATE) // kart selection update
@ -219,9 +218,8 @@ bool ClientLobbyRoomProtocol::notifyEventAsynchronous(Event* event)
{ {
NetworkString &data = event->data(); NetworkString &data = event->data();
assert(data.size()); // assert that data isn't empty assert(data.size()); // assert that data isn't empty
uint8_t message_type = data[0]; uint8_t message_type = data.getUInt8();
data.removeFront(1);
Log::info("ClientLobbyRoomProtocol", "Asynchronous message of type %d", Log::info("ClientLobbyRoomProtocol", "Asynchronous message of type %d",
message_type); message_type);
switch(message_type) switch(message_type)
@ -341,10 +339,10 @@ void ClientLobbyRoomProtocol::newPlayer(Event* event)
if (!checkDataSize(event, 2)) return; if (!checkDataSize(event, 2)) return;
const NetworkString &data = event->data(); const NetworkString &data = event->data();
uint8_t player_id = data[0]; uint8_t player_id = data.getUInt8();
uint8_t host_id = data[1]; uint8_t host_id = data.getUInt8();
core::stringw name; core::stringw name;
data.decodeStringW(2, &name); data.decodeStringW(&name);
// FIXME need adjusting when splitscreen is used/ // FIXME need adjusting when splitscreen is used/
if(STKHost::get()->getGameSetup()->isLocalMaster(player_id)) if(STKHost::get()->getGameSetup()->isLocalMaster(player_id))
{ {
@ -386,7 +384,8 @@ void ClientLobbyRoomProtocol::disconnectedPlayer(Event* event)
NetworkString &data = event->data(); NetworkString &data = event->data();
while(data.size()>0) while(data.size()>0)
{ {
const NetworkPlayerProfile *profile = m_setup->getProfile(data[0]); const NetworkPlayerProfile *profile =
m_setup->getProfile(data.getUInt8());
if (m_setup->removePlayer(profile)) if (m_setup->removePlayer(profile))
{ {
Log::info("ClientLobbyRoomProtocol", Log::info("ClientLobbyRoomProtocol",
@ -398,7 +397,6 @@ void ClientLobbyRoomProtocol::disconnectedPlayer(Event* event)
Log::error("ClientLobbyRoomProtocol", Log::error("ClientLobbyRoomProtocol",
"The disconnected peer wasn't known."); "The disconnected peer wasn't known.");
} }
data.removeFront(1);
} // while } // while
STKHost::get()->removePeer(event->getPeer()); STKHost::get()->removePeer(event->getPeer());
@ -435,10 +433,9 @@ void ClientLobbyRoomProtocol::connectionAccepted(Event* event)
name = PlayerManager::getCurrentOnlineUserName(); name = PlayerManager::getCurrentOnlineUserName();
else else
name = PlayerManager::getCurrentPlayer()->getName(); name = PlayerManager::getCurrentPlayer()->getName();
uint8_t my_player_id = data[0]; uint8_t my_player_id = data.getUInt8();
uint8_t my_host_id = data[1]; uint8_t my_host_id = data.getUInt8();
uint8_t authorised = data[2]; uint8_t authorised = data.getUInt8();
data.removeFront(3);
// Store this client's authorisation status in the peer information // Store this client's authorisation status in the peer information
// for the server. // for the server.
event->getPeer()->setAuthorised(authorised!=0); event->getPeer()->setAuthorised(authorised!=0);
@ -456,10 +453,10 @@ void ClientLobbyRoomProtocol::connectionAccepted(Event* event)
// =============== // ===============
while (data.size() > 0) while (data.size() > 0)
{ {
uint8_t player_id = data[0]; uint8_t player_id = data.getUInt8();
uint8_t host_id = data[1]; uint8_t host_id = data.getUInt8();
irr::core::stringw name; irr::core::stringw name;
int bytes_read = data.decodeStringW(2, &name); int bytes_read = data.decodeStringW(&name);
NetworkPlayerProfile* profile2 = NetworkPlayerProfile* profile2 =
new NetworkPlayerProfile(name, player_id, host_id); new NetworkPlayerProfile(name, player_id, host_id);
@ -467,7 +464,6 @@ void ClientLobbyRoomProtocol::connectionAccepted(Event* event)
// Inform the network lobby of all players so that the GUI can // Inform the network lobby of all players so that the GUI can
// show all currently connected players. // show all currently connected players.
NetworkingLobby::getInstance()->addPlayer(profile2); NetworkingLobby::getInstance()->addPlayer(profile2);
data.removeFront(bytes_read+2);
} }
// Add self after other players so that player order is identical // Add self after other players so that player order is identical
@ -495,7 +491,7 @@ void ClientLobbyRoomProtocol::connectionRefused(Event* event)
if (!checkDataSize(event, 1)) return; if (!checkDataSize(event, 1)) return;
const NetworkString &data = event->data(); const NetworkString &data = event->data();
switch (data[0]) // the second byte switch (data.getUInt8()) // the second byte
{ {
case 0: case 0:
Log::info("ClientLobbyRoomProtocol", Log::info("ClientLobbyRoomProtocol",
@ -531,7 +527,7 @@ void ClientLobbyRoomProtocol::kartSelectionRefused(Event* event)
const NetworkString &data = event->data(); const NetworkString &data = event->data();
switch (data[0]) // the error code switch (data.getUInt8()) // the error code
{ {
case 0: case 0:
Log::info("ClientLobbyRoomProtocol", Log::info("ClientLobbyRoomProtocol",
@ -563,9 +559,9 @@ void ClientLobbyRoomProtocol::kartSelectionUpdate(Event* event)
{ {
if(!checkDataSize(event, 3)) return; if(!checkDataSize(event, 3)) return;
const NetworkString &data = event->data(); const NetworkString &data = event->data();
uint8_t player_id = data[0]; uint8_t player_id = data.getUInt8();
std::string kart_name; std::string kart_name;
data.decodeString(1, &kart_name); data.decodeString(&kart_name);
if (!m_setup->isKartAvailable(kart_name)) if (!m_setup->isKartAvailable(kart_name))
{ {
Log::error("ClientLobbyRoomProtocol", Log::error("ClientLobbyRoomProtocol",
@ -655,11 +651,10 @@ void ClientLobbyRoomProtocol::raceFinished(Event* event)
int position = 1; int position = 1;
while(data.size()>0) while(data.size()>0)
{ {
uint8_t kart_id = data[0]; uint8_t kart_id = data.getUInt8();
ranked_world->setKartPosition(kart_id,position); ranked_world->setKartPosition(kart_id,position);
Log::info("ClientLobbyRoomProtocol", "Kart %d has finished #%d", Log::info("ClientLobbyRoomProtocol", "Kart %d has finished #%d",
kart_id, position); kart_id, position);
data.removeFront(1);
position++; position++;
} }
ranked_world->endSetKartPositions(); ranked_world->endSetKartPositions();
@ -684,7 +679,9 @@ void ClientLobbyRoomProtocol::playerMajorVote(Event* event)
const NetworkString &data = event->data(); const NetworkString &data = event->data();
if (!checkDataSize(event, 2)) if (!checkDataSize(event, 2))
return; return;
m_setup->getRaceConfig()->setPlayerMajorVote(data[0], data[1]); uint8_t player_id = data.getUInt8();
uint8_t mode = data.getUInt8();
m_setup->getRaceConfig()->setPlayerMajorVote(player_id, mode);
} // playerMajorVote } // playerMajorVote
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -702,7 +699,9 @@ void ClientLobbyRoomProtocol::playerRaceCountVote(Event* event)
{ {
if (!checkDataSize(event, 2)) return; if (!checkDataSize(event, 2)) return;
const NetworkString &data = event->data(); const NetworkString &data = event->data();
m_setup->getRaceConfig()->setPlayerRaceCountVote(data[0], data[1]); uint8_t player_id = data.getUInt8();
uint8_t count = data.getUInt8();
m_setup->getRaceConfig()->setPlayerRaceCountVote(player_id, count);
} // playerRaceCountVote } // playerRaceCountVote
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -720,7 +719,9 @@ void ClientLobbyRoomProtocol::playerMinorVote(Event* event)
{ {
if (!checkDataSize(event, 2)) return; if (!checkDataSize(event, 2)) return;
const NetworkString &data = event->data(); const NetworkString &data = event->data();
m_setup->getRaceConfig()->setPlayerMinorVote(data[0], data[1]); uint8_t player_id = data.getUInt8();
uint8_t minor = data.getUInt8();
m_setup->getRaceConfig()->setPlayerMinorVote(player_id, minor);
} // playerMinorVote } // playerMinorVote
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -740,9 +741,11 @@ void ClientLobbyRoomProtocol::playerTrackVote(Event* event)
if (!checkDataSize(event, 3)) return; if (!checkDataSize(event, 3)) return;
const NetworkString &data = event->data(); const NetworkString &data = event->data();
std::string track_name; std::string track_name;
int N = data.decodeString(2, &track_name); uint8_t player_id = data.getUInt8();
m_setup->getRaceConfig()->setPlayerTrackVote(data[0], track_name, uint8_t number = data.getUInt8();
data[1]); int N = data.decodeString(&track_name);
m_setup->getRaceConfig()->setPlayerTrackVote(player_id, track_name,
number);
} // playerTrackVote } // playerTrackVote
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -761,8 +764,11 @@ void ClientLobbyRoomProtocol::playerReversedVote(Event* event)
{ {
if (!checkDataSize(event, 3)) return; if (!checkDataSize(event, 3)) return;
const NetworkString &data = event->data(); const NetworkString &data = event->data();
m_setup->getRaceConfig()->setPlayerReversedVote(data[0], data[1]!=0, uint8_t player_id = data.getUInt8();
data[2]); uint8_t reversed = data.getUInt8();
uint8_t number = data.getUInt8();
m_setup->getRaceConfig()->setPlayerReversedVote(player_id, reversed!=0,
number);
} // playerReversedVote } // playerReversedVote
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -781,7 +787,10 @@ void ClientLobbyRoomProtocol::playerLapsVote(Event* event)
{ {
if (!checkDataSize(event, 3)) return; if (!checkDataSize(event, 3)) return;
const NetworkString &data = event->data(); const NetworkString &data = event->data();
m_setup->getRaceConfig()->setPlayerLapsVote(data[0], data[1], data[2]); uint8_t player_id = data.getUInt8();
uint8_t laps = data.getUInt8();
uint8_t number = data.getUInt8();
m_setup->getRaceConfig()->setPlayerLapsVote(player_id, laps, number);
} // playerLapsVote } // playerLapsVote
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@ -387,7 +387,7 @@ void ConnectToServer::handleSameLAN()
BareNetworkString message(buffer, len); BareNetworkString message(buffer, len);
std::string received; std::string received;
message.decodeString(0, &received); message.decodeString(&received);
host->startListening(); // start listening again host->startListening(); // start listening again
std::string aloha("aloha_stk"); std::string aloha("aloha_stk");
if (received==aloha) if (received==aloha)

View File

@ -50,9 +50,8 @@ bool ControllerEventsProtocol::notifyEventAsynchronous(Event* event)
if(!checkDataSize(event, 13)) return true; if(!checkDataSize(event, 13)) return true;
NetworkString &data = event->data(); NetworkString &data = event->data();
float time = data.getFloat(0); float time = data.getFloat();
data.removeFront(4); // remove time
uint8_t client_index = -1; uint8_t client_index = -1;
while (data.size() >= 9) while (data.size() >= 9)
{ {
@ -62,12 +61,11 @@ bool ControllerEventsProtocol::notifyEventAsynchronous(Event* event)
{ {
Log::warn("ControllerEventProtocol", "No valid kart id (%s).", Log::warn("ControllerEventProtocol", "No valid kart id (%s).",
kart_id); kart_id);
data.removeFront(9);
continue; continue;
} }
uint8_t serialized_1 = data.getUInt8(1); uint8_t serialized_1 = data.getUInt8();
PlayerAction action = (PlayerAction)(data.getUInt8(4)); PlayerAction action = (PlayerAction)(data.getUInt8());
int action_value = data.getUInt32(5); int action_value = data.getUInt32();
Log::info("ControllerEventsProtocol", "KartID %d action %d value %d", Log::info("ControllerEventsProtocol", "KartID %d action %d value %d",
kart_id, action, action_value); kart_id, action, action_value);
Controller *controller = World::getWorld()->getKart(kart_id) Controller *controller = World::getWorld()->getKart(kart_id)
@ -81,7 +79,6 @@ bool ControllerEventsProtocol::notifyEventAsynchronous(Event* event)
controls->m_skid = KartControl::SkidControl(serialized_1 & 0x03); controls->m_skid = KartControl::SkidControl(serialized_1 & 0x03);
controller->action(action, action_value); controller->action(action, action_value);
data.removeFront(9);
} }
if (data.size() > 0 ) if (data.size() > 0 )
{ {

View File

@ -49,8 +49,7 @@ bool GameEventsProtocol::notifyEvent(Event* event)
Log::warn("GameEventsProtocol", "Bad token."); Log::warn("GameEventsProtocol", "Bad token.");
return true; return true;
} }
int8_t type = data.getUInt8(4); int8_t type = data.getUInt8();
data.removeFront(5);
switch (type) switch (type)
{ {
case GE_ITEM_COLLECTED: case GE_ITEM_COLLECTED:
@ -115,8 +114,8 @@ void GameEventsProtocol::collectedItem(const NetworkString &data)
Log::warn("GameEventsProtocol", "Too short message."); Log::warn("GameEventsProtocol", "Too short message.");
} }
uint32_t item_id = data.getUInt32(); uint32_t item_id = data.getUInt32();
uint8_t powerup_type = data.getUInt8(4); uint8_t powerup_type = data.getUInt8();
uint8_t kart_id = data.getUInt8(5); uint8_t kart_id = data.getUInt8();
// now set the kart powerup // now set the kart powerup
AbstractKart* kart = World::getWorld()->getKart(kart_id); AbstractKart* kart = World::getWorld()->getKart(kart_id);
ItemManager::get()->collectedItem(ItemManager::get()->getItem(item_id), ItemManager::get()->collectedItem(ItemManager::get()->getItem(item_id),
@ -148,8 +147,8 @@ void GameEventsProtocol::kartFinishedRace(AbstractKart *kart, float time)
*/ */
void GameEventsProtocol::kartFinishedRace(const NetworkString &ns) void GameEventsProtocol::kartFinishedRace(const NetworkString &ns)
{ {
uint8_t kart_id = ns.getUInt8(0); uint8_t kart_id = ns.getUInt8();
float time = ns.getFloat(1); float time = ns.getFloat();
World::getWorld()->getKart(kart_id)->finishedRace(time, World::getWorld()->getKart(kart_id)->finishedRace(time,
/*from_server*/true); /*from_server*/true);
} // kartFinishedRace } // kartFinishedRace

View File

@ -144,23 +144,22 @@ std::string GetPublicAddress::parseStunResponse()
// check that the stun response is a response, contains the magic cookie // check that the stun response is a response, contains the magic cookie
// and the transaction ID // and the transaction ID
if (datas.getUInt16(0) != 0x0101) if (datas.getUInt16() != 0x0101)
return "STUN response doesn't contain the magic cookie"; return "STUN response doesn't contain the magic cookie";
int message_size = datas.getUInt16();
if (datas.getUInt32(4) != m_stun_magic_cookie) if (datas.getUInt32() != m_stun_magic_cookie)
{ {
return "STUN response doesn't contain the magic cookie"; return "STUN response doesn't contain the magic cookie";
} }
for (int i = 0; i < 12; i++) for (int i = 0; i < 12; i++)
{ {
if (datas.getUInt8(i+8) != m_stun_tansaction_id[i]) if (datas.getUInt8() != m_stun_tansaction_id[i])
return "STUN response doesn't contain the transaction ID"; return "STUN response doesn't contain the transaction ID";
} }
Log::debug("GetPublicAddress", Log::debug("GetPublicAddress",
"The STUN server responded with a valid answer"); "The STUN server responded with a valid answer");
int message_size = datas.getUInt16(2);
// The stun message is valid, so we parse it now: // The stun message is valid, so we parse it now:
if (message_size == 0) if (message_size == 0)
@ -173,14 +172,16 @@ std::string GetPublicAddress::parseStunResponse()
int pos = 20; int pos = 20;
while (true) while (true)
{ {
int type = datas.getUInt16(pos); int type = datas.getUInt16();
int size = datas.getUInt16(pos+2); int size = datas.getUInt16();
if (type == 0 || type == 1) if (type == 0 || type == 1)
{ {
assert(size == 8); assert(size == 8);
assert(datas.getUInt8(pos+5) == 0x01); // Family IPv4 only datas.getUInt8(); // skip 1 byte
TransportAddress address(datas.getUInt32(pos + 8), assert(datas.getUInt8() == 0x01); // Family IPv4 only
datas.getUInt16(pos + 6)); uint16_t port = datas.getUInt16();
uint32_t ip = datas.getUInt32();
TransportAddress address(ip, port);
// finished parsing, we know our public transport address // finished parsing, we know our public transport address
Log::debug("GetPublicAddress", Log::debug("GetPublicAddress",
"The public address has been found: %s", "The public address has been found: %s",
@ -188,7 +189,7 @@ std::string GetPublicAddress::parseStunResponse()
NetworkConfig::get()->setMyAddress(address); NetworkConfig::get()->setMyAddress(address);
break; break;
} // type = 0 or 1 } // type = 0 or 1
pos += 4 + size; datas.skip(4 + size);
message_size -= 4 + size; message_size -= 4 + size;
if (message_size == 0) if (message_size == 0)
return "STUN response is invalid."; return "STUN response is invalid.";

View File

@ -44,15 +44,14 @@ bool KartUpdateProtocol::notifyEvent(Event* event)
Log::info("KartUpdateProtocol", "Message too short."); Log::info("KartUpdateProtocol", "Message too short.");
return true; return true;
} }
ns.removeFront(4); float time = ns.getFloat();
while(ns.size() >= 29) while(ns.size() >= 29)
{ {
uint8_t kart_id = ns.getUInt8(0); uint8_t kart_id = ns.getUInt8();
Vec3 xyz = ns.getVec3(1); Vec3 xyz = ns.getVec3();
btQuaternion quat = ns.getQuat(13); btQuaternion quat = ns.getQuat();
m_next_positions [kart_id] = xyz; m_next_positions [kart_id] = xyz;
m_next_quaternions[kart_id] = quat; m_next_quaternions[kart_id] = quat;
ns.removeFront(29);
} // while ns.size()>29 } // while ns.size()>29
// Set the flag that a new update was received // Set the flag that a new update was received

View File

@ -82,8 +82,7 @@ bool ServerLobbyRoomProtocol::notifyEventAsynchronous(Event* event)
NetworkString &data = event->data(); NetworkString &data = event->data();
assert(data.size()); // message not empty assert(data.size()); // message not empty
uint8_t message_type; uint8_t message_type;
message_type = data[0]; message_type = data.getUInt8();
data.removeFront(1);
Log::info("ServerLobbyRoomProtocol", "Message received with type %d.", Log::info("ServerLobbyRoomProtocol", "Message received with type %d.",
message_type); message_type);
switch(message_type) switch(message_type)
@ -443,10 +442,10 @@ void ServerLobbyRoomProtocol::connectionRequested(Event* event)
// Connection accepted. // Connection accepted.
// ==================== // ====================
std::string name_u8; std::string name_u8;
int len = data.decodeString(0, &name_u8); int len = data.decodeString(&name_u8);
core::stringw name = StringUtils::utf8ToWide(name_u8); core::stringw name = StringUtils::utf8ToWide(name_u8);
std::string password; std::string password;
data.decodeString(len, &password); data.decodeString(&password);
bool is_authorised = (password==NetworkConfig::get()->getPassword()); bool is_authorised = (password==NetworkConfig::get()->getPassword());
// Get the unique global ID for this player. // Get the unique global ID for this player.
@ -535,9 +534,9 @@ void ServerLobbyRoomProtocol::kartSelectionRequested(Event* event)
const NetworkString &data = event->data(); const NetworkString &data = event->data();
STKPeer* peer = event->getPeer(); STKPeer* peer = event->getPeer();
uint8_t player_id = data[0]; uint8_t player_id = data.getUInt8();
std::string kart_name; std::string kart_name;
data.decodeString(1, &kart_name); data.decodeString(&kart_name);
// check if selection is possible // check if selection is possible
if (!m_selection_enabled) if (!m_selection_enabled)
{ {
@ -598,8 +597,8 @@ void ServerLobbyRoomProtocol::playerMajorVote(Event* event)
if (!checkDataSize(event, 5)) return; if (!checkDataSize(event, 5)) return;
NetworkString &data = event->data(); NetworkString &data = event->data();
uint8_t player_id = data.getUInt8(0); uint8_t player_id = data.getUInt8();
uint32_t major = data.getUInt32(1); uint32_t major = data.getUInt32();
m_setup->getRaceConfig()->setPlayerMajorVote(player_id, major); m_setup->getRaceConfig()->setPlayerMajorVote(player_id, major);
// Send the vote to everybody (including the sender) // Send the vote to everybody (including the sender)
NetworkString *other = getNetworkString(6); NetworkString *other = getNetworkString(6);
@ -623,8 +622,8 @@ void ServerLobbyRoomProtocol::playerRaceCountVote(Event* event)
{ {
if (!checkDataSize(event, 1)) return; if (!checkDataSize(event, 1)) return;
NetworkString &data = event->data(); NetworkString &data = event->data();
uint8_t player_id = data.getUInt8(0); uint8_t player_id = data.getUInt8();
uint8_t race_count = data.getUInt8(1); uint8_t race_count = data.getUInt8();
m_setup->getRaceConfig()->setPlayerRaceCountVote(player_id, race_count); m_setup->getRaceConfig()->setPlayerRaceCountVote(player_id, race_count);
// Send the vote to everybody (including the sender) // Send the vote to everybody (including the sender)
NetworkString *other = getNetworkString(3); NetworkString *other = getNetworkString(3);
@ -650,8 +649,8 @@ void ServerLobbyRoomProtocol::playerMinorVote(Event* event)
{ {
if (!checkDataSize(event, 1)) return; if (!checkDataSize(event, 1)) return;
NetworkString &data = event->data(); NetworkString &data = event->data();
uint8_t player_id = data.getUInt8(0); uint8_t player_id = data.getUInt8();
uint32_t minor = data.getUInt32(1); uint32_t minor = data.getUInt32();
m_setup->getRaceConfig()->setPlayerMinorVote(player_id, minor); m_setup->getRaceConfig()->setPlayerMinorVote(player_id, minor);
// Send the vote to everybody (including the sender) // Send the vote to everybody (including the sender)
@ -677,12 +676,12 @@ void ServerLobbyRoomProtocol::playerTrackVote(Event* event)
{ {
if (!checkDataSize(event, 3)) return; if (!checkDataSize(event, 3)) return;
NetworkString &data = event->data(); NetworkString &data = event->data();
uint8_t player_id = data.getUInt8(0); uint8_t player_id = data.getUInt8();
// As which track this track should be used, e.g. 1st track: Sandtrack // As which track this track should be used, e.g. 1st track: Sandtrack
// 2nd track Mathclass, ... // 2nd track Mathclass, ...
uint8_t track_number = data.getUInt8(1); uint8_t track_number = data.getUInt8();
std::string track_name; std::string track_name;
int N = data.decodeString(2, &track_name); int N = data.decodeString(&track_name);
m_setup->getRaceConfig()->setPlayerTrackVote(player_id, track_name, m_setup->getRaceConfig()->setPlayerTrackVote(player_id, track_name,
track_number); track_number);
// Send the vote to everybody (including the sender) // Send the vote to everybody (including the sender)
@ -711,9 +710,9 @@ void ServerLobbyRoomProtocol::playerReversedVote(Event* event)
if (!checkDataSize(event, 3)) return; if (!checkDataSize(event, 3)) return;
NetworkString &data = event->data(); NetworkString &data = event->data();
uint8_t player_id = data.getUInt8(0); uint8_t player_id = data.getUInt8();
uint8_t reverse = data.getUInt8(1); uint8_t reverse = data.getUInt8();
uint8_t nb_track = data.getUInt8(2); uint8_t nb_track = data.getUInt8();
m_setup->getRaceConfig()->setPlayerReversedVote(player_id, m_setup->getRaceConfig()->setPlayerReversedVote(player_id,
reverse!=0, nb_track); reverse!=0, nb_track);
// Send the vote to everybody (including the sender) // Send the vote to everybody (including the sender)
@ -739,9 +738,9 @@ void ServerLobbyRoomProtocol::playerLapsVote(Event* event)
{ {
if (!checkDataSize(event, 2)) return; if (!checkDataSize(event, 2)) return;
NetworkString &data = event->data(); NetworkString &data = event->data();
uint8_t player_id = data.getUInt8(0); uint8_t player_id = data.getUInt8();
uint8_t lap_count = data.getUInt8(1); uint8_t lap_count = data.getUInt8();
uint8_t track_nb = data.getUInt8(2); uint8_t track_nb = data.getUInt8();
m_setup->getRaceConfig()->setPlayerLapsVote(player_id, lap_count, m_setup->getRaceConfig()->setPlayerLapsVote(player_id, lap_count,
track_nb); track_nb);
NetworkString *other = getNetworkString(4); NetworkString *other = getNetworkString(4);

View File

@ -126,8 +126,8 @@ bool StartGameProtocol::notifyEventAsynchronous(Event* event)
if(!checkDataSize(event, 1)) return true; if(!checkDataSize(event, 1)) return true;
const NetworkString &data = event->data(); const NetworkString &data = event->data();
uint8_t player_id = data.getUInt8(0); uint8_t player_id = data.getUInt8();
uint8_t ready = data.getUInt8(1); uint8_t ready = data.getUInt8();
if (NetworkConfig::get()->isServer() && ready) // on server, player is ready if (NetworkConfig::get()->isServer() && ready) // on server, player is ready
{ {
Log::info("StartGameProtocol", "One of the players is ready."); Log::info("StartGameProtocol", "One of the players is ready.");

View File

@ -45,8 +45,8 @@ bool SynchronizationProtocol::notifyEventAsynchronous(Event* event)
if(!checkDataSize(event, 5)) return true; if(!checkDataSize(event, 5)) return true;
const NetworkString &data = event->data(); const NetworkString &data = event->data();
uint32_t request = data.getUInt8(0); uint32_t request = data.getUInt8();
uint32_t sequence = data.getUInt32(1); uint32_t sequence = data.getUInt32();
const std::vector<STKPeer*> &peers = STKHost::get()->getPeers(); const std::vector<STKPeer*> &peers = STKHost::get()->getPeers();
assert(peers.size() > 0); assert(peers.size() > 0);
@ -77,9 +77,9 @@ bool SynchronizationProtocol::notifyEventAsynchronous(Event* event)
sequence, StkTime::getRealTime()); sequence, StkTime::getRealTime());
// countdown time in the message // countdown time in the message
if (data.size() == 9) if (data.size() == 4)
{ {
uint32_t time_to_start = data.getUInt32(5); uint32_t time_to_start = data.getUInt32();
Log::debug("SynchronizationProtocol", Log::debug("SynchronizationProtocol",
"Request to start game in %d.", time_to_start); "Request to start game in %d.", time_to_start);
if (!m_countdown_activated) if (!m_countdown_activated)

View File

@ -173,13 +173,13 @@ Online::XMLRequest* ServersManager::getLANRefreshRequest() const
BareNetworkString s(buffer, len); BareNetworkString s(buffer, len);
irr::core::stringw name; irr::core::stringw name;
// bytes_read is the number of bytes read // bytes_read is the number of bytes read
uint8_t bytes_read = s.decodeStringW(0, &name); uint8_t bytes_read = s.decodeStringW(&name);
uint8_t max_players = s.getUInt8(bytes_read ); uint8_t max_players = s.getUInt8();
uint8_t players = s.getUInt8(bytes_read+1); uint8_t players = s.getUInt8();
uint32_t my_ip = s.getUInt32(bytes_read+2); uint32_t my_ip = s.getUInt32();
uint32_t my_port = s.getUInt16(bytes_read+6); uint16_t my_port = s.getUInt16();
uint16_t mode = s.getUInt16(bytes_read+8); uint16_t mode = s.getUInt16();
uint8_t difficulty = s.getUInt8(bytes_read+10); uint8_t difficulty = s.getUInt8();
Server* server = new Server(name, /*lan*/true, Server* server = new Server(name, /*lan*/true,
max_players, players, sender); max_players, players, sender);
server->setDifficulty((RaceManager::Difficulty)difficulty); server->setDifficulty((RaceManager::Difficulty)difficulty);

View File

@ -572,7 +572,7 @@ void STKHost::handleLANRequests()
if(len<=0) return; if(len<=0) return;
BareNetworkString message(buffer, len); BareNetworkString message(buffer, len);
std::string command; std::string command;
message.decodeString(0, &command); message.decodeString(&command);
if (command == "stk-server") if (command == "stk-server")
{ {
Log::verbose("STKHost", "Received LAN server query"); Log::verbose("STKHost", "Received LAN server query");