I'm going to describe a new class, (last one before I start with the server)
We already have a class to store persistent data, now we need a way to communicate with the world.
I need a class to start a TCP socket server then accept incoming TCP connections , I need to be able to send data to any of the clients too, and finally enqueue the incoming data so I can process it when I need to do.
What the class is going to do is:
- Open a server socket in a port X
- Start a thread that will be listening in port X
- When we receive connections it will save the client info (ip/port) to a vector so we can communicate with the client, every client starts a new thread to get the incoming data from each client (it is a blocking-socket server)
- The data is stored into two different queues depending if the client has logged in or not, I made that because I will apply different priority for the queues , the main (client updates) must be processed with maximum priority other way clients will be lagging.. in the other hand client login request can wait a pair of seconds without big problems.
Then I have the client implementation:
Notice the client stores the data in two different queues depending if the client has loged in or not, finally we have the main socket server class:
There are many things here, but I will resume it to the maximum, to do that, below there is a simple example to use the class:
- We include the class
- Then instantiate it
- Start the listener thread (at this moment it is fully functional)
- Then I extract a message from the input queue (let's suppose we have a connected client that send it..)
- I send a message to the server ("data")
- Finally, I close the server
That's all for now, in the next post I will mix everything to make the client login a reality, I will make a text based client too and hope everything is clear that way.