Code archives/Networking/GamePool

This code has been declared by its author to be Public Domain code.

Download source code

GamePool by JoshK2008
The purpose of this code is to retrieve a list of available server IP addresses which can then be used with whichever networking routines you prefer.
SuperStrict

Import brl.stream
Import brl.socket
Import brl.httpstream
Import brl.linkedlist

Type TGamePoolServer
	
	Const LOCALIP:Int=2130706433
	
	Field ip:Int=LOCALIP
	Field url$
	Field game$
	Field name$

	Method Delete()
		Local stream:TStream
		If url
			stream=OpenStream(url+"?action=removeserver")
			If stream stream.close()
		EndIf
	EndMethod
	
	Method Refresh:Int()
		Local stream:TStream
		stream=OpenStream(url+"?action=refreshserver")
		If Not stream Return False
		stream.close()
		Return True	
	EndMethod
	
	Function Create:TGamePoolServer(url$,name$="",game$="")
		Local server:TGamePoolServer=New TGamePoolServer
		Local stream:TStream
		server.url=url
		server.name=name
		server.game=game
		server.url=url
		stream=OpenStream(url+"?action=addserver&gamename="+game+"&servername="+name)
		If Not stream Return Null
		While Not stream.Eof()
			If ReadLine(stream).Trim()
				stream.close()
				Return Null
			EndIf
		Wend
		If stream stream.close()
		Return server
	EndFunction
	
	Function Get:TGamePoolServer[](url$,game$="")
		Local descarray:TGamePoolServer[]
		Local list:TList
		Local stream:TStream
		Local s$,sarr$[],n:Int
		Local server:TGamePoolServer
		stream=OpenStream(url+"?action=listservers&gamename="+game)
		If Not stream Return Null
		list=New TList
		While Not stream.Eof()
			s=ReadLine(stream)
			sarr=s.split("|")
			If sarr.length=2
				list.addfirst(s)
			EndIf
		Wend
		stream.close()
		If list.IsEmpty() Return Null
		descarray=New TGamePoolServer[list.count()]
		For s=EachIn list
			sarr=s.split("|")
			server=New TGamePoolServer
			server.game=game
			server.ip=HostIp(sarr[0])
			server.name=sarr[1]
			descarray[n]=server
			n:+1
		Next
		Return descarray
	EndFunction
	
EndType

Function CreateGamePoolServer:TGamePoolServer(url$,name$="",game$="")
	Return TGamePoolServer.Create(url$,name$,game)
EndFunction

Function RefreshGamePoolServer(server:TGamePoolServer)
	server.Refresh()
EndFunction

Function GetGamePoolServers:TGamePoolServer[](url$,game$="")
	Return TGamePoolServer.Get(url,game)
EndFunction

Function GetGamePoolServerIP:Int(server:TGamePoolServer)
	Return server.ip
EndFunction

Function GetGamePoolServerName:String(server:TGamePoolServer)
	Return server.name
EndFunction

Function GetGamePoolServerGame:String(server:TGamePoolServer)
	Return server.game
EndFunction

Comments

JoshK2008
Here is the php script (Thanks to "Helios" for this):
<?

	// Master Server
	// Copyright 2008 Binary Phoenix

	// This script is pretty simple, it allows you to pass an argument to this file that
	// specifies what statistic you want. Normally this will be a request to get all
	// servers running for a given game.
	
	// MySql DB Structure;
	//
	//  - servers (table)
	//		- ID, int, auto_increment, primary key
	//		- IP, tinytext
	//		- Name, text
	//		- TimeoutTimer, int
	//		- GameName, text
	
	// Arguments;
	//
	//		- action : This specifies the action to preform, it can be any of the following;
	//			- listservers  :  This will cause the script to emit a list of currently
	//							 running servers. This must be used with the gameid argument
	//							 to specify what game the servers must be running.
	//			- addserver    : This will add the current ip to the server list. This must be 
	//							 used with the gameid argument to specify what game the server is be running.
	//			- removeserver : This will remove the current ip from the server list. 
	//			- refreshserver: Resets the servers timeout timer so that its not removed from
	//							 the server list.
	//
	//		- gamename 		: This is used by several actions to decide what game the servers it 
	//				   		  is dealing with should be running.
	//		- servername 	: This is used when adding a server to the list. It just contains
	//					   	  a name based description of the server.
	//		- ip			: Returns the current ip address.
	//			
	
	// Errors;
	//	
	//		The server can return certain error strings depending on the situation, what follows
	//		is a list of them;
	//
	//		- "error 1" : This is a mysql error. Its usually temporary.
	//		- "error 2" : Argument missing. This occurs when the request url dosen't 
	//					  contain an required argument.

	// This variable stores how long it takes for a server to timeout.
	$timeoutDelay = 60; // 1 minutes.
	
	// Connect to the database.
	$connection = mysql_connect("localhost", "InsertDataBaseUsername", "InsertDataBasePassword") or die("error 1");
	mysql_select_db("InsertDataBaseName") or die("error 1");

	// Make sure we have been given an action.
	if (isset($_GET['action']))
	{
		// Process the action.
		switch ($_GET['action'])
		{
			case 'ip':
			
				die($_SERVER['REMOTE_ADDR']);
				break;

		
			case 'listservers':
				
				// Check correct arguments have been passed.
				if (!isset($_GET['gamename'])) die("error 2");
				
				// Get required arguemnts and clean them up.
				$gamename = $_GET['gamename'];
			
				// Kill off any servers that haven't refreshed themselfs in a long time.
				mysql_query("DELETE FROM servers WHERE TimeoutTimer <= " . time());
				
				// Grab server list.
				$result = mysql_query("SELECT * FROM servers WHERE GameName='" . mysql_escape_string($gamename) . "'");
				
				if ($result)
				{
					// Emit all the servers.
					while ($row = mysql_fetch_assoc($result)) 
					{
						echo $row['IP'] . '|' . $row['Name'] . "\n";
					}
				}
				
				break;
				
			case 'addserver':
				
				// Check correct arguments have been passed.
				if (!isset($_GET['gamename'])) die("error 2");
				if (!isset($_GET['servername'])) die("error 2");
				
				// Get required arguemnts and clean them up.
				$gamename = $_GET['gamename'];
				$serverName = $_GET['servername'];
				
				// Remove any old servers with the same ip and name.
				mysql_query("DELETE FROM servers WHERE IP='" . mysql_escape_string($_SERVER['REMOTE_ADDR']) . "' and Name='" . mysql_escape_string($serverName) ."' and GameName='" . mysql_escape_string($gamename) . "'");
				
				// Insert it into the database.
				mysql_query("INSERT INTO servers(IP, Name, TimeoutTimer, GameName) VALUES('" . mysql_escape_string($_SERVER['REMOTE_ADDR']) . "','" . mysql_escape_string($serverName) . "'," . (time() + $timeoutDelay) . ",'" . mysql_escape_string($gamename) . "')");

				break;
				
			case 'removeserver':
			
				// Remove this server based on its ip.
				mysql_query("DELETE FROM servers WHERE IP='" . mysql_escape_string($_SERVER['REMOTE_ADDR']) . "'");
			
				break;

			case 'refreshserver':
			
				// Refersh the servers timeout timer.
				mysql_query("UPDATE servers SET TimeoutTimer='" . (time() + $timeoutDelay) . "' WHERE IP='" . mysql_escape_string($_SERVER['REMOTE_ADDR']) . "'");
			
				break;
		}
	}
	else
	{
		die("error 2");
	}

	// Close database connection.
	mysql_close($connection);

?>



JoshK2008
Usage:

SuperStrict

Framework brl.standardio
Import "gamepool.bmx"

Local server:TGamePoolServer[]
Local n:Int

Local server1:TGamePoolServer=CreateGamePoolServer("http::www.leadwerks.com/gameservers/gameservers.php","My_Server_1","My_Game")
Local server2:TGamePoolServer=CreateGamePoolServer("http::www.leadwerks.com/gameservers/gameservers.php","My_Server_2","My_Game")
Local server3:TGamePoolServer=CreateGamePoolServer("http::www.leadwerks.com/gameservers/gameservers.php","My_Server_3","My_Game")

server=GetGamePoolServers("http::www.leadwerks.com/gameservers/gameservers.php","My_Game")
If server
	For n=0 To server.length-1
		Print server[n].name+", "+DottedIP(server[n].ip)
	Next
EndIf

'Keep the variables in scope so they don't get deleted:
If server1 End
If server2 End
If server3 End


More info here:
http://blitzmax.com/Community/posts.php?topic=77140#863164


AJ002002009
How do you connect to the public server to host your game?


Code Archives Forum