Fixed various bugs caused by switching to new NetworkString class.

This commit is contained in:
hiker 2016-02-26 16:38:59 +11:00
parent 5c32cbe1be
commit be815e4234
6 changed files with 45 additions and 48 deletions

View File

@ -20,6 +20,7 @@
#include "utils/string_utils.hpp"
#include <algorithm> // for std::min
#include <iomanip>
#include <ostream>
// ============================================================================
@ -126,11 +127,11 @@ std::string BareNetworkString::getLogMessage() const
std::ostringstream oss;
for(unsigned int line=0; line<16; line+=16)
{
oss << line << " : ";
oss << std::hex << std::setw(3) << line << " : ";
unsigned int upper_limit = std::min(line+16, size());
for(unsigned int i=line; i<upper_limit; i++)
{
oss << m_buffer[i];
oss << std::hex << std::setw(2) << int(m_buffer[i]);
if(i%2==1) oss << " ";
} // for i
// Add ascii representation

View File

@ -145,8 +145,8 @@ public:
const char* getData() const { return (char*)(m_buffer.data()); };
// ------------------------------------------------------------------------
/** Returns the current length of the network string. */
unsigned int size() const { return (int)m_buffer.size(); }
/** Returns the remaining length of the network string. */
unsigned int size() const { return (int)m_buffer.size()-m_current_offset; }
// ------------------------------------------------------------------------
// All functions related to adding data to a network string
@ -159,7 +159,7 @@ public:
// ------------------------------------------------------------------------
/** Adds a single character to the string. */
BareNetworkString& addChar(const char& value)
BareNetworkString& addChar(const char value)
{
m_buffer.push_back((uint8_t)(value));
return *this;
@ -295,17 +295,23 @@ class NewNetworkString : public BareNetworkString
public:
static void unitTesting();
/** Constructor, sets the protocol type of this message. */
/** Constructor for a message to be sent. It sets the
* protocol type of this message. */
NewNetworkString(ProtocolType type, int capacity=16)
: BareNetworkString(capacity)
{
m_buffer.push_back(type);
addUInt32(0); // add dummy token for now
} // NewNetworkString
// ------------------------------------------------------------------------
/** Constructor for a received message. It automatically ignored the first
* 5 bytes which contain the type and token. Those will be access using
* special functions. */
NewNetworkString(const uint8_t *data, int len)
: BareNetworkString((char*)data, len)
{
m_current_offset = 5; // ignore type and token
} // NewNetworkString
// ------------------------------------------------------------------------
@ -313,7 +319,7 @@ public:
ProtocolType getProtocolType() const
{
assert(!m_buffer.empty());
return (ProtocolType)m_buffer[0];
return (ProtocolType)(m_buffer[0] & ~PROTOCOL_SYNCHRONOUS);
} // getProtocolType
// ------------------------------------------------------------------------
@ -323,13 +329,13 @@ public:
if(b)
m_buffer[0] |= PROTOCOL_SYNCHRONOUS;
else
m_buffer[0] &= !PROTOCOL_SYNCHRONOUS;
m_buffer[0] &= ~PROTOCOL_SYNCHRONOUS;
} // setSynchronous
// ------------------------------------------------------------------------
/** Returns if this message is synchronous or not. */
bool isSynchronous() const
{
return (m_buffer[0] & PROTOCOL_SYNCHRONOUS) != 0;
return (m_buffer[0] & PROTOCOL_SYNCHRONOUS) == PROTOCOL_SYNCHRONOUS;
} // isSynchronous
// ------------------------------------------------------------------------
/** Sets a token for a message. Note that the token in an already

View File

@ -110,8 +110,7 @@ void ProtocolManager::propagateEvent(Event* event)
{
if (event->data().size() > 0)
{
searched_protocol = (ProtocolType)(event->data()[0]);
event->removeFront(1);
searched_protocol = event->data().getProtocolType();
}
else
{
@ -125,14 +124,7 @@ void ProtocolManager::propagateEvent(Event* event)
Log::verbose("ProtocolManager", "Received event for protocols of type %d",
searched_protocol);
bool is_synchronous = false;
if(searched_protocol & PROTOCOL_SYNCHRONOUS)
{
is_synchronous = true;
// Reset synchronous flag to restore original protocol id
searched_protocol = ProtocolType(searched_protocol &
~PROTOCOL_SYNCHRONOUS );
}
std::vector<unsigned int> protocols_ids;
m_protocols.lock();
for (unsigned int i = 0; i < m_protocols.getData().size() ; i++)
@ -163,7 +155,9 @@ void ProtocolManager::propagateEvent(Event* event)
{
EventProcessingInfo epi;
epi.m_arrival_time = (double)StkTime::getTimeSinceEpoch();
epi.m_is_synchronous = is_synchronous;
// Only message events will optionally be delivered synchronously
epi.m_is_synchronous = event->getType()==EVENT_TYPE_MESSAGE &&
event->data().isSynchronous();
epi.m_event = event;
epi.m_protocols_ids = protocols_ids;
// Add the event to the queue. After the event is handled

View File

@ -393,17 +393,17 @@ void ClientLobbyRoomProtocol::disconnectedPlayer(Event* event)
* \param event : Event providing the information.
*
* Format of the data :
* Byte 0 1 2 3 7 8 9
* -----------------------------------------------------------------------------
* Size | 1 | 1 | 1 | 4 | 1 | 1 | |
* Data | 1 | 0 <= race id < 16 | 4 | priv token | hostid | authorised |playernames* |
* ------------------------------------------------------------------------------
* Byte 0 1 2 3
* ---------------------------------------------------------
* Size | 1 | 1 | 1 | |
* Data | 0 <= race id < 16 | hostid | authorised |playernames* |
* ---------------------------------------------------------
*/
void ClientLobbyRoomProtocol::connectionAccepted(Event* event)
{
const NewNetworkString &data = event->data();
NewNetworkString &data = event->data();
// At least 12 bytes should remain now
if (data.size() < 9|| data[0] != 1 || data[2] != 4)
if (data.size() < 3)
{
Log::error("ClientLobbyRoomProtocol",
"A message notifying an accepted connection wasn't "
@ -423,9 +423,10 @@ void ClientLobbyRoomProtocol::connectionAccepted(Event* event)
name = PlayerManager::getCurrentOnlineUserName();
else
name = PlayerManager::getCurrentPlayer()->getName();
uint8_t my_player_id = data.getUInt8(1);
uint8_t my_host_id = data.getUInt8(7);
uint8_t authorised = data.getUInt8(8);
uint8_t my_player_id = data.getUInt8(0);
uint8_t my_host_id = data.getUInt8(1);
uint8_t authorised = data.getUInt8(2);
data.removeFront(3);
// Store this client's authorisation status in the peer information
// for the server.
event->getPeer()->setAuthorised(authorised!=0);
@ -436,31 +437,26 @@ void ClientLobbyRoomProtocol::connectionAccepted(Event* event)
STKHost::get()->getGameSetup()->setLocalMaster(my_player_id);
m_setup->setNumLocalPlayers(1);
// connection token
uint32_t token = data.getUInt32(3);
uint32_t token = data.getToken();
peer->setClientServerToken(token);
// Add all players
// ===============
unsigned int n = 9;
while (n < data.size())
while (data.size() > 0)
{
if (data[n] != 1 )
Log::error("ClientLobbyRoomProtocol",
"Bad format in players list.");
uint8_t player_id = data[n + 1];
uint8_t player_id = data[0];
uint8_t host_id = data[1];
irr::core::stringw name;
int bytes_read = data.decodeStringW(n + 2, &name);
uint8_t host_id = data.getUInt8(n+2+bytes_read);
int bytes_read = data.decodeStringW(2, &name);
NetworkPlayerProfile* profile2 =
new NetworkPlayerProfile(player_id, name);
profile2->setHostId(host_id);
m_setup->addPlayer(profile2);
n += bytes_read+3;
// 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

View File

@ -496,15 +496,15 @@ void ServerLobbyRoomProtocol::connectionRequested(Event* event)
NewNetworkString *message_ack = getNetworkString(14 + players.size() * 7);
message_ack->setToken(token);
// connection success -- size of token -- token
message_ack->addUInt8(LE_CONNECTION_ACCEPTED).addUInt8(1).addUInt8(new_player_id).addUInt8(4)
.addUInt8(new_host_id).addUInt8(is_authorised);
message_ack->addUInt8(LE_CONNECTION_ACCEPTED).addUInt8(new_player_id)
.addUInt8(new_host_id).addUInt8(is_authorised);
// Add all players so that this user knows (this new player is only added
// to the list of players later, so the new player's info is not included)
for (unsigned int i = 0; i < players.size(); i++)
{
message_ack->addUInt8(1).addUInt8(players[i]->getGlobalPlayerId())
.encodeString(players[i]->getName())
.addUInt8(players[i]->getHostId());
message_ack->addUInt8(players[i]->getGlobalPlayerId())
.addUInt8(players[i]->getHostId())
.encodeString(players[i]->getName());
}
sendMessage(peer, *message_ack);
delete message_ack;

View File

@ -68,7 +68,7 @@ void STKPeer::sendPacket(NewNetworkString const& data, bool reliable)
Log::verbose("STKPeer", "sending packet of size %d to %s",
data.size(), a.toString().c_str());
ENetPacket* packet = enet_packet_create(data.getData(), data.size() + 1,
ENetPacket* packet = enet_packet_create(data.getData(), data.size(),
(reliable ? ENET_PACKET_FLAG_RELIABLE
: ENET_PACKET_FLAG_UNSEQUENCED));
enet_peer_send(m_enet_peer, 0, packet);