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, ...
Get SFML Game Development By Example now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.