Game Server
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.
| Game | Class |
|---|---|
| Age of Chivalry | OrangeBoxServer |
| America's Army | UnrealEngine2Server |
| Battlefield: Bad Company 2 | BattlefieldBadCompany2Server |
| Battlefield 1942 | Battlefield1942Server |
| Counterstrike: Source | OrangeBoxServer |
| Day of Defeat: Source | OrangeBoxServer |
| Garry's Mod | Left4DeadServer |
| Half-Life 1 | GoldSourceServer |
| Half-Life 2 | SourceServer |
| Killing Floor | UnrealEngine2Server |
| Left 4 Dead 1 | Left4DeadServer |
| Left 4 Dead 2 | Left4DeadServer |
| Synergy | OrangeBoxServer |
| Team Fortress 2 | Left4DeadServer |
| Unreal | UnrealEngine1Server |
| Unreal Tournament | UnrealEngine1Server |
| Unreal Tournament 2003 | UnrealEngine2Server |
| Unreal Tournament 2004 | UnrealEngine2Server |
| Zombie Panic! Source | OrangeBoxServer |
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
- Remove ServerSpy support