Battlefield Heroes Rcon .Net library

By | 2013.09.05

What is BFH?

Battlefield Heroes is a free-to-play first person cartoon shooter. 8×8 players battle it out on different maps using traditional weapons and special abilities.

My server

http://www.battlefieldheroes.com/en/bookmark/server:0104ea2f-1c0f-42a1-b05e-e19e33a6f4ba

You need to bookmark, start the game, select “Game finder”, select “Bookmarked” and locate the server to join it.

Background

For the past few years I have been playing Battlefield Heroes on and off. Nearly a month ago I decided to drop my Minecraft server and get a BFH server instead. As it turned out you can’t run a server yourself, but you can rent one online. It comes with some basic capabilities, but to get a more “complete” server you have to run an application on another machine. This application will connect to your server through a special Rcon admin protocol and provide user and server administration as well as functionality such as in-game commands and messages.

I went with AutoAdmin which seemed to be an ok and easy to use admin interface.

image

But of course, as usual, it didn’t take long before I wanted to play around with this myself. I’m not SO fond of games, but I love coding.

The source code

Although AutoAdmin is written in .Net I could not find any .Net libraries for the Rcon protocol. So I researched the Rcon protocol and started writing a library for it (in C#). Although writing protocol implementations has always been a passion of mine it has been a while since I had the opportunity to implementing a communication protocol. Last time I think was when I held a workshop on writing your own HTTP server. Implementing the Rcon protocol was great fun, and I found that async/await was awesome for this kind of protocol implementation.

I have released the source code on GitHub and set the license to LGPL v3. I normally choose BSD-license for my open source projects, but since I’m not sure if I will finish this and there are no other libraries out there I want any improvements to the lib to be made public so others can benefit from it.

https://github.com/KernelNO/BFH-Rcon-Admin

Using the code

You need to modify Config.xml with your server ip, port and admin-password. Then have a look at Kernel.BFHAdmin.Client.GUI project for an example of how to use.

Model properties use INotifyPropertyChanged so they are directly compatible with WPF databinding.

Your main entry point is “RconClient”, see example under “Kernel.BFHAdmin.Client.Gui” file “RconModel.cs”.

Automated commands

An internal timer is polling things like player list and server info at regular intervals to keep internal memory models updated. I chose to let both command implementation and updated memory model be confined to a single module, so under the project “Kernel.BDHAdmin.Client.BRHRconProtocol” there is a folder named “Commands” that contains modules responsible for their own operation. For example “PlayerListCommand.cs” has a RefrehsPlayerList() method (autorun by timer) that will send request for player list, wait for the response, parse response, set model and fire off events accordingly (player joined, left, changed). All of the command modules use models that use INotifyPropertyChange, so you can both programatically listen for change to a specifi property or databind XAML: <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding RconClient.ServerInfoCommand.ServerInfo.ServerName}" />

Custom commands

SimpleCommand.cs is a generic command implementation. It allows you to send asynchronous commands and optionally async await for response. It is suited for both direct use and as a baseclass for more complex commands. An instance of SimpleCommand is provided as RconClient.Command.

Example:
RconClient.Command.Exec("mm saveConfig");

or if you need the reply:
List<string> lines = await RconClient.Command.Exec("mm saveConfig");

Rcon protocol implementation

The Rcon protocol is basically a text-based request-response protocol. You send a command, read the reply (ie. lines of tab-separated data) and you are ready for next command.

Since the reply for every command varies and there is no “End of reply” signal I added an invalid command at the end of every command. In short I send “done11done” after every command, the server will then reply with response for first command, then with an error that “done11done” is not a command. I use the error as a “End of reply” signal.

Commands are queued internally, and each command has a waiting Task. Queued commands are processed on a separate thread and sent in sequence to the server, response is read, when “End of reply” signal is received the list of lines read is returnet to the waiting Task. This allows for use of async-await when communicating with server. Basically allowing asynchronous communicaiton with synchronous programming, meaning no pesky callbacks. Oh, and command queueing is thread safe.

Modules

Once you start up RconClient (the library itself) it will scan current folder for files named *.module.*.dll and load these. It will then scan for classes using interface IAmModule and instanciate+register them. In other words: plugin support.

Check out “Kernel.BFHAdmin.Module.DefaultScript” for some sample modules.

Screenshots

Some screenshots (7th September 2013).

image image image image image

Want to help?

I have no particular plans for this library. However… If you have some .Net (C# preferrably) skills and want to assist/add/test something please feel free to contact me.

Leave a Reply