Tales Framework for XNA: Network Helper
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:
- Subscribe to the
GameSession.Helper.XXXFinishedandGameSession.Helper.XXXErroredevents. - Call the
GameSession.Helper.XXXStartmethods to start an operation. - In the menu’s
UpdateorDrawmethod, call themethod. If new network data is available the call to.UpdateGameSession.Helperwill cause either theGameSession.Helper.UpdateXXXFinishedorXXXErroredevents to fire. - When you close the menu call the
GameSession.Helper.Clearmethod.
Here are the complete details of what happens when following the above example and steps:
- 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.FindFinishedandGameSession.Helper.FindErroredevents. When they start looking for a network game you callGameSession.Helper.StartFind. The menu's Update method should continually call theGameSession.Helper.Updatemethod. - Tales Code: The helper class asks XNA to perform a find operation.
- Game Code: When the menu is created you should register for the
- User Action: Use goes back, closing the menu before the
GameSession.Helper.FindFinishedorGameSession.Helper.FindErroredevents fire.- Game Code: When the menu closes you should call
GameSession.Helper.Clear. You should continue to call theGameSession.Helper.Updatemethod. - Tales Code: The helper is still waiting to hear back from XNA libraries so it queues the request to clear the data.
- Game Code: When the menu closes you should call
- 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.CreateFinishedandGameSession.Helper.CreateErroredevents. When the user creates the network game call theGameSession.Helper.StartCreate. In the menu's Update method you should continually call theGameSession.Helper.Updatemethod. - 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.
- Game Code: When the menu is created you should register for the
- User Action: User waits for the create game to start.
- Game Code: Continue to call the
GameSession.Helper.Updatemethod. - 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 theCreateFinishedevent will fire.
- Game Code: Continue to call the
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.
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
... so said 'Ruth' on 03/27/2009 @ 06:37:11 PM [Direct Link]