Tales Framework for XNA: Network Helper

Posted 10/24/2008 @ 07:01:01 AM by Joseph Molnar
Filed under: Programming , Tales Framework , Xbox 360 , XNA

The past two Tales Framework posts discussed UI elements. In this post we will move to networking. In particular I’m going to concentrate on the class I built to aid the finding, creating and joining of network sessions.

XNA GS’s built-in networking functions provide a great consistent way to handle both local networked and Internet networked games while allowing you to choose either synchronous or asynchronous mechanisms to find, create and join sessions. What I did was create a helper class to alleviate one of the remaining tricky parts in getting a networked session going ... providing a responsive and thread-safe UI.

The menuing system of a game should be as responsive as possible, so you definitely do not want to use XNA's synchronous network find, create and join methods in direct response to user input. Even if you put the find, create and join calls in a separate thread or you use the asynchronous calls (which use separate threads when calling your callbacks) you still need to synchronize access to any data you share with the UI thread and manage and ensure you never try to create a NetworkSession and AvailableNetworkSessionCollection at the same time.

The GameSessionHelper class handles these tricky parts for you. For example, users can start a find operation and then immediately start a create operation and not have to wait between these two steps.

If you are going to use the GameSessionHelper class, these are the important steps:

  1. Subscribe to the GameSession.Helper.XXXFinished and GameSession.Helper.XXXErrored events.
  2. Call the GameSession.Helper.XXXStart methods to start an operation.
  3. In the menu’s Update or Draw method, call the GameSession.Helper.Update method. If new network data is available the call to GameSession.Helper.Update will cause either the XXXFinished or XXXErrored events to fire.
  4. When you close the menu call the GameSession.Helper.Clear method.

Here are the complete details of what happens when following the above example  and steps:

  1. User Action: User goes to the 'find network game' menu and starts looking for a network game.
    • Game Code: When the menu is created you should register for the GameSession.Helper.FindFinished and GameSession.Helper.FindErrored events. When they start looking for a network game you call GameSession.Helper.StartFind. The menu's Update method should continually call the GameSession.Helper.Update method.
    • Tales Code: The helper class asks XNA to perform a find operation.
  2. User Action: Use goes back, closing the menu before the GameSession.Helper.FindFinished or GameSession.Helper.FindErrored events fire.
    • Game Code: When the menu closes you should call GameSession.Helper.Clear. You should continue to call the GameSession.Helper.Update method.
    • Tales Code: The helper is still waiting to hear back from XNA libraries so it queues the request to clear the data.
  3. User Action: User goes to the 'create game' menu and creates a network game.
    • Game Code: When the menu is created you should register for the GameSession.Helper.CreateFinished and GameSession.Helper.CreateErrored events. When the user creates the network game call the GameSession.Helper.StartCreate. In the menu's Update method you should continually call the GameSession.Helper.Update method.
    • Tales Code: The helper has not yet heard back from the XNA framework on the initial find call so it queues up the request to do a create.
  4. User Action: User waits for the create game to start.
    • Game Code: Continue to call the GameSession.Helper.Update method.
    • Tales Code: The helper will eventually hear back from the XNA framework for the initial find request. The helper clears out the data, in this case the AvailableNetworkSessionCollection. Then the helper starts the game session create operation. Once the session is created the CreateFinished event will fire.

The above shows the user only waited during moments when they initiated a network request. The UI was fully responsive; to the user it felt like they were canceling or starting a new operation when in fact the old operation was completing in the background.

 

This is a pretty detailed look at the GameSessionHelper class. Too much code is required to provide an example in this post but the sample I'll be providing with the framework should provide a good amount of reference code.

Comments

I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.

Ruth

http://laptopmessengerbag.info

Post a comment