Skip to content

Commit

Permalink
[Server] Now forwaring ClientDisconnection packet to all clients. Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Mar 9, 2020
1 parent 1a9a0db commit 8d44d25
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
16 changes: 14 additions & 2 deletions client/source/network/ClientCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ void ClientCommandHandler::sendChatMessage(const std::string &message) {
}

void ClientCommandHandler::setupCallbacks() {
m_client.setCommandCallback(Network::Command::ClientDisconnect, [this](sf::Packet &packet) {
u16 clientID;
packet >> clientID;

auto it = m_playerBoxes.find(clientID);
if (it != m_playerBoxes.end())
m_playerBoxes.erase(it);
});

m_client.setCommandCallback(Network::Command::RegistryData, [this](sf::Packet &packet) {
// FIXME: This is a quick fix for concurrency between client and server in singleplayer
if (!m_isSingleplayer)
Expand Down Expand Up @@ -157,8 +166,11 @@ void ClientCommandHandler::setupCallbacks() {
packet >> x >> y >> z;
packet >> isTeleportation;

if (clientId != m_client.id())
m_playerBoxes.at(clientId).setPosition(x, y, z);
if (clientId != m_client.id()) {
auto it = m_playerBoxes.find(clientId);
if (it != m_playerBoxes.end())
it->second.setPosition(x, y, z);
}
else if (isTeleportation) {
m_player.setPosition(x, y, z);
}
Expand Down
6 changes: 6 additions & 0 deletions server/source/network/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ void Server::handleClientMessages() {
}

void Server::disconnectClient(ClientInfo &client) {
sf::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);

Expand Down
4 changes: 3 additions & 1 deletion server/source/network/ServerCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ void ServerCommandHandler::setupCallbacks() {
});

m_server.setCommandCallback(Network::Command::ClientDisconnect, [this](ClientInfo &client, sf::Packet &) {
m_players.erase(client.id);
auto it = m_players.find(client.id);
if (it != m_players.end())
m_players.erase(it);
});

m_server.setCommandCallback(Network::Command::ChunkRequest, [this](ClientInfo &client, sf::Packet &packet) {
Expand Down

0 comments on commit 8d44d25

Please sign in to comment.