Implementing a simple master server
by Guimo · 07/13/2011 (11:06 pm) · 5 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
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!
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!
#2
Just execute the Perl script on the server, it will wait for connections. Change your masterserver pref in your game distribution to the address of your web server, use the default port, and away you go. Your game will connect to your web server when people click "query master", and it will return a list of what games are running.
Works great, and no programming required.
Can get a bit slow after being up for a few days, if you have a lot of people connecting to it, since it doesn't seem to time out the connections... Thus people requesting a list of running games will have to wait while it queries every IP that has hit it since it was launched, most of which will not be there any more. But I just scheduled a simple batch file that recycles the thing every night - that keeps the response nice and snappy with only a few IPs to check.
I posted a copy on our website if anyone wants it:
http://gambitrealm.com/installer/c3masterserver.zip
(Author: Thomas Lund, thomas@trylleskoven.dk, www.c3command.com)
07/15/2011 (10:56 am)
We use this simple one we found online after trying a few available ones, it's a Perl script. Of course you need a web server somewhere to run it on that has support for Perl, but that's all.Just execute the Perl script on the server, it will wait for connections. Change your masterserver pref in your game distribution to the address of your web server, use the default port, and away you go. Your game will connect to your web server when people click "query master", and it will return a list of what games are running.
Works great, and no programming required.
Can get a bit slow after being up for a few days, if you have a lot of people connecting to it, since it doesn't seem to time out the connections... Thus people requesting a list of running games will have to wait while it queries every IP that has hit it since it was launched, most of which will not be there any more. But I just scheduled a simple batch file that recycles the thing every night - that keeps the response nice and snappy with only a few IPs to check.
I posted a copy on our website if anyone wants it:
http://gambitrealm.com/installer/c3masterserver.zip
(Author: Thomas Lund, thomas@trylleskoven.dk, www.c3command.com)
#3
07/16/2011 (3:18 am)
@ Paul, this looks also very interesing to me. Ty a lot , to Paul and Guimo. I Think both resources will help me much.
#4
Guimo, I'm sure your work is awesome, but in addition, Thomas Lund's works right out of the box.
Randel Reiss
Director Product Development
Prairie Games, Inc.
mailto:RandelR@PrairieGames.com
07/16/2011 (4:01 pm)
I too used the c3masterserver one when I first built vSide.Guimo, I'm sure your work is awesome, but in addition, Thomas Lund's works right out of the box.
Randel Reiss
Director Product Development
Prairie Games, Inc.
mailto:RandelR@PrairieGames.com
#5
I know this may be a dumb question, but what about hardware?
What type of computer should we use for a master server with multiple accounts?
What is needed?
How much RAM?
How much Memory?
Processor?
Hard drive space?
Dynamic IP or Static IP?
Cable or DSL?
Operating system? I usually use Windows7, is this OK?
The following cost almost $5,000.00. Is that too much?
PowerEdge T610
Tower Chassis for Up to 8, 2.5" Hard Drives
Primary Processor Intel® Xeon® E5645 2.40GHz, 12M Cache, 5.86 GT/s QPI, 6C
Memory 24GB Memory (6x4GB), 1333MHz Dual Ranked LV RDIMMs for 2 Procs, Optimized
Additional Processor Intel® Xeon® E5645 2.40GHz, 12M Cache, 5.86 GT/s QPI, 6C
Operating System No Operating System
Enterprise Software Licensing None
Optional Virtualization Offerings None
Secondary OS None
OS Media kits None
Enabled Virtualization None
Internal Controller SAS 6/iR Integrated
Hard Drive Configuration No RAID for H200 or SAS 6/iR Controllers
Hard Drives 146GB 10K RPM Serial-Attach SCSI 6Gbps 2.5in Hotplug Hard Drive
External Controller None
Power Supply High Output Power Supply, Redundant, 870W
Power Cords NEMA 5-15P to C13 Wall Plug, 125 Volt, 15 AMP, 10 Feet (3m), Power Cord
Power Cords 2x NEMA 5-15P to C13 Wall Plug, 125 Volt, 15 AMP, 10 Feet (3m), Power Cord
OS Partitions None
SYSTEMS MANAGEMENT OPTIONS
Embedded Management iDRAC6 Express
NETWORKING OPTIONS
Network Adapter Broadcom 5709 Dual Port 1GbE NIC w/TOE iSCSI, PCIe-4 STORAGE BACKUP OPTIONS
Host Bus Adapater/Converged Network Adapter None
SOFTWARE OPTIONS
Microsoft SQL Server None
Client Access Licenses None
OTHER OPTIONS
Rails Tower Chassis, No Rails Required
Internal Optical Drive DVD-ROM, SATA, Internal
Server Accessories None
System Documentation Electronic System Documentation and OpenManage DVD Kit
Optional Documentation None
Removable Disks and Tape Drives None
Tape Backup Software None
Systems Management Consoles and Licenses None
Media None edit
OTHER OPTIONS
Feature Upgrades for Embedded NIC Ports Embedded NICs are TOE Ready with iSCSI Offload Enabled
BIOS Setting Power Saving BIOS Setting
Cooling None
Systems Management Upgrades None
Flexible Client Solutions None
Tape Media None
Energy Star None
Uninterruptible Power Supplies and Accessories None
Asset Tag on System Chassis (CFI) None
SERVICES
Hardware Support Services 3 Year ProSupport and NBD On-site Service
Installation Services No Installation
Open Manage Subscription None
Proactive Maintenance Maintenance Declined
Data Protection Offers None
Dell Recycling None
Remote Advisory and Onsite services None
Should I even post this here?
Any help would be greatly appreciated.
01/04/2012 (1:08 am)
Hi guys,I know this may be a dumb question, but what about hardware?
What type of computer should we use for a master server with multiple accounts?
What is needed?
How much RAM?
How much Memory?
Processor?
Hard drive space?
Dynamic IP or Static IP?
Cable or DSL?
Operating system? I usually use Windows7, is this OK?
The following cost almost $5,000.00. Is that too much?
PowerEdge T610
Tower Chassis for Up to 8, 2.5" Hard Drives
Primary Processor Intel® Xeon® E5645 2.40GHz, 12M Cache, 5.86 GT/s QPI, 6C
Memory 24GB Memory (6x4GB), 1333MHz Dual Ranked LV RDIMMs for 2 Procs, Optimized
Additional Processor Intel® Xeon® E5645 2.40GHz, 12M Cache, 5.86 GT/s QPI, 6C
Operating System No Operating System
Enterprise Software Licensing None
Optional Virtualization Offerings None
Secondary OS None
OS Media kits None
Enabled Virtualization None
Internal Controller SAS 6/iR Integrated
Hard Drive Configuration No RAID for H200 or SAS 6/iR Controllers
Hard Drives 146GB 10K RPM Serial-Attach SCSI 6Gbps 2.5in Hotplug Hard Drive
External Controller None
Power Supply High Output Power Supply, Redundant, 870W
Power Cords NEMA 5-15P to C13 Wall Plug, 125 Volt, 15 AMP, 10 Feet (3m), Power Cord
Power Cords 2x NEMA 5-15P to C13 Wall Plug, 125 Volt, 15 AMP, 10 Feet (3m), Power Cord
OS Partitions None
SYSTEMS MANAGEMENT OPTIONS
Embedded Management iDRAC6 Express
NETWORKING OPTIONS
Network Adapter Broadcom 5709 Dual Port 1GbE NIC w/TOE iSCSI, PCIe-4 STORAGE BACKUP OPTIONS
Host Bus Adapater/Converged Network Adapter None
SOFTWARE OPTIONS
Microsoft SQL Server None
Client Access Licenses None
OTHER OPTIONS
Rails Tower Chassis, No Rails Required
Internal Optical Drive DVD-ROM, SATA, Internal
Server Accessories None
System Documentation Electronic System Documentation and OpenManage DVD Kit
Optional Documentation None
Removable Disks and Tape Drives None
Tape Backup Software None
Systems Management Consoles and Licenses None
Media None edit
OTHER OPTIONS
Feature Upgrades for Embedded NIC Ports Embedded NICs are TOE Ready with iSCSI Offload Enabled
BIOS Setting Power Saving BIOS Setting
Cooling None
Systems Management Upgrades None
Flexible Client Solutions None
Tape Media None
Energy Star None
Uninterruptible Power Supplies and Accessories None
Asset Tag on System Chassis (CFI) None
SERVICES
Hardware Support Services 3 Year ProSupport and NBD On-site Service
Installation Services No Installation
Open Manage Subscription None
Proactive Maintenance Maintenance Declined
Data Protection Offers None
Dell Recycling None
Remote Advisory and Onsite services None
Should I even post this here?
Any help would be greatly appreciated.

Torque Owner Richard Ranft
Roostertail Games