From b41739146e8c3d8ba30d496e0aed70582ca2eba5 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Sun, 23 May 2021 02:43:18 +0200 Subject: [PATCH] Fixed server crash when two players tried to connect with same name. --- source/server/network/Server.cpp | 32 +++++++++---------- source/server/network/Server.hpp | 3 +- .../server/network/ServerCommandHandler.cpp | 17 +++++++--- .../server/network/ServerCommandHandler.hpp | 2 +- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/source/server/network/Server.cpp b/source/server/network/Server.cpp index 31cc832cc..ca3b56040 100644 --- a/source/server/network/Server.cpp +++ b/source/server/network/Server.cpp @@ -43,6 +43,22 @@ void Server::disconnectAllClients() { disconnectClient(it); } +void Server::disconnectClient(ClientInfo &client) { + Network::Packet packet; + packet << Network::Command::ClientDisconnect << client.id; + sendToAllClients(packet); + + m_commands[Network::Command::ClientDisconnect](client, packet); + + m_selector.remove(*client.tcpSocket); + m_info.removeClient(client.id); + + if (m_isSingleplayer && m_info.clients().size() == 0) { + m_tcpListener.close(); + m_isRunning = false; + } +} + void Server::handleGameEvents() { if (m_selector.wait(sf::milliseconds(10))) { if (m_selector.isReady(m_tcpListener)) { @@ -121,22 +137,6 @@ void Server::handleClientMessages() { } } -void Server::disconnectClient(ClientInfo &client) { - Network::Packet packet; - packet << Network::Command::ClientDisconnect << client.id; - sendToAllClients(packet); - - m_commands[Network::Command::ClientDisconnect](client, packet); - - m_selector.remove(*client.tcpSocket); - m_info.removeClient(client.id); - - if (m_isSingleplayer && m_info.clients().size() == 0) { - m_tcpListener.close(); - m_isRunning = false; - } -} - void Server::sendToAllClients(Network::Packet &packet) const { for (const ClientInfo &client : m_info.clients()) { client.tcpSocket->send(packet); diff --git a/source/server/network/Server.hpp b/source/server/network/Server.hpp index 6113e9c4d..36fa26069 100644 --- a/source/server/network/Server.hpp +++ b/source/server/network/Server.hpp @@ -44,6 +44,7 @@ class Server { void init(u16 port = 4242); void disconnectAllClients(); + void disconnectClient(ClientInfo &client); void handleGameEvents(); @@ -66,8 +67,6 @@ class Server { void handleNewConnections(); void handleClientMessages(); - void disconnectClient(ClientInfo &client); - bool m_isRunning = false; bool m_isSingleplayer = false; diff --git a/source/server/network/ServerCommandHandler.cpp b/source/server/network/ServerCommandHandler.cpp index e3b9e9918..0768907f8 100644 --- a/source/server/network/ServerCommandHandler.cpp +++ b/source/server/network/ServerCommandHandler.cpp @@ -48,14 +48,18 @@ void ServerCommandHandler::sendServerTick(const ClientInfo *client) const { client->tcpSocket->send(packet); } -void ServerCommandHandler::sendServerClosed(const std::string &message, const ClientInfo *client) const { +void ServerCommandHandler::sendServerClosed(const std::string &message, ClientInfo *client) const { Network::Packet packet; packet << Network::Command::ServerClosed << message; - if (!client) + if (!client) { m_server.sendToAllClients(packet); - else + m_server.disconnectAllClients(); + } + else { client->tcpSocket->send(packet); + m_server.disconnectClient(*client); + } } void ServerCommandHandler::sendChunkUnload(s32 chunkX, s32 chunkY, s32 chunkZ, const ClientInfo *client) const { @@ -294,8 +298,11 @@ void ServerCommandHandler::setupCallbacks() { }); m_server.setCommandCallback(Network::Command::ClientDisconnect, [this](ClientInfo &client, Network::Packet &) { - m_players.getPlayerFromClientID(client.id)->clearLoadedChunks(); - m_players.disconnectPlayer(client.playerName); + ServerPlayer *player = m_players.getPlayerFromClientID(client.id); + if (player) { + player->clearLoadedChunks(); + m_players.disconnectPlayer(client.playerName); + } }); m_server.setCommandCallback(Network::Command::ChunkUnload, [this](ClientInfo &client, Network::Packet &packet) { diff --git a/source/server/network/ServerCommandHandler.hpp b/source/server/network/ServerCommandHandler.hpp index d5ed4c8ec..adf240618 100644 --- a/source/server/network/ServerCommandHandler.hpp +++ b/source/server/network/ServerCommandHandler.hpp @@ -62,7 +62,7 @@ class ServerCommandHandler { : m_scriptEngine(scriptEngine), m_server(server), m_worldController(worldController), m_players(players), m_registry(registry) {} void sendServerTick(const ClientInfo *client = nullptr) const; - void sendServerClosed(const std::string &message, const ClientInfo *client = nullptr) const; + void sendServerClosed(const std::string &message, ClientInfo *client = nullptr) const; void sendChunkUnload(s32 chunkX, s32 chunkY, s32 chunkZ, const ClientInfo *client = nullptr) const; void sendBlockDataUpdate(s32 x, s32 y, s32 z, const BlockData *blockData, const ClientInfo *client = nullptr) const; void sendBlockInvUpdate(s32 x, s32 y, s32 z, const Inventory &inventory, const ClientInfo *client = nullptr) const;