Network: Handle the disconnect of a client

master
B3n30 2017-07-09 10:43:53 +07:00
parent 641346c15c
commit 35a0b32553
1 changed files with 18 additions and 1 deletions

@ -2,6 +2,7 @@
// Licensed under GPLv2 or any later version // Licensed under GPLv2 or any later version
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <algorithm>
#include <atomic> #include <atomic>
#include <random> #include <random>
#include <thread> #include <thread>
@ -104,6 +105,12 @@ public:
* @param event The ENet event containing the data * @param event The ENet event containing the data
*/ */
void HandleWifiPacket(const ENetEvent* event); void HandleWifiPacket(const ENetEvent* event);
/**
* Removes the client from the members list if it was in it and announces the change
* to all other clients.
*/
void HandleClientDisconnection(ENetPeer* client);
}; };
// RoomImpl // RoomImpl
@ -125,7 +132,7 @@ void Room::RoomImpl::ServerLoop() {
enet_packet_destroy(event.packet); enet_packet_destroy(event.packet);
break; break;
case ENET_EVENT_TYPE_DISCONNECT: case ENET_EVENT_TYPE_DISCONNECT:
// TODO(B3N30): Handle the disconnect from a client HandleClientDisconnection(event.peer);
break; break;
} }
} }
@ -264,6 +271,16 @@ void Room::RoomImpl::HandleWifiPacket(const ENetEvent* event) {
enet_host_flush(server); enet_host_flush(server);
} }
void Room::RoomImpl::HandleClientDisconnection(ENetPeer* client) {
// Remove the client from the members list.
members.erase(std::remove_if(members.begin(), members.end(),
[&](const Member& member) { return member.peer == client; }),
members.end());
// Announce the change to all clients.
BroadcastRoomInformation();
}
// Room // Room
Room::Room() : room_impl{std::make_unique<RoomImpl>()} {} Room::Room() : room_impl{std::make_unique<RoomImpl>()} {}