I set up a TPC Lobby on an instance at Digital Ocean recently and thought this little tutorial might help someone trying to do the same. If you are going to check out digital ocean please use this referral link (
https://www.digitalocean.com/?refcode=234d5b709fd7) if my little tutorial was useful.
For my game I need a Lobby Server somewhere in the cloud. Players will either start a game server, which registers with the Lobby, or start as a client looking for a match to join.
I'll explain these steps assuming you use Digital Ocean (DO) but with some variation you could apply the same info on a server running on AWS. There is probably a much cleaner way to do what I am about to do but brute forcing it works for me and my simple lobby needs.
I want my Lobby to run as a daemon (service) in Linux. It will auto start when the server is started. For this to work the area where the TNServer.exe waits for user input should be removed. Wile I'm at it, I'm going to change the default args too so I do not have to pass that along to the exe either. Look in your project folder and find `\Assets\TNet\TNetServer.zip`. Extract is somewhere cause you need to modify it. Open the solution and the file ServerMain.cs.
My first change is to comment out lines 30-45 and simply add the one line that forces args to be what I need.
static int Main (string[] args)
{
//if (args == null || args.Length == 0)
//{
// Console.WriteLine("No arguments specified, assuming default values.");
// Console.WriteLine("In the future you can specify your own ports like so:\n");
// Console.WriteLine(" -name \"Your Server\" <-- Name your server");
// Console.WriteLine(" -tcp [port] <-- TCP port for clients to connect to");
// Console.WriteLine(" -udp [port] <-- UDP port used for communication");
// Console.WriteLine(" -udpLobby [address] [port] <-- Start or connect to a UDP lobby");
// Console.WriteLine(" -tcpLobby [address] [port] <-- Start or connect to a TCP lobby");
// Console.WriteLine(" -ip [ip] <-- Choose a specific network interface");
// Console.WriteLine("\nFor example:");
// Console.WriteLine(" TNServer -name \"My Server\" -tcp 5127 -udp 5128 -udpLobby 5129");
// //args = new string[] { "TNet Server", "-tcp", "5127", "-udp", "5128", "-tcpLobby", "5129" };
// args = new string[] { "Lobby Server", "-tcpLobby", "5129" };
//}
args
= new string[] { "Lobby Server",
"-tcpLobby",
"5129" };
Next I comment out lines starting at the "for (; ; )" and add (or replace it with) a Thread Sleep.
// Open up ports on the router / gateway
if (up != null)
{
if (tcpPort > 0) up.OpenTCP(tcpPort, OnPortOpened);
if (udpPort > 0) up.OpenUDP(udpPort, OnPortOpened);
}
for (; ; ){Thread.Sleep(2000);}
//for (; ; )
//{
// Console.WriteLine("Press 'q' followed by ENTER when you want to quit.\n");
// string command = Console.ReadLine();
// if (command == "q") break;
//}
//Console.WriteLine("Shutting down...");
//// Close all opened ports
//if (up != null)
//{
// up.Close();
// up.WaitForThreads();
// up = null;
//}
//// Stop the game server
//if (gameServer != null)
//{
// gameServer.SaveTo("server.dat");
// gameServer.Stop();
// gameServer = null;
//}
//// Stop the lobby server
//if (lobbyServer != null)
//{
// lobbyServer.Stop();
// lobbyServer = null;
//}
}
//Console.WriteLine("The server has shut down. Press ENTER to terminate the application.");
//Console.ReadLine();
}
Now recompile and find the new exe in "path\something\bin\Release".
You will also need a bash script that can start/ stop the server. You can call this file "gamelobby".
#!/bin/sh
#
APP_NAME="gamelobby"
APP_USER=root
case "$1" in
start)
echo "Starting $APP_NAME"
start-stop-daemon --start \
--background \
--make-pidfile \
--pidfile /var/run/$APP_NAME.pid \
--chuid $APP_USER \
--exec /usr/bin/mono -- /root/mygame/TNServer.exe
;;
stop)
echo "Stopping $APP_NAME"
start-stop-daemon -o --stop \
--pidfile /var/run/$APP_NAME.pid
;;
*)
echo "Usage: /root/mygame/$APP_NAME {start|stop}"
exit 1
;;
esac
exit 0
Now for the server instance.
I created the smallest droplet (instance) on DO, the $5 per month one and chose an Ubuntu 14.10 x64 image.
After it is done being created I will use putty (
http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html) to access it (I'm using Windows). Putty is a Telnet and SSH client which you use to login to the remote server and is presented with a console where you can then type commands.
* When you start putty.exe you will see a window where you can enter a Host Name (enter the IP address emailed to you after created the server instance).
* Make sure connection type is set to SSH
* Click on SSH in the left-hand menu and choose "2 only"
* Click on Session in the left-hand menu and enter a name for it and click on "Save" so you can access this in future
* Now double click on the session in the list and a black window should open. This is the console.
You should now see "Login as:". Type "root" and press enter. Next you should see something like "root@ip_address password". Here you enter the password that was mailed to you. Note that while you type nothing will be shown. After you are done entering the password, press Enter. You can also copy-paste the password by copying it and then right-clicking in that black windows to "paste". Press enter after. If all went well you should now see a prompt to enter a new password. Do this and we can go on with the next steps.
You now need to install Mono. Type the following and press Enter. Choose Yes when it asks to continue with something.
apt-get install mono-complete
The server should now be ready for the lobby software. Get FileZilla client (
https://filezilla-project.org/download.php?type=client). It is an FTP client that will be used to upload the lobby and some other files.
Run FileZilla and go to menu: "File > Site Manager".
Click on "new site".
* In Host enter the IP address if your server
* You can leave Port empty
* Protocol: SFTP - SSH File Transfer Protocol
* Logon Type: Normal
* User: root
* Password: your password
Click Connect. In the right-hand panel you see the server's folders and files and on the left-hand side your local machine's folders and files.
Create a new folder on the server (right-click in right-hand panel and choose `craete directory`) and name it "mygame". Enter this new folder/ directory.
In the left-hand panel, navigate to TNServer.exe; right-click on it and choose "upload". Then go to where you created the bash script named "gamelobby" and upload that too. You should now see "gamelobby" and "TServer.exe" in the right-hand panel.
Back to putty (console).
Type and press Enter.
The line-endings of the bash script must now be fixed else they will cause errors.
Type and press Enter. An editor now opens the file.
Type and press Enter. This changes the file to the correct format.
Type and press Enter. This exits Vim
Now the file permissions needs to be set, just in case they are not right.
Type and press Enter, to make the script executable
Type and press Enter to set root as owner
chown root:root gamelobby
Now to set it to auto start the lobby server when the server is started/ rebooted.
Type and press Enter. This will install an editor
Type and press Enter. This will open the file in an editor
* Go to the line just before "exit 0" (with your arrow keys) and enter "/root/mygame/gamelobby start"
* Press Ctrl+O and press Enter (to save your changes)
* Press Ctrl+X (to exit the editor)
You are done, restart the server.
Type and press Enter.
This will reboot the server and putty will now loose its connection, so close the putty console window.