Used new decode/encodeString functions in other places, made getString private.

This commit is contained in:
hiker 2015-11-27 08:55:21 +11:00
parent b092b47a58
commit 0076a041c9
5 changed files with 38 additions and 57 deletions

View File

@ -58,6 +58,18 @@ private:
std::vector<uint8_t> m_string;
// ------------------------------------------------------------------------
/** Returns a part of the network string as a std::string. This is an
* internal function only, the user should call decodeString(W) instead.
* \param pos First position to be in the string.
* \param len Number of bytes to copy.
*/
std::string getString(int pos, int len) const
{
return std::string(m_string.begin() + pos,
m_string.begin() + pos + len);
} // getString
public:
/** Dummy constructor. */
NetworkString() { }
@ -91,11 +103,6 @@ public:
NetworkString& encodeString(const irr::core::stringw &value);
int decodeString(int n, std::string *out) const;
int decodeStringW(int n, irr::core::stringw *out) const;
// ------------------------------------------------------------------------
NetworkString& add(const std::string &s)
{
return addUInt8(uint8_t(s.size())).as(s);
} // add
// ------------------------------------------------------------------------
NetworkString& removeFront(int size)
@ -316,17 +323,6 @@ public:
{
return get<unsigned char>(pos);
} // getUChar
// ------------------------------------------------------------------------
/** Returns a part of the network string as a std::string.
* \param pos First position to be in the string.
* \param len Number of bytes to copy.
*/
std::string getString(int pos, int len) const
{
return std::string(m_string.begin() + pos,
m_string.begin() + pos + len);
} // getString
// ------------------------------------------------------------------------
/** Returns a standard integer. */
inline int gi(int pos = 0) const { return get<int, 4>(pos); }

View File

@ -61,7 +61,7 @@ void ClientLobbyRoomProtocol::requestKartSelection(const std::string &kart_name)
NetworkString request(6+1+kart_name.size());
// size_token (4), token, size kart name, kart name
request.ai8(LE_KART_SELECTION).ai8(4).ai32(m_server->getClientServerToken())
.add(kart_name);
.encodeString(kart_name);
sendMessage(request, true);
} // requestKartSelection
@ -106,7 +106,7 @@ void ClientLobbyRoomProtocol::voteTrack(const std::string &track,
NetworkString request(8+1+track.size());
// size_token (4), token, size track, track, size #track, #track
request.ai8(LE_VOTE_TRACK).ai8(4).ai32(m_server->getClientServerToken())
.add(track).ai8(1).ai8(track_nb);
.encodeString(track).ai8(1).ai8(track_nb);
sendMessage(request, true);
} // voteTrack
@ -310,9 +310,9 @@ void ClientLobbyRoomProtocol::newPlayer(Event* event)
uint32_t global_id = data.gui32(1);
uint8_t player_id = data.gui8(6);
int name_len = data.getUInt8(7);
std::string name_utf8 = data.getString(8, name_len);
core::stringw name = StringUtils::utf8ToWide(name_utf8);
core::stringw name;
data.decodeStringW(7, &name);
if (global_id == PlayerManager::getCurrentOnlineId())
{
Log::error("ClientLobbyRoomProtocol",
@ -554,15 +554,8 @@ void ClientLobbyRoomProtocol::kartSelectionUpdate(Event* event)
return;
}
uint8_t player_id = data[1];
uint8_t kart_name_length = data[2];
std::string kart_name = data.getString(3, kart_name_length);
if (kart_name.size() != kart_name_length)
{
Log::error("ClientLobbyRoomProtocol",
"Kart names sizes differ: told: %d, real: %d.",
kart_name_length, kart_name.size());
return;
}
std::string kart_name;
data.decodeString(2, &kart_name);
if (!m_setup->isKartAvailable(kart_name))
{
Log::error("ClientLobbyRoomProtocol",
@ -811,11 +804,12 @@ void ClientLobbyRoomProtocol::playerTrackVote(Event* event)
return;
if (!isByteCorrect(event, 5, 1))
return;
int N = data[7];
std::string track_name = data.getString(8, N);
std::string track_name;
int N = data.decodeString(7, &track_name);
if (!isByteCorrect(event, N+8, 1))
return;
m_setup->getRaceConfig()->setPlayerTrackVote(data[6], track_name, data[N+9]);
m_setup->getRaceConfig()->setPlayerTrackVote(data[6], track_name,
data[N+9]);
} // playerTrackVote
//-----------------------------------------------------------------------------

View File

@ -528,14 +528,8 @@ void ServerLobbyRoomProtocol::kartSelectionRequested(Event* event)
if (!checkDataSizeAndToken(event, 6))
return;
uint8_t kart_name_size = data.gui8(5);
std::string kart_name = data.getString(6, kart_name_size);
if (kart_name.size() != kart_name_size)
{
Log::error("ServerLobbyRoomProtocol", "Kart names sizes differ: told:"
"%d, real: %d.", kart_name_size, kart_name.size());
return;
}
std::string kart_name;
data.decodeString(5, &kart_name);
// check if selection is possible
if (!m_selection_enabled)
{
@ -566,9 +560,8 @@ void ServerLobbyRoomProtocol::kartSelectionRequested(Event* event)
// send a kart update to everyone
NetworkString answer(3+1+kart_name.size());
// kart update (3), 1, race id
answer.ai8(0x03).ai8(1).ai8(peer->getPlayerProfile()->getPlayerID());
// kart name size, kart name
answer.add(kart_name);
answer.ai8(0x03).ai8(1).ai8(peer->getPlayerProfile()->getPlayerID())
.encodeString(kart_name);
sendMessage(answer);
m_setup->setPlayerKart(peer->getPlayerProfile()->getPlayerID(), kart_name);
} // kartSelectionRequested
@ -687,8 +680,8 @@ void ServerLobbyRoomProtocol::playerTrackVote(Event* event)
STKPeer* peer = event->getPeer();
if (!checkDataSizeAndToken(event, 8))
return;
int N = data[5];
std::string track_name = data.getString(6, N);
std::string track_name;
int N = data.decodeString(5, &track_name);
if (!isByteCorrect(event, N+6, 1))
return;
uint8_t player_id = peer->getPlayerProfile()->getPlayerID();

View File

@ -173,16 +173,15 @@ Online::XMLRequest* ServersManager::getLANRefreshRequest() const
if(len>0)
{
NetworkString s(buffer, len);
uint8_t name_len = s.getUInt8(0);
std::string name = s.getString(1, name_len);
irr::core::stringw name_w =
StringUtils::utf8ToWide(name);
uint8_t max_players = s.getUInt8(1+name_len );
uint8_t players = s.getUInt8(1+name.size()+1);
uint32_t my_ip = s.getUInt32(1+name.size()+2);
uint32_t my_port = s.getUInt16(1+name.size()+6);
irr::core::stringw name;
// name_len is the number of bytes read
uint8_t bytes_read = s.decodeStringW(0, &name);
uint8_t max_players = s.getUInt8(1+bytes_read );
uint8_t players = s.getUInt8(1+bytes_read+1);
uint32_t my_ip = s.getUInt32(1+bytes_read+2);
uint32_t my_port = s.getUInt16(1+bytes_read+6);
ServersManager::get()
->addServer(new Server(name_w, /*lan*/true,
->addServer(new Server(name, /*lan*/true,
max_players, players,
sender) );
TransportAddress me(my_ip, my_port);

View File

@ -525,8 +525,7 @@ void STKHost::handleLANRequests()
// number (which solves the problem which network interface
// might be the right one if there is more than one).
NetworkString s;
s.addUInt8((uint8_t)name.size());
s.addString(name.c_str());
s.encodeString(name);
s.addUInt8(NetworkConfig::get()->getMaxPlayers());
s.addUInt8(0); // FIXME: current number of connected players
s.addUInt32(sender.getIP());