You should be using, last I recall, the NIO sockets.
thank you very much, didn't know about this!
First of all, Transformice is not an MMO, (each game has a very low number of players so you don't need to deal with MMO scale networking for this type of game).
What you should do is have one master server for the "lobby" and then run multiple game servers, They can but doesn't have to be on the same machine but needs different ports and should be handled by their own instance of the server application.
Have the master server keep track of how many instances of the game server each node is running and launch new ones as needed on the least busy node, (You can have game servers report their nodes status to the master server for this purpose)
For the game servers 1 or 2 threads are enough, (having one thread that only waits for new connections and one that runs the game can simplify things slightly)
I'm not sure if I understand your post. Do you mean that rather than spawning a new thread per client connected, I run some kind of game server node for handle the new client connected ? Or do you mean these game server nodes should be able to handle more than one client ? Because that is what really I was wondering. I can't figure out a way for handling multiple clients without spawning a new thread for each connection.
Maybe I'm a little bad for these things also thank you very much for your answer too!
You'll most likely have multiple nodes, each node runs a number of game servers, (each game server hosts 1 game with X players), the game server should have 1 thread for all clients, possibly 2 if you want to use blocking sockets (not really recommended), If you're allowing players to create games themselves you'll have the master server tell the least populated node to launch a new game server, (This can quite easily be done using ssh) before directing the player to it, if players can't create games then you just launch new game servers when the currently running ones are full).
as for the game server handling multiple connections its fairly easy.
loop through the connections and deal with them one at the time. use non blocking sockets to avoid having reads and writes stall the application. (If you use blocking sockets you have to be very careful and always check how much data you can attempt to read before you try to read it and you can't ever write more data than what you can fit in the sockets output buffer)
When reading data you should take all the data that is currently on the socket and place it in your own buffer, extract any complete "packets" from it and handle those, save any data belonging to incomplete packets in the buffer (The next time you read from the same socket you'll get the rest of it.
When writing data you have to do the opposite, keep your own buffer of data to be sent, try to write it all to the socket, check the return value from the write function (it indicates how many bytes was actually written) and remove that number of bytes from your buffer then move on to the next socket in the list.
The important thing is to never stop to wait for a socket.
If your server runs at a fairly low update rate you can increase the size of the recieve buffer using the setRecieveBufferSize method (allthough its only a hint to the underlying implementation, if you have a problem with it filling up and can't increase it enough you might benefit by moving the socket reads to their own thread to get data to your own buffers faster. (Your own buffers aren't limited in size by the platform in the same way) but usually its not necessary (a game shouldn't generate that much data on a single socket)
Edited by SimonForsman, 05 May 2012 - 06:45 PM.