The Lobby Server
Once a functioning client and host were running i could once again turn my eyes on the lobby server.
To get a quick start i took the current Game server, copied it over and tore out most of what made it a game server.
I lauched the lobby server and connected a client to it, and wouldn't you know since it was running the same code it worked.
The next logical step was to make the host a client..
Wait thats not logical at all but bear with me. Since i wanted the system as a whole to be expandable while running i needed to be able to hook up and remove servers on the fly, now that sounds an awfull lot like clients being able to connect and disconnect so thats where that line of thinking is coming from.
So once i ripped out the client part of the client and put it in the host aswell iquickly realized that because of the way i had gone about it the lobby server had no idea what was connecting to it, it could be a client or a host, some form of identification step was necessary. So one was added, upon connecting to a server the client or host identifies what service it's providing/requesting from the server.
The next step
With both a client and a host connected to the lobby and identified the next step would be to connect them to eachother.
Simple enough, Send the address of the host to the client and reconect to the host.
If only that would have been that easy, well it was... i was the one that made it hard. since i wanted to simulate something realistic i wanted to make sure the client connected to the optimal host. So the question arose, what is the optimal host?
The criteria i settled on were:
- Can the server handle my load or will i decrease the tickrate below some arbitrary threshold ruining the experience.
- What's the latency of my actions i.e how long does a round-trip-time ping take.
So now i needed to measure. I started with trying to figure out the load on each server to see if any of them needn't be considered at all. This was simply solved by having the hosts selfrapport to the lobby which also served as an excellent way to filter out dead servers.
Pingtime was a bit harder however as my network interface was only built for having one on one communication.
Since only neaded to send one packet i didn't want to to start up a whole socket just to test the ping so i had to rewrite the interface a bit to allow what i called customly Addressed packets. once every relevant function had it's arguments changed to reflect the exsistance of these packets everything went swimmingly. If i had had more time on this project i would have wanted to rework the interface to better allow multiple connections.
Now measuring the ping of any server was easy create a pingpacket, hock a callback send it of to a custom address. If it takes to long assume the server went down and discard it.
Once all servers have been evaluated by the client send the results back to the server and wait for a decision.
Had this been a real game this is where the player would have been able to interfer and connect to a specific server of their chosing if they wanted to play with friends etc.
Once the lobby server gets the results of every possible server (or too much time has passed) it figures out which of them would be best suited based of of current load and round-trip-time to each of them. It then send off the result to the client who quickly disconnects and connects to the chosen server instead.
I played around a bit with the thought of making this whole process recursive i.e repeat until a host is found, and have more than one lobby server in a large tree structure. Sadly due to time constraints i didn't have the chance to add this functionality.