Game Server

From Documentation

Jump to: navigation, search

Created by BarkerJr

Game Server is a library that provides querying and parsing of server requests and returns them as JavaBean-like objects, easily convertible to JDOM/XML or JSON.

Contents

Server Classes

To get a representation of a server of a specific game, you need to know the class. For a list of engines supported, see the list.

GameClass
Age of ChivalryOrangeBoxServer
America's ArmyUnrealEngine2Server
Battlefield: Bad Company 2BattlefieldBadCompany2Server
Battlefield 1942Battlefield1942Server
Counterstrike: SourceOrangeBoxServer
Day of Defeat: SourceOrangeBoxServer
Garry's ModLeft4DeadServer
Half-Life 1GoldSourceServer
Half-Life 2SourceServer
Killing FloorUnrealEngine2Server
Left 4 Dead 1Left4DeadServer
Left 4 Dead 2Left4DeadServer
SynergyOrangeBoxServer
Team Fortress 2Left4DeadServer
UnrealUnrealEngine1Server
Unreal TournamentUnrealEngine1Server
Unreal Tournament 2003UnrealEngine2Server
Unreal Tournament 2004UnrealEngine2Server
Zombie Panic! SourceOrangeBoxServer

Tips

Invoke the close() Method

The servers based on UdpServer are cached internally. To retrieve the cached copy of the server, use the getInstance() method. To replace the cached version with a new instance, use the new keyword. To remove the instance from cache, invoke the close() method.

The Battlefield: Bad Company 2 servers are based on TCP. They will keep their connection to the server open until the close() method is called.

Use Asynchronous Methods

If you need to do multiple queries on the same or multiple servers, it's good to do so asynchronously. By adding a listener and invoking methods that do not contain timeout parameters, you can send a query which will return immediately. Your listener will be called when your queries are complete.

Examples

These examples are shown with no error handling for simplicity.

Error Handling

SourceServer server = SourceServer.getInstance(new InetSocketAddress("127.0.0.2", 27015));
server.addListener(new Listener () {
	@Override
	public void errorHandler(Throwable error, GameServer server) {
		System.err.println("Error querying: " + server);
		error.printStackTrace();
	}
});

Query

InetSocketAddress addr = new InetSocketAddress("127.0.0.2", 27015);
SourceServer server = SourceServer.getInstance(addr);
try {
	server.load(2000, Request.INFORMATION, Request.PLAYERS, Request.RULES);
} finally {
	server.close();
}

Server List

SourceServerList list = new SourceServerList();
list.gameDir = "synergy";
HashSet<SourceServer> serverList = new HashSet<SourceServer>();
ValveServerList<SourceServer>.ServerIterator servers = list.iterator(10000);
try {
	while (servers.hasNext()) {
		SourceServer server = servers.next();
		serverList.add(server);
		server.addListener(listener);
		server.load(Request.INFORMATION);
	}
} finally {
	servers.close();
}
Thread.sleep(2000);
for (SourceServer server: serverList) {
	System.out.println(server);
}

To Do List

Feel free to add to this list or program some of the items and send me your changes.

  • Add support for gametype: examples package
  • Redesign RCON support: package
  • Properly extend Player class for specific games, rather than putting all the properties in the main Player class: class
  • Support Stats ID in Unreal Engine 2 servers: class
  • Verify length and checksum of bziped replies: class
  • Replicate SourceServerList to support Left 4 Dead and Orange Box

See Also