I'm tinkering with creating Multiplayer.
I was thinking about a good architecture to manage sending/receiving stuff and the best I've come up with is this:
So that would only be the Server-side part.
//TCP.h
class TCP
{
public:
TCP(void);
~TCP(void);
sf::SocketTCP m_listener;
sf::SocketTCP m_client;
sf::Thread *m_networkLoop;
};
//TCP.cpp
//Thread functions handling the Socket.
void NetworkThread( void* UserData ){
TCP* tt = static_cast<TCP*>(UserData);
sf::Packet packet;
sf::SocketTCP someClient;
while(true){
//Look for incoming connections and add them to a list
while( tt->m_listener.Accept( someClient ) == sf::Socket::Done ){
//Add someClient to m_clientsList
}
//Looks for incoming packets from m_client.
//Will be changed so it iterates through all sockets in m_clientsList.
while (tt->m_client.Receive( packet ) == sf::Socket::Done){
//Do stuff with packet
}
//If the local machine has some thing to send ..
while( gotSomethingToSend() ){
tt->m_client.Send( packet ); //Would be changed to send it to all sockets in m_clientsList
}
sf::Sleep(0.1f);
}
}
TCP::TCP(void)
{
m_listener.Listen(1234);
m_listener.SetBlocking( false );
m_client.SetBlocking( false );
m_networkLoop = new sf::Thread(&NetworkThread, this);
m_networkLoop->Launch();
}
TCP::~TCP(void)
{
delete m_networkLoop;
}
Now, I understand that there's a lot of problems in this code in regard of how to properly handle Sockets/Threads (like not closing them).
But my main question is whether the concept itself would work.
e.g.
That received packets and incoming connections can be taken from the Socket like events from the event queue.
And that I can use the same socket to send and receive.
Or whether this is a horribly bad idea ( wouldn't be my first one!
).