Prevent usage of duplicated host ids (before if hosts A and B are connected,

then A disconnects and C connects, B and C would have the same host id).
This commit is contained in:
hiker 2016-02-02 09:52:07 +11:00
parent ef8c04e117
commit 4f12a9a66c
3 changed files with 21 additions and 1 deletions

View File

@ -456,7 +456,9 @@ void ServerLobbyRoomProtocol::connectionRequested(Event* event)
if(m_setup->getLocalMasterID()==0)
m_setup->setLocalMaster(new_player_id);
int new_host_id = STKHost::get()->getPeerCount();
// The host id has already been incremented when the peer
// was added, so it is the right id now.
int new_host_id = STKHost::get()->getNextHostId();
// Notify everybody that there is a new player
// -------------------------------------------

View File

@ -225,6 +225,7 @@ void STKHost::create()
*/
STKHost::STKHost(uint32_t server_id, uint32_t host_id)
{
m_next_unique_host_id = -1;
// Will be overwritten with the correct value once a connection with the
// server is made.
m_host_id = 0;
@ -249,6 +250,9 @@ STKHost::STKHost(uint32_t server_id, uint32_t host_id)
STKHost::STKHost(const irr::core::stringw &server_name)
{
init();
// The host id will be increased whenever a new peer is added, so the
// first client will have host id 1 (host id 0 is the server).
m_next_unique_host_id = 0;
m_host_id = 0; // indicates a server host.
ENetAddress addr;
@ -671,6 +675,7 @@ STKPeer* STKHost::getPeer(ENetPeer *enet_peer)
peer, enet_peer);
m_peers.push_back(peer);
m_next_unique_host_id ++;
return peer;
} // getPeer
// ----------------------------------------------------------------------------

View File

@ -74,6 +74,12 @@ private:
/** The list of peers connected to this instance. */
std::vector<STKPeer*> m_peers;
/** Next unique host id. It is increased whenever a new peer is added (see
* getPeer()), but not decreased whena host (=peer) disconnects. This
* results in a unique host id for each host, even when a host should
* disconnect and then reconnect. */
int m_next_unique_host_id;
/** Host id of this host. */
uint8_t m_host_id;
@ -201,6 +207,13 @@ public:
/** Returns a const reference to the list of peers. */
const std::vector<STKPeer*> &getPeers() { return m_peers; }
// --------------------------------------------------------------------
/** Returns the next (unique) host id. */
unsigned int getNextHostId() const
{
assert(m_next_unique_host_id >= 0);
return m_next_unique_host_id;
}
// --------------------------------------------------------------------
/** Returns the number of currently connected peers. */
unsigned int getPeerCount() { return (int)m_peers.size(); }
// --------------------------------------------------------------------