Implementing server
Let's start by taking a look at the second constructor and the destructor of this class:
Server::Server(void(*l_handler)(sf::IpAddress&, const PortNumber&, const PacketID&, sf::Packet&, Server*)) : m_listenThread(&Server::Listen, this) { // Bind a packet handler function. m_packetHandler = std::bind(l_handler, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5); } Server::~Server(){ Stop(); }
Nothing too interesting is happening here. The constructor simply binds the provided packet handler function, while the destructor just invokes the Stop
method, which we are going to cover shortly. Speaking of binding, we also need a function to handle client timeouts:
void Server::BindTimeoutHandler(void(*l_handler) (const ClientID&)) { m_timeoutHandler = std::bind(l_handler, std::placeholders::_1); }
Simply having the client get disconnected is not always...