Skip to content

Commit

Permalink
Fixed server crash when two players tried to connect with same name.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed May 23, 2021
1 parent d738e0c commit b417391
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
32 changes: 16 additions & 16 deletions source/server/network/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions source/server/network/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Server {
void init(u16 port = 4242);

void disconnectAllClients();
void disconnectClient(ClientInfo &client);

void handleGameEvents();

Expand All @@ -66,8 +67,6 @@ class Server {
void handleNewConnections();
void handleClientMessages();

void disconnectClient(ClientInfo &client);

bool m_isRunning = false;
bool m_isSingleplayer = false;

Expand Down
17 changes: 12 additions & 5 deletions source/server/network/ServerCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion source/server/network/ServerCommandHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b417391

Please sign in to comment.