From f2c26f06f1b30f592824e128e11fb71022df3353 Mon Sep 17 00:00:00 2001 From: hiker Date: Sun, 13 Mar 2016 13:49:43 +1100 Subject: [PATCH] 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). --- src/network/network_string.cpp | 17 ++-- src/network/network_string.hpp | 91 ++++++++++--------- .../protocols/client_lobby_room_protocol.cpp | 73 ++++++++------- src/network/protocols/connect_to_server.cpp | 2 +- .../protocols/controller_events_protocol.cpp | 11 +-- .../protocols/game_events_protocol.cpp | 11 +-- src/network/protocols/get_public_address.cpp | 23 ++--- .../protocols/kart_update_protocol.cpp | 9 +- .../protocols/server_lobby_room_protocol.cpp | 41 ++++----- src/network/protocols/start_game_protocol.cpp | 4 +- .../protocols/synchronization_protocol.cpp | 8 +- src/network/servers_manager.cpp | 14 +-- src/network/stk_host.cpp | 2 +- 13 files changed, 157 insertions(+), 149 deletions(-) diff --git a/src/network/network_string.cpp b/src/network/network_string.cpp index 63558e639..d648dea90 100644 --- a/src/network/network_string.cpp +++ b/src/network/network_string.cpp @@ -46,11 +46,9 @@ void NetworkString::unitTesting() // Append some values from the message s.addUInt16(12345); s.addFloat(1.2345f); - // Ignore message type and token - s.removeFront(5); - assert(s.getUInt16(0) == 12345); - float f = s.getFloat(2); + assert(s.getUInt16() == 12345); + float f = s.getFloat(); assert(f==1.2345f); // 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. * \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(m_current_offset+pos); - *out = getString(pos+1, len); + uint8_t len = get(); + *out = getString(len); return len+1; } // decodeString // ---------------------------------------------------------------------------- /** Returns an irrlicht wide string from the utf8 encoded string at the * given position. - * \param[in] pos Buffer position where the encoded string starts. * \param[out] out The decoded string. * \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 * characters can mean that the length of the returned string is * 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; - int len = decodeString(pos, &s); + int len = decodeString(&s); *out = StringUtils::utf8ToWide(s); return len; } // decodeString diff --git a/src/network/network_string.hpp b/src/network/network_string.hpp index 9877720d8..5547cdc0e 100644 --- a/src/network/network_string.hpp +++ b/src/network/network_string.hpp @@ -64,7 +64,7 @@ protected: * should be left as signed, otherwise certain arithmetic (e.g. * 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 @@ -72,10 +72,12 @@ protected: * \param pos First position to be in the string. * \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 ), - m_buffer.begin() + (m_current_offset+pos + len) ); + std::string a(m_buffer.begin() + (m_current_offset ), + m_buffer.begin() + (m_current_offset + len)); + m_current_offset += len; + return a; } // getString // ------------------------------------------------------------------------ /** 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 - T get(int pos) const + T get() const { int a = n; T result = 0; - int offset = m_current_offset + pos + n -1; + m_current_offset += n; + int offset = m_current_offset -1; while (a--) { result <<= 8; // offset one byte @@ -105,9 +108,9 @@ protected: // ------------------------------------------------------------------------ /** Another function for n == 1 to surpress warnings in clang. */ template - T get(int pos) const + T get() const { - return m_buffer[pos]; + return m_buffer[m_current_offset++]; } // get public: @@ -137,8 +140,8 @@ public: // ------------------------------------------------------------------------ BareNetworkString& encodeString(const std::string &value); BareNetworkString& encodeString(const irr::core::stringw &value); - int decodeString(int n, std::string *out) const; - int decodeStringW(int n, irr::core::stringw *out) const; + int decodeString(std::string *out) const; + int decodeStringW(irr::core::stringw *out) const; std::string getLogMessage(const std::string &indent="") const; // ------------------------------------------------------------------------ /** Returns a byte pointer to the content of the network string. */ @@ -152,6 +155,14 @@ public: /** Returns the remaining length of the network string. */ 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 * difference to size() happens if the string to be sent was previously @@ -229,40 +240,24 @@ public: } // add // 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. */ - inline uint32_t getUInt32(int pos = 0) const { return get(pos); } + inline uint32_t getUInt32() const { return get(); } // ------------------------------------------------------------------------ /** Returns an unsigned 16 bit integer. */ - inline uint16_t getUInt16(int pos=0) const { return get(pos); } + inline uint16_t getUInt16() const { return get(); } // ------------------------------------------------------------------------ /** 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 // ------------------------------------------------------------------------ /** 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; // Doig a "return *(float*)&u;" appears to be more efficient, // but it can create incorrect code on higher optimisation: c++ @@ -282,17 +277,25 @@ public: // ------------------------------------------------------------------------ /** 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 // ------------------------------------------------------------------------ - /** Gets a Vec3. */ - btQuaternion getQuat(int pos=0) const + /** Gets a bullet quaternion. */ + btQuaternion getQuat() const { - return btQuaternion(getFloat(pos), getFloat(pos+ 4), - getFloat(pos+8), getFloat(pos+12) ); + btQuaternion q; + q.setX(getFloat()); + q.setY(getFloat()); + q.setZ(getFloat()); + q.setW(getFloat()); + return q; } // getQuat // ------------------------------------------------------------------------ @@ -333,7 +336,7 @@ public: // ------------------------------------------------------------------------ /** 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. */ NetworkString(const uint8_t *data, int len) : BareNetworkString((char*)data, len) @@ -385,9 +388,13 @@ public: * specified token. */ uint32_t getToken() const { - // Since m_current_offset might be set, we need to make sure - // to really access bytes 1-4 - return getUInt32(1-m_current_offset); + // We need to reset the current position to 1 so that we can use the + // existing read 4 byte integer function to get the token. + int save_pos = m_current_offset; + m_current_offset = 1; + uint32_t token = getUInt32(); + m_current_offset = save_pos; + return token; } // getToken }; // class NetworkString diff --git a/src/network/protocols/client_lobby_room_protocol.cpp b/src/network/protocols/client_lobby_room_protocol.cpp index c4644524b..f332eeb1e 100644 --- a/src/network/protocols/client_lobby_room_protocol.cpp +++ b/src/network/protocols/client_lobby_room_protocol.cpp @@ -192,12 +192,11 @@ bool ClientLobbyRoomProtocol::notifyEvent(Event* event) { NetworkString &data = event->data(); 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 && message_type != LE_RACE_FINISHED ) return false; // don't treat the event - data.removeFront(1); Log::info("ClientLobbyRoomProtocol", "Synchronous message of type %d", message_type); if (message_type == LE_KART_SELECTION_UPDATE) // kart selection update @@ -219,9 +218,8 @@ bool ClientLobbyRoomProtocol::notifyEventAsynchronous(Event* event) { NetworkString &data = event->data(); 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", message_type); switch(message_type) @@ -341,10 +339,10 @@ void ClientLobbyRoomProtocol::newPlayer(Event* event) if (!checkDataSize(event, 2)) return; const NetworkString &data = event->data(); - uint8_t player_id = data[0]; - uint8_t host_id = data[1]; + uint8_t player_id = data.getUInt8(); + uint8_t host_id = data.getUInt8(); core::stringw name; - data.decodeStringW(2, &name); + data.decodeStringW(&name); // FIXME need adjusting when splitscreen is used/ if(STKHost::get()->getGameSetup()->isLocalMaster(player_id)) { @@ -386,7 +384,8 @@ void ClientLobbyRoomProtocol::disconnectedPlayer(Event* event) NetworkString &data = event->data(); 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)) { Log::info("ClientLobbyRoomProtocol", @@ -398,7 +397,6 @@ void ClientLobbyRoomProtocol::disconnectedPlayer(Event* event) Log::error("ClientLobbyRoomProtocol", "The disconnected peer wasn't known."); } - data.removeFront(1); } // while STKHost::get()->removePeer(event->getPeer()); @@ -435,10 +433,9 @@ void ClientLobbyRoomProtocol::connectionAccepted(Event* event) name = PlayerManager::getCurrentOnlineUserName(); else name = PlayerManager::getCurrentPlayer()->getName(); - uint8_t my_player_id = data[0]; - uint8_t my_host_id = data[1]; - uint8_t authorised = data[2]; - data.removeFront(3); + uint8_t my_player_id = data.getUInt8(); + uint8_t my_host_id = data.getUInt8(); + uint8_t authorised = data.getUInt8(); // Store this client's authorisation status in the peer information // for the server. event->getPeer()->setAuthorised(authorised!=0); @@ -456,10 +453,10 @@ void ClientLobbyRoomProtocol::connectionAccepted(Event* event) // =============== while (data.size() > 0) { - uint8_t player_id = data[0]; - uint8_t host_id = data[1]; + uint8_t player_id = data.getUInt8(); + uint8_t host_id = data.getUInt8(); irr::core::stringw name; - int bytes_read = data.decodeStringW(2, &name); + int bytes_read = data.decodeStringW(&name); NetworkPlayerProfile* profile2 = 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 // show all currently connected players. NetworkingLobby::getInstance()->addPlayer(profile2); - data.removeFront(bytes_read+2); } // 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; const NetworkString &data = event->data(); - switch (data[0]) // the second byte + switch (data.getUInt8()) // the second byte { case 0: Log::info("ClientLobbyRoomProtocol", @@ -531,7 +527,7 @@ void ClientLobbyRoomProtocol::kartSelectionRefused(Event* event) const NetworkString &data = event->data(); - switch (data[0]) // the error code + switch (data.getUInt8()) // the error code { case 0: Log::info("ClientLobbyRoomProtocol", @@ -563,9 +559,9 @@ void ClientLobbyRoomProtocol::kartSelectionUpdate(Event* event) { if(!checkDataSize(event, 3)) return; const NetworkString &data = event->data(); - uint8_t player_id = data[0]; + uint8_t player_id = data.getUInt8(); std::string kart_name; - data.decodeString(1, &kart_name); + data.decodeString(&kart_name); if (!m_setup->isKartAvailable(kart_name)) { Log::error("ClientLobbyRoomProtocol", @@ -655,11 +651,10 @@ void ClientLobbyRoomProtocol::raceFinished(Event* event) int position = 1; while(data.size()>0) { - uint8_t kart_id = data[0]; + uint8_t kart_id = data.getUInt8(); ranked_world->setKartPosition(kart_id,position); Log::info("ClientLobbyRoomProtocol", "Kart %d has finished #%d", kart_id, position); - data.removeFront(1); position++; } ranked_world->endSetKartPositions(); @@ -684,7 +679,9 @@ void ClientLobbyRoomProtocol::playerMajorVote(Event* event) const NetworkString &data = event->data(); if (!checkDataSize(event, 2)) 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 //----------------------------------------------------------------------------- @@ -702,7 +699,9 @@ void ClientLobbyRoomProtocol::playerRaceCountVote(Event* event) { if (!checkDataSize(event, 2)) return; 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 //----------------------------------------------------------------------------- @@ -720,7 +719,9 @@ void ClientLobbyRoomProtocol::playerMinorVote(Event* event) { if (!checkDataSize(event, 2)) return; 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 //----------------------------------------------------------------------------- @@ -740,9 +741,11 @@ void ClientLobbyRoomProtocol::playerTrackVote(Event* event) if (!checkDataSize(event, 3)) return; const NetworkString &data = event->data(); std::string track_name; - int N = data.decodeString(2, &track_name); - m_setup->getRaceConfig()->setPlayerTrackVote(data[0], track_name, - data[1]); + uint8_t player_id = data.getUInt8(); + uint8_t number = data.getUInt8(); + int N = data.decodeString(&track_name); + m_setup->getRaceConfig()->setPlayerTrackVote(player_id, track_name, + number); } // playerTrackVote //----------------------------------------------------------------------------- @@ -761,8 +764,11 @@ void ClientLobbyRoomProtocol::playerReversedVote(Event* event) { if (!checkDataSize(event, 3)) return; const NetworkString &data = event->data(); - m_setup->getRaceConfig()->setPlayerReversedVote(data[0], data[1]!=0, - data[2]); + uint8_t player_id = data.getUInt8(); + uint8_t reversed = data.getUInt8(); + uint8_t number = data.getUInt8(); + m_setup->getRaceConfig()->setPlayerReversedVote(player_id, reversed!=0, + number); } // playerReversedVote //----------------------------------------------------------------------------- @@ -781,7 +787,10 @@ void ClientLobbyRoomProtocol::playerLapsVote(Event* event) { if (!checkDataSize(event, 3)) return; 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 //----------------------------------------------------------------------------- diff --git a/src/network/protocols/connect_to_server.cpp b/src/network/protocols/connect_to_server.cpp index c503de988..524c37fff 100644 --- a/src/network/protocols/connect_to_server.cpp +++ b/src/network/protocols/connect_to_server.cpp @@ -387,7 +387,7 @@ void ConnectToServer::handleSameLAN() BareNetworkString message(buffer, len); std::string received; - message.decodeString(0, &received); + message.decodeString(&received); host->startListening(); // start listening again std::string aloha("aloha_stk"); if (received==aloha) diff --git a/src/network/protocols/controller_events_protocol.cpp b/src/network/protocols/controller_events_protocol.cpp index 0bf2e4a91..a7613f3ed 100644 --- a/src/network/protocols/controller_events_protocol.cpp +++ b/src/network/protocols/controller_events_protocol.cpp @@ -50,9 +50,8 @@ bool ControllerEventsProtocol::notifyEventAsynchronous(Event* event) if(!checkDataSize(event, 13)) return true; NetworkString &data = event->data(); - float time = data.getFloat(0); + float time = data.getFloat(); - data.removeFront(4); // remove time uint8_t client_index = -1; while (data.size() >= 9) { @@ -62,12 +61,11 @@ bool ControllerEventsProtocol::notifyEventAsynchronous(Event* event) { Log::warn("ControllerEventProtocol", "No valid kart id (%s).", kart_id); - data.removeFront(9); continue; } - uint8_t serialized_1 = data.getUInt8(1); - PlayerAction action = (PlayerAction)(data.getUInt8(4)); - int action_value = data.getUInt32(5); + uint8_t serialized_1 = data.getUInt8(); + PlayerAction action = (PlayerAction)(data.getUInt8()); + int action_value = data.getUInt32(); Log::info("ControllerEventsProtocol", "KartID %d action %d value %d", kart_id, action, action_value); Controller *controller = World::getWorld()->getKart(kart_id) @@ -81,7 +79,6 @@ bool ControllerEventsProtocol::notifyEventAsynchronous(Event* event) controls->m_skid = KartControl::SkidControl(serialized_1 & 0x03); controller->action(action, action_value); - data.removeFront(9); } if (data.size() > 0 ) { diff --git a/src/network/protocols/game_events_protocol.cpp b/src/network/protocols/game_events_protocol.cpp index 1d23ec00d..f100bf0e7 100644 --- a/src/network/protocols/game_events_protocol.cpp +++ b/src/network/protocols/game_events_protocol.cpp @@ -49,8 +49,7 @@ bool GameEventsProtocol::notifyEvent(Event* event) Log::warn("GameEventsProtocol", "Bad token."); return true; } - int8_t type = data.getUInt8(4); - data.removeFront(5); + int8_t type = data.getUInt8(); switch (type) { case GE_ITEM_COLLECTED: @@ -115,8 +114,8 @@ void GameEventsProtocol::collectedItem(const NetworkString &data) Log::warn("GameEventsProtocol", "Too short message."); } uint32_t item_id = data.getUInt32(); - uint8_t powerup_type = data.getUInt8(4); - uint8_t kart_id = data.getUInt8(5); + uint8_t powerup_type = data.getUInt8(); + uint8_t kart_id = data.getUInt8(); // now set the kart powerup AbstractKart* kart = World::getWorld()->getKart(kart_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) { - uint8_t kart_id = ns.getUInt8(0); - float time = ns.getFloat(1); + uint8_t kart_id = ns.getUInt8(); + float time = ns.getFloat(); World::getWorld()->getKart(kart_id)->finishedRace(time, /*from_server*/true); } // kartFinishedRace diff --git a/src/network/protocols/get_public_address.cpp b/src/network/protocols/get_public_address.cpp index f4af3e9b4..213cf60df 100644 --- a/src/network/protocols/get_public_address.cpp +++ b/src/network/protocols/get_public_address.cpp @@ -144,23 +144,22 @@ std::string GetPublicAddress::parseStunResponse() // check that the stun response is a response, contains the magic cookie // and the transaction ID - if (datas.getUInt16(0) != 0x0101) + if (datas.getUInt16() != 0x0101) return "STUN response doesn't contain the magic cookie"; - - if (datas.getUInt32(4) != m_stun_magic_cookie) + int message_size = datas.getUInt16(); + if (datas.getUInt32() != m_stun_magic_cookie) { return "STUN response doesn't contain the magic cookie"; } 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"; } Log::debug("GetPublicAddress", "The STUN server responded with a valid answer"); - int message_size = datas.getUInt16(2); // The stun message is valid, so we parse it now: if (message_size == 0) @@ -173,14 +172,16 @@ std::string GetPublicAddress::parseStunResponse() int pos = 20; while (true) { - int type = datas.getUInt16(pos); - int size = datas.getUInt16(pos+2); + int type = datas.getUInt16(); + int size = datas.getUInt16(); if (type == 0 || type == 1) { assert(size == 8); - assert(datas.getUInt8(pos+5) == 0x01); // Family IPv4 only - TransportAddress address(datas.getUInt32(pos + 8), - datas.getUInt16(pos + 6)); + datas.getUInt8(); // skip 1 byte + assert(datas.getUInt8() == 0x01); // Family IPv4 only + uint16_t port = datas.getUInt16(); + uint32_t ip = datas.getUInt32(); + TransportAddress address(ip, port); // finished parsing, we know our public transport address Log::debug("GetPublicAddress", "The public address has been found: %s", @@ -188,7 +189,7 @@ std::string GetPublicAddress::parseStunResponse() NetworkConfig::get()->setMyAddress(address); break; } // type = 0 or 1 - pos += 4 + size; + datas.skip(4 + size); message_size -= 4 + size; if (message_size == 0) return "STUN response is invalid."; diff --git a/src/network/protocols/kart_update_protocol.cpp b/src/network/protocols/kart_update_protocol.cpp index a514ba73a..a6f41ad91 100644 --- a/src/network/protocols/kart_update_protocol.cpp +++ b/src/network/protocols/kart_update_protocol.cpp @@ -44,15 +44,14 @@ bool KartUpdateProtocol::notifyEvent(Event* event) Log::info("KartUpdateProtocol", "Message too short."); return true; } - ns.removeFront(4); + float time = ns.getFloat(); while(ns.size() >= 29) { - uint8_t kart_id = ns.getUInt8(0); - Vec3 xyz = ns.getVec3(1); - btQuaternion quat = ns.getQuat(13); + uint8_t kart_id = ns.getUInt8(); + Vec3 xyz = ns.getVec3(); + btQuaternion quat = ns.getQuat(); m_next_positions [kart_id] = xyz; m_next_quaternions[kart_id] = quat; - ns.removeFront(29); } // while ns.size()>29 // Set the flag that a new update was received diff --git a/src/network/protocols/server_lobby_room_protocol.cpp b/src/network/protocols/server_lobby_room_protocol.cpp index 246728b7c..b4c6bab81 100644 --- a/src/network/protocols/server_lobby_room_protocol.cpp +++ b/src/network/protocols/server_lobby_room_protocol.cpp @@ -82,8 +82,7 @@ bool ServerLobbyRoomProtocol::notifyEventAsynchronous(Event* event) NetworkString &data = event->data(); assert(data.size()); // message not empty uint8_t message_type; - message_type = data[0]; - data.removeFront(1); + message_type = data.getUInt8(); Log::info("ServerLobbyRoomProtocol", "Message received with type %d.", message_type); switch(message_type) @@ -443,10 +442,10 @@ void ServerLobbyRoomProtocol::connectionRequested(Event* event) // Connection accepted. // ==================== std::string name_u8; - int len = data.decodeString(0, &name_u8); + int len = data.decodeString(&name_u8); core::stringw name = StringUtils::utf8ToWide(name_u8); std::string password; - data.decodeString(len, &password); + data.decodeString(&password); bool is_authorised = (password==NetworkConfig::get()->getPassword()); // Get the unique global ID for this player. @@ -535,9 +534,9 @@ void ServerLobbyRoomProtocol::kartSelectionRequested(Event* event) const NetworkString &data = event->data(); STKPeer* peer = event->getPeer(); - uint8_t player_id = data[0]; + uint8_t player_id = data.getUInt8(); std::string kart_name; - data.decodeString(1, &kart_name); + data.decodeString(&kart_name); // check if selection is possible if (!m_selection_enabled) { @@ -598,8 +597,8 @@ void ServerLobbyRoomProtocol::playerMajorVote(Event* event) if (!checkDataSize(event, 5)) return; NetworkString &data = event->data(); - uint8_t player_id = data.getUInt8(0); - uint32_t major = data.getUInt32(1); + uint8_t player_id = data.getUInt8(); + uint32_t major = data.getUInt32(); m_setup->getRaceConfig()->setPlayerMajorVote(player_id, major); // Send the vote to everybody (including the sender) NetworkString *other = getNetworkString(6); @@ -623,8 +622,8 @@ void ServerLobbyRoomProtocol::playerRaceCountVote(Event* event) { if (!checkDataSize(event, 1)) return; NetworkString &data = event->data(); - uint8_t player_id = data.getUInt8(0); - uint8_t race_count = data.getUInt8(1); + uint8_t player_id = data.getUInt8(); + uint8_t race_count = data.getUInt8(); m_setup->getRaceConfig()->setPlayerRaceCountVote(player_id, race_count); // Send the vote to everybody (including the sender) NetworkString *other = getNetworkString(3); @@ -650,8 +649,8 @@ void ServerLobbyRoomProtocol::playerMinorVote(Event* event) { if (!checkDataSize(event, 1)) return; NetworkString &data = event->data(); - uint8_t player_id = data.getUInt8(0); - uint32_t minor = data.getUInt32(1); + uint8_t player_id = data.getUInt8(); + uint32_t minor = data.getUInt32(); m_setup->getRaceConfig()->setPlayerMinorVote(player_id, minor); // Send the vote to everybody (including the sender) @@ -677,12 +676,12 @@ void ServerLobbyRoomProtocol::playerTrackVote(Event* event) { if (!checkDataSize(event, 3)) return; 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 // 2nd track Mathclass, ... - uint8_t track_number = data.getUInt8(1); + uint8_t track_number = data.getUInt8(); 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, track_number); // Send the vote to everybody (including the sender) @@ -711,9 +710,9 @@ void ServerLobbyRoomProtocol::playerReversedVote(Event* event) if (!checkDataSize(event, 3)) return; NetworkString &data = event->data(); - uint8_t player_id = data.getUInt8(0); - uint8_t reverse = data.getUInt8(1); - uint8_t nb_track = data.getUInt8(2); + uint8_t player_id = data.getUInt8(); + uint8_t reverse = data.getUInt8(); + uint8_t nb_track = data.getUInt8(); m_setup->getRaceConfig()->setPlayerReversedVote(player_id, reverse!=0, nb_track); // Send the vote to everybody (including the sender) @@ -739,9 +738,9 @@ void ServerLobbyRoomProtocol::playerLapsVote(Event* event) { if (!checkDataSize(event, 2)) return; NetworkString &data = event->data(); - uint8_t player_id = data.getUInt8(0); - uint8_t lap_count = data.getUInt8(1); - uint8_t track_nb = data.getUInt8(2); + uint8_t player_id = data.getUInt8(); + uint8_t lap_count = data.getUInt8(); + uint8_t track_nb = data.getUInt8(); m_setup->getRaceConfig()->setPlayerLapsVote(player_id, lap_count, track_nb); NetworkString *other = getNetworkString(4); diff --git a/src/network/protocols/start_game_protocol.cpp b/src/network/protocols/start_game_protocol.cpp index 676bfcabc..223c6f301 100644 --- a/src/network/protocols/start_game_protocol.cpp +++ b/src/network/protocols/start_game_protocol.cpp @@ -126,8 +126,8 @@ bool StartGameProtocol::notifyEventAsynchronous(Event* event) if(!checkDataSize(event, 1)) return true; const NetworkString &data = event->data(); - uint8_t player_id = data.getUInt8(0); - uint8_t ready = data.getUInt8(1); + uint8_t player_id = data.getUInt8(); + uint8_t ready = data.getUInt8(); if (NetworkConfig::get()->isServer() && ready) // on server, player is ready { Log::info("StartGameProtocol", "One of the players is ready."); diff --git a/src/network/protocols/synchronization_protocol.cpp b/src/network/protocols/synchronization_protocol.cpp index beb75a652..c553b79ad 100644 --- a/src/network/protocols/synchronization_protocol.cpp +++ b/src/network/protocols/synchronization_protocol.cpp @@ -45,8 +45,8 @@ bool SynchronizationProtocol::notifyEventAsynchronous(Event* event) if(!checkDataSize(event, 5)) return true; const NetworkString &data = event->data(); - uint32_t request = data.getUInt8(0); - uint32_t sequence = data.getUInt32(1); + uint32_t request = data.getUInt8(); + uint32_t sequence = data.getUInt32(); const std::vector &peers = STKHost::get()->getPeers(); assert(peers.size() > 0); @@ -77,9 +77,9 @@ bool SynchronizationProtocol::notifyEventAsynchronous(Event* event) sequence, StkTime::getRealTime()); // 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", "Request to start game in %d.", time_to_start); if (!m_countdown_activated) diff --git a/src/network/servers_manager.cpp b/src/network/servers_manager.cpp index ab0a9b4d0..3c45b594a 100644 --- a/src/network/servers_manager.cpp +++ b/src/network/servers_manager.cpp @@ -173,13 +173,13 @@ Online::XMLRequest* ServersManager::getLANRefreshRequest() const BareNetworkString s(buffer, len); irr::core::stringw name; // bytes_read is the number of bytes read - uint8_t bytes_read = s.decodeStringW(0, &name); - uint8_t max_players = s.getUInt8(bytes_read ); - uint8_t players = s.getUInt8(bytes_read+1); - uint32_t my_ip = s.getUInt32(bytes_read+2); - uint32_t my_port = s.getUInt16(bytes_read+6); - uint16_t mode = s.getUInt16(bytes_read+8); - uint8_t difficulty = s.getUInt8(bytes_read+10); + uint8_t bytes_read = s.decodeStringW(&name); + uint8_t max_players = s.getUInt8(); + uint8_t players = s.getUInt8(); + uint32_t my_ip = s.getUInt32(); + uint16_t my_port = s.getUInt16(); + uint16_t mode = s.getUInt16(); + uint8_t difficulty = s.getUInt8(); Server* server = new Server(name, /*lan*/true, max_players, players, sender); server->setDifficulty((RaceManager::Difficulty)difficulty); diff --git a/src/network/stk_host.cpp b/src/network/stk_host.cpp index 182bbcb3c..cad666134 100644 --- a/src/network/stk_host.cpp +++ b/src/network/stk_host.cpp @@ -572,7 +572,7 @@ void STKHost::handleLANRequests() if(len<=0) return; BareNetworkString message(buffer, len); std::string command; - message.decodeString(0, &command); + message.decodeString(&command); if (command == "stk-server") { Log::verbose("STKHost", "Received LAN server query");