Client/Server Communication Data Formats

by Cameron Albert 5. January 2009 23:41

While developing Perenthia I tried several different communication formats from sending JSON serialized objects back and forth to sending byte arrays containing mostly integers indicating what should be loaded and displayed, what commands to execute etc. What I eventually settled on was a bit of a hybrid. I've created a simple set of "tags" that can be sent to and from the server. The tags are nothing more than pipe delimited strings contained within curly braces and there are a limited number of tags that provide simplicity yet flexibility with the data that is transmitted. The tags are represented in C# by tag objects allow me to create them, query them, etc.

A simple command tag for the SAY command might look like this: {CMD|SAY|"Hello World!"}

I wrote a custom tag writer class that parses the tag objects into strings to be sent to the client and likewise a tag reader that reads the strings sent from the client and parses them into tag objects.

The client can only send commands to the server but the server sends commands, messages and objects to the client. The commands are all the same, the CMD text then the one word name of the command and then a list of arguments, messages are system, chat and tell messages but objects have a bit more information. For instance, an object tag encompasses several different types starting with a base ACTOR, a PLACE, a PLAYER and a PROP or property. The ACTOR, PLACE and PLAYER tags all define the ID, Name and Descriptions of the objects, with some additional data per type but the PROP tag defines the object id of its owner and a name/value pair. An example of a PLAYER tag with properties for x, y and z coordinates might be:

{OBJ|PLAYER|3756|"Aldarian"|"My character description..."}




The client can find and parse the player tag and then find the properties associated with that player instance. The way the server works now it will send the full player tag once logged in and a character is selected and then from then on out it just sends property updates.

Using this type of tag structure allows quite a bit of flexibility for the client. I could choose to write a very simple client that only displays MSG tags, much like a MUD, I could write a simple 2D interface client and display and move sprites about the map using the properties from the server, etc. Once Perenthia is up and running I will probably post some information on the tags and how to talk with the server should someone feel the need to write their own client. :)


1/6/2009 2:10:16 PM #


Nice technique, I ended up doing something similar.

One thing that I have learned/modified.

At times due to server or client lag, more than one command gets backed up. I've built the system so that it can hold a que of commands. IE:

{CMD|SAY|"Hello World!"}{OBJ|PROP|3756|"X"|45}{OBJ|PROP|3756|"Y"|26}

The client will then work through those commands in sequence before requesting a new list.

I also log the last 100 or so commands in a table, in order to monitor or debug the system.

The last thing I added was if the que got longer then 10 items to just refresh the page (thus clearing the que as the game resets on a refresh)

This helped both the game play and the bandwidth used as less requests were made to the server.

I used all that in my rpg game

Good work, hope the idea helps.

mobeamer United States

1/6/2009 2:34:39 PM #


Nice looking game, interesting concept and implementation..

My client is Silverlight and is processing commands asynchronously on background threads so as tags are sent down to the client it processes them as it receives them and can process multiple packets of tags at once while still sending commands to the server to process. I hadn't bothered to worry about if tag groups are not sent or processed in sequence though as the server will correct them. I will have to see how it plays out once there are more people playing and causing more of a load on the server. I appreciate the tips though and will keep them in mind.

I implemented command logging pretty early on, during a debug build it writes to the output window, during a release build it logs to a database table that have scheduled to backup and purge the records.

calbert United States

Powered by BlogEngine.NET
Modified Theme by Mads Kristensen

About the Author I am Senior Software Development Consultant specializing in Silverlight, WPF and the Microsoft .NET Framework. 

I have released an iPhone game called the Adventures of Puppyman that was built using ExEn and am currently working on a WP7 and iPhone version of Perenthia soon to be released.

View Cameron Albert's profile on LinkedIn
See how we're connected

Follow cameronalbert on Twitter


Recommended Books

Silverlight 4 Business Application Development - Beginner's Guide:

Microsoft Silverlight 4 Business Application Development: Beginner’s Guide