From 85822e682e978b844b4a9d37e4b5fb2a28ce79bb Mon Sep 17 00:00:00 2001 From: rna88 Date: Tue, 13 Mar 2018 04:22:22 -0700 Subject: [PATCH] Fix getPeerCount() to return correct number of peers Calling getPeerCount() used to return the maximum number of peers on the host, no matter how many peers were actually connected. Changing this function to count the elements within the "players" vector returns the correct peer count. However calling getPeerCount() within the "onDisconnect" callback results in an extra peer being present due to the "players" vector only being updated *after* the callback invocation. Hence the "onDisconnect" callback is now invoked after the disconnecting peer is removed from the vector. * Count peers by looping through the "players" vector. * Move invocation of "onDisconnect" callback to after peer is deleted. * Add getPeerCount() calls to example 3 to illustrate use. --- examples/03.ClientManagement/main.cpp | 17 ++++++++++++++--- source/CNetManager.cpp | 22 +++++++++++++++++----- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/examples/03.ClientManagement/main.cpp b/examples/03.ClientManagement/main.cpp index c137c95..fe195ac 100644 --- a/examples/03.ClientManagement/main.cpp +++ b/examples/03.ClientManagement/main.cpp @@ -85,9 +85,15 @@ public: core::stringc message; message = "Client number "; message += playerId; - message += " has just connected."; + message += " has just connected. "; + message += netManager->getPeerCount(); + message += " peer(s) total."; packet << message; netManager->sendOutPacket(packet); + + std::cout << "Client number " << playerId << " connected. " + << netManager->getPeerCount() << " peer(s) total." << std::endl; + } } @@ -100,10 +106,15 @@ public: core::stringc message; message = "Client number "; message += playerId; - message += " has just left the building."; + message += " has just left the building. "; + message += netManager->getPeerCount(); + message += " peer(s) left."; packet << message; netManager->sendOutPacket(packet); - std::cout << "Client number " << playerId << " disconnected" << std::endl; + + std::cout << "Client number " << playerId << " disconnected. " + << netManager->getPeerCount() << " peer(s) left." << std::endl; + } // Handle the packets, as usual. diff --git a/source/CNetManager.cpp b/source/CNetManager.cpp index 48993a4..95dc0be 100644 --- a/source/CNetManager.cpp +++ b/source/CNetManager.cpp @@ -316,16 +316,18 @@ namespace irr if(pData) { + u16 disconnectingPID = pData->playerID; + if(verbose) std::cout << "irrNetLite: Player number " - << pData->playerID + << disconnectingPID << " disconnected.\n"; - if(pHandler) pHandler->onDisconnect(pData->playerID); - - players[pData->playerID] = 0; + players[disconnectingPID] = 0; delete pData; event.peer->data = 0; + + if(pHandler) pHandler->onDisconnect(disconnectingPID); } } default: @@ -366,7 +368,17 @@ namespace irr const u32 CNetManager::getPeerCount() { - return (u32)host->peerCount; + u32 count = 0; + + for (u32 i = 1; i < netParams.maxClients; ++i) + { + if (players[i]) + { + ++count; + } + } + + return count; } const u16 CNetManager::getPlayerNumber()