From 1dea3780a7269cd7ec298578a335b065676b4ac9 Mon Sep 17 00:00:00 2001 From: Ziek Date: Thu, 5 Feb 2015 22:16:34 -0800 Subject: [PATCH] Fixed issue where players were not disconnecting correctly --- Source/Core/Common/TraversalClient.cpp | 16 +++----------- Source/Core/Core/NetPlayClient.cpp | 1 - Source/Core/Core/NetPlayServer.cpp | 29 +++++++++++++++++++++----- Source/Core/Core/NetPlayServer.h | 4 ++-- 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/Source/Core/Common/TraversalClient.cpp b/Source/Core/Common/TraversalClient.cpp index 17e77780c6..736263090d 100644 --- a/Source/Core/Common/TraversalClient.cpp +++ b/Source/Core/Common/TraversalClient.cpp @@ -354,16 +354,6 @@ void ReleaseTraversalClient() if (!g_TraversalClient) return; - if (g_OldPort != 0) - { - // If we were listening at a specific port, kill the - // TraversalClient to avoid hanging on to the port. - g_TraversalClient.reset(); - g_MainNetHost.reset(); - } - else - { - // Reset any pending connection attempts. - g_TraversalClient->Reset(); - } -} + g_TraversalClient.release(); + g_MainNetHost.release(); +} \ No newline at end of file diff --git a/Source/Core/Core/NetPlayClient.cpp b/Source/Core/Core/NetPlayClient.cpp index 55759b6f00..47ae698b86 100644 --- a/Source/Core/Core/NetPlayClient.cpp +++ b/Source/Core/Core/NetPlayClient.cpp @@ -52,7 +52,6 @@ NetPlayClient::~NetPlayClient() { ReleaseTraversalClient(); } - } // called from ---GUI--- thread diff --git a/Source/Core/Core/NetPlayServer.cpp b/Source/Core/Core/NetPlayServer.cpp index 00b278ff87..03ef95523c 100644 --- a/Source/Core/Core/NetPlayServer.cpp +++ b/Source/Core/Core/NetPlayServer.cpp @@ -150,7 +150,11 @@ void NetPlayServer::ThreadFunc() // don't need to lock, this client isn't in the client map std::lock_guard lks(m_crit.send); Send(accept_peer, spac); - + if (netEvent.peer->data) + { + delete (PlayerId *) netEvent.peer->data; + netEvent.peer->data = nullptr; + } enet_peer_disconnect(accept_peer, 0); } } @@ -160,13 +164,19 @@ void NetPlayServer::ThreadFunc() sf::Packet rpac; rpac.append(netEvent.packet->data, netEvent.packet->dataLength); - auto it = m_players.find(netEvent.peer->connectID); + auto it = m_players.find(*(PlayerId *)netEvent.peer->data); Client& client = it->second; if (OnData(rpac, client) != 0) { // if a bad packet is received, disconnect the client std::lock_guard lkg(m_crit.game); OnDisconnect(client); + + if (netEvent.peer->data) + { + delete (PlayerId *)netEvent.peer->data; + netEvent.peer->data = nullptr; + } } enet_packet_destroy(netEvent.packet); } @@ -174,13 +184,17 @@ void NetPlayServer::ThreadFunc() case ENET_EVENT_TYPE_DISCONNECT: { std::lock_guard lkg(m_crit.game); - auto it = m_players.find(netEvent.peer->connectID); + auto it = m_players.find(*(PlayerId *)netEvent.peer->data); if (it != m_players.end()) { Client& client = it->second; OnDisconnect(client); - netEvent.peer->data = nullptr; + if (netEvent.peer->data) + { + delete (PlayerId *)netEvent.peer->data; + netEvent.peer->data = nullptr; + } } } break; @@ -192,7 +206,11 @@ void NetPlayServer::ThreadFunc() // close listening socket and client sockets for (auto& player_entry : m_players) + { + delete (PlayerId *)player_entry.second.socket->data; + player_entry.second.socket->data = nullptr; enet_peer_disconnect(player_entry.second.socket, 0); + } } // called from ---NETPLAY--- thread @@ -241,6 +259,7 @@ unsigned int NetPlayServer::OnConnect(ENetPeer* socket) } } player.pid = pid; + socket->data = new PlayerId(pid); // try to automatically assign new user a pad for (PadMapping& mapping : m_pad_map) @@ -296,7 +315,7 @@ unsigned int NetPlayServer::OnConnect(ENetPeer* socket) // add client to the player list { std::lock_guard lkp(m_crit.players); - m_players.insert(std::pair(player.socket->connectID, player)); + m_players.insert(std::pair(*(PlayerId *)player.socket->data, player)); std::lock_guard lks(m_crit.send); UpdatePadMapping(); // sync pad mappings with everyone UpdateWiimoteMapping(); diff --git a/Source/Core/Core/NetPlayServer.h b/Source/Core/Core/NetPlayServer.h index 3a589aa2ca..93e28afb57 100644 --- a/Source/Core/Core/NetPlayServer.h +++ b/Source/Core/Core/NetPlayServer.h @@ -95,7 +95,7 @@ private: PadMapping m_pad_map[4]; PadMapping m_wiimote_map[4]; - std::map m_players; + std::map m_players; struct { @@ -126,4 +126,4 @@ private: static bool m_upnp_error; static std::thread m_upnp_thread; #endif -}; \ No newline at end of file +};