Building the Master Server/Facilitator on your own
Reference Manual > Network Reference Guide > Master Server > Building the Master Server/Facilitator on your own

Building the Master Server/Facilitator on your own

The source code to the Master Server and Facilitator can be downloaded from the Unity3d website. This is a single project which implement both the master server functionality and the facilitator. It can be seen from the source file how these two things are separate and its easy to split them into two projects if that is desired (to put them on different machines maybe).

The package includes the RakNet 3.0 networking library which does all the basic networking functions. It also includes the NAT punchthrough plugin used by the Facilitator and the LightweightDatabase plugin used by the Master Server. The package consists of the MasterServer.cpp/.h source files which does some simple networking setup all the rest is done in the RakNet source files. The punchthrough plugin is unmodified but the LightweightDatabase has been heavily modified to do everything we need it to do.

The package include three different types of project files, ready for compilation:

The Xcode and Visual Studio projects can just be opened, compiled and built. To build with the Makefile just run "make". It should work with a standard compilation setup on Linux and Mac OS X, if you have gcc then it should work.

The source code for the Master Server can be downloaded from the Unity Master Server Download page


The Master Server

The Master Server uses an internal database structure to keep track of host information.

Hosts send messages with the RUM_UPDATE_OR_ADD_ROW message identifier and all their host information embedded. This is processed in the OnReceive() function in the LightweightDatabaseServer.cpp file. This is where all message initially appear and therefore a good place to start if you want to trace how a message is processed. A table is created within the database structure for each game type which is set when you use MasterServer.RegisterHost function. All game types are grouped together in a table, if the table does not exist it is dynamically created in the CreateDefaultTable() function.

The host information data is modified by the master server. The IP and port of the game which is registering, as seen by the master server, is injected into the host data. This way we can for sure detect the correct external IP and port in cases where the host has a private address (NAT address). The IP and port in the host data sent by the game server is the private address and port and this is stored for later use. If the master server detects that a client is requesting the host data for a game server and the server has the same IP address then he uses the private address of the server instead of the external one. This is to handle cases where the client and server are on the same local network, using the same router with NAT addresses. Thus they will have the same external address and cannot connect to each other through it, they need to use the private addresses and those will work in this case.

Clients send messages with the ID_DATABASE_QUERY_REQUEST message identifier and what game type they are looking for. The table or host list is fetched from the database structure and sent to the client. If it isn't found and empty host list is sent.

All messages sent to the master server must contain version information which is checked in the CheckVersion() function. At the moment each version of Unity will set a new master server version internally and this is checked here. So if the master server communication routine will change at any point it will be able to detect older versions here and possibly refer to another version of the master server (if at all needed) or modify the processing of that message to account for differences.

The Facilitator

The facilitator uses the NAT punchthrough plugin from RakNet directly with no modifications. This is essentially just a peer listening on a port with the NAT punchthrough plugin loaded. When a server and a client with NAT addresses are both connected to this peer, they will be able to perform NAT punchthrough to connect to each other. When the Network.useNat property is enabled the connection is set up automatically for you.