O'Reilly logo

SFML Game Development By Example by Raimondas Pupius

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required