Implementing the client
With the class definition covered, it's time to actually make it do something, starting with the constructor and destructor:
Client::Client():m_listenThread(&Client::Listen, this){}
Client::~Client(){ m_socket.unbind(); }In the client constructor's initializer list, we bind the listening thread to the Listen method of this class. Threads do not have a default empty constructor, which is why this is necessary. The destructor is simply used to unbind the socket we're using.
Now, let's take a stab at implementing the connection protocol:
bool Client::Connect(){
if (m_connected){ return false; }
m_socket.bind(sf::Socket::AnyPort);
sf::Packet p;
StampPacket(PacketType::Connect, p);
p << m_playerName;
if (m_socket.send(p, m_serverIp, m_serverPort) !=
sf::Socket::Done)
{
m_socket.unbind();
return false;
}
m_socket.setBlocking(false);
p.clear();
sf::IpAddress recvIP;
PortNumber recvPORT;
sf::Clock timer;
...