Game Development Community

dev|Pro Game Development Curriculum

Implementing a simple master server

by Guimo · 07/13/2011 (11:04 pm) · 7 comments

Ok, so every now and then I hear that someone cries for a master server or some kind of support for a master server. So I decided to just go and make a simple one for the sake of just making everyone happy. Took me about two hours of coding and testing. This is a basic implementation so feel free to expand on it.

The source code is here.

This master server uses Java and interacts with Torque through a TCP connection. I will assume you know Java, Eclipse, Torquescript. I guess if you are in a level where you need this resource then you should already be comfortable with TorqueScript.

Give me credit if you want or send me a cookie.

<b>The concept</b>
Games needs servers. Indeed the best parto of Torque IMHO is the networking architecture and how it seamlessly connects games. The theory behind every example is, create a game host, then let players connect. Of course thats easy if you know the IP address of the other guys in your network. The problem is when you create a game host and want to invite other players to join your game using the web and that's where you need a server which let players connect and know who is hosting a game and the IP and port they need to connect to.

So, a master server is a service running on a computer and listening to a port. Some players will connect to that server and register a new game by sending the game information (id, name, ip, port), others will connect and request the list of games which are currently running and will receive the list of games with the ip and port info so they know where to connect.

<b>Requirement</b>
Eclipse. Preferably with the Maven plugin installed. If you are proficient with Java you should be able to move this to any other IDE.
A router and a DynDNS account (or similar) if you will connect on Internet.

<b>Basic setup<>
The resource includes a script and a project folder. That project is ready to be loaded in Eclipse so open your Eclipse and compile it. When you run the program it will open and listen on the port 9100 (Check MasterServer main procedure for the startup commands).

The MasterServer is mutithreaded but shares the same game list data structure. This data structure is protected through synchronized methods and flags so that there should be no problems adding or removing games by multiple players at the same time.

So, when you run it you will see a message in the Eclipse console saying "Listening to port 9100".

Test your connection with telnet (Windows Vista/7 users will need to add the telnet support, read the Windows Knowledge base). Run: "telnet localhost 9100" in the command promt and you will see a YESMASTER? message in telnet and a connection accepted message in the Eclipse console. YESMASTER? is the message this server sends to mark a successful connection.

So, now that your server is running, why dont you try opening it to the world? Use your DynDNS account and syncronize your current IP address with the DynDNS server so your IP address is known to the world. Say you use the name: mygameserver.dyndns.org. Install the DynDNS program to keep your computer IP and the DNS name synchronized.

If you plan this to be used on the web, the next step is to open a port to your router. Use your Router NAT setup and redirect any request from the internet on the port 9100 to your computer IP. For everythign to work properly, your computer should be assigned a fixed internal IP (not by DHCP). Say your computer has the IP 192.168.1.1, then your router should redirect the internet TCP traffic for port 9100 to the internal IP 192.168.1.1 port 9100. Effectively you have created a bridge to the web.

Please make sure all your other ports are closed if you don't need them.

So then you can test this from your computer using telnet as: "telnet mygameserver.dyndns.org 9100" and you should get a YESMASTER? respopnse. Now go to your office or buddys house and test the same command. It should respond fine provided your computer is properly setup.

<b>Connecting from Torque</b>
The MasterServerClient.cs script contains a simple script to handle some communication with your server. Copy the script to an appropriate location in your Torque game and load it from a proper place (i.e. init.cs with all the other exec commanda like exec("MasterServerClient.cs");)

So lets test it. Put this

exec("./MasterServerClient.cs");

//This test will summon an assortment pof creatures. The purpose is to have a place to test performance
function testMasterServer() {
   initializeMasterServerClient("localhost", 9100);
   schedule(10000, 0, testMasterServer2);	
}

function testMasterServer2() {
   MSClient.registerGame("MYGAME:UNIQUEID1", "localhost", 1234, "Cruel Intentions");
   MSClient.registerGame("MYGAME:UNIQUEID2", "192.168.1.1", 5678, "Prison Break");
   MSClient.registerGame("MYGAME:UNIQUEID3", "yourserver.ontheweb.com", 9012, "My little pony");
   
   MSClient.listGames();

   MSClient.unregisterGame("MYGAME:UNIQUEID2");

   MSClient.listGames();
   
}

The first function will connect to the localhost (if you setup the computer for internet use up there you should connect using the public name (mygameserver.dyndns.org) and of course the IP port which in this case is 9100. Then we wait for some seconds for the connection to be stablished. When it does you should see the in your Torque console the connection message "Connection established with MasterServer" and in the Eclipse console the appropriate connection.

Then after some 10 seconds the program will send some messages to the server. Now, remember all web communication is asynchronous so you really don't know when the results will come back. You should handle all the results by using that sample script, check the onLine function.


<b>So what now?</b>
Well, you have now a mechanism to share your game. When Torque starts, connect to the master server. When your game is created send the game info with the current IP and port to the master server.
When players start their games, connect to the master server. Then at any time, request the list of games. You will get the IPs and ports so the clients know where to connect.

You are not expecting me to integrate this to your game right?

<b>Why Java? Why not pure Torque</b>
Because Torque is designed for fast execution and will take all the resources in your computer. You can slowdown Torque but will require more explanations about the engine. This solution is simple enough.
Because Java network is well behaved. It will sleep while waiting for messages so minimal resources are used in your server.


<b>Improvements</b>
When a client disconnects, any game registered by that player should be removed from the list.
Adding callback support so that no changes are required in the MasterServerClient.cs script.
More security maybe, DB based authorization?


Nothing else. If you need support then talk to the guy on your left, right, or front (just joking). But seriously, you are programmers so learn to read code and solve your own problems.

Luck!

#1
07/14/2011 (6:18 am)
Very nice, a new master server to fight with. I'll have to give this a shot after I get our game servers back up and running. Thank you!
#2
07/14/2011 (5:38 pm)
Thanks Guimo for taking the time to do it.
#3
07/15/2011 (4:02 am)
Ty Guimo. I think i was the crie man :) sry for this. And ty much for the recource and your time spend. We allways can use things like that !
#4
07/15/2011 (6:19 pm)
Thanks Guimo, this looks like a good solution and Java which is good :-) thanks!
#5
07/16/2011 (1:23 pm)
Nice script, but I have my own setup :).

I interface server IP's through a list on my web server (MYSQL/PHP), and have the servers ping updates every 3 minutes. If no new update has been received, the server is removed from the list.

Then when a client needs the list, I just use PHP to output the IP list.

Easy, efficient, and no expensive server necessary :P
#6
07/16/2011 (2:50 pm)
@Robert,

You can setup a resource for your process as well. It actually would work out for my next game. I just find it a tad tacky to put down another's resource when they took the time to share their knowledge and you didn't. People are always asking for Master Server processes so the more the merrier.
#7
09/26/2012 (1:15 am)
RE: "You are not expecting me to integrate this to your game right?"

Awe come on... would you? *grin*

Thanks for the resource. Checking it out now.