1 post tagged “networking”
Hello fellow developers, I haven't made a mention of it before, but I've been working on an XNA network library for a little while now. I like XNA GS, but I don't have a subscription to the Creator's Club so I can't really test my projects on the Xbox 360. When the XNA Framework v1.0 came out I was incredibly excited. I had been developing in Managed DirectX up until then so having a framework that takes away all the complexity was really nice for me. The missing component with the XNA Framework v1.0 at the time was networking capabilities. The DirectX team was no longer supporting DirectPlay either so I wanted to make my own networking api for games.
C# TCP Networking
A year or two ago a friend of mine and I wrote a networking api for a load-balanced distributed helpdesk system. I found the api about a month ago under some old folders in a hard drive and decided to shine it up for XNA. Here we go.
XNANetwork handles a few things that you'd normally expect in a networking api. All networking is done through Asynchronous TCP sockets. The api already manages all the sockets for you in the SocketManager, complete with thread locking. Although the topology seems to be Client-Server bound, you can really use the api to create any type of networking system: P2P, Client-Server, or even Distributed Servers. I can vouch, my friend and I have used the api in all network topology scenarios.
There are a still a few things that I'd like to put into this api. One being UDP (or even a real-time protocol) socket connections instead of TCP. But I figured the effort in putting together the repackaging, the out-of-order transfers... it's just to much work right now. So, UDP aside, it works substantially well in managing most of everything you need in a networking api.
Because of my previous work, I had to have all my messages in XML form to ensure valid messages were being sent back and forth, not to mention the encryption layer. For the sake of this article I have left out the encryption layer. On top of the XML, there were many different triggers in the system that needed to occur, and I needed to find a way to add triggers without the hassle of changing components. So, I wrote a library called GSS.
GSS is a simple command messaging syndication that allows different commands to be event driven. Every command has it's name and an array of possible parameters. When you start your project, you can register commands into the GSSManager by the given name and a delegated callback method for that command. You can also use the GSSEnumerator to register an entire enumeration of commands. The Interpreter handles all the the conversion and delegated executions. Just create a new interpreter, and work from there. It's pretty straight forward.
But, in case you don't want to use XML, which in most cases you may not, you can transfer your items with straight byte conversions with in interface called ITransferable. This interface is in the XNANetwork which I'll discuss now.
Session
XNANetwork has a class called Session. Within this class you can make a call to Session.Create() to create a new local "server" connection on the default port 2093. When you want to join a network you simply have to make a call to the Session.Join method. The only downfall at this point is that I haven't implemented a simple registration service like Xbox Live has. So joining by nickname does not necessarily work at all at this point.
Because of this, in order to join networks you need to use the GameClient class instead. When you create a new game client you need to pass the server that you plan on joining and the port to connect on. Here is an example implementation of my networked game client.
There are plenty of improvements that could be made to this api. My largest concern with this api is dealing with three major networking issues: latency, packet loss, and bandwidth throttling. The networking api, GSS, XNANetwork, MasterServer and network client are all included in the download.
If you plan on using this library please let me know as I would love to give or get insights on this project's current state.