Handling Lost Sessions

When the host of the session quit, the connection was then dropped. There was no longer any host, so the connection couldn't be maintained any longer. DirectPlay sent a message to all connected clients informing them of this; however, we never included a handler for this event. Let's do this now:

 connection.SessionTerminated += new     SessionTerminatedEventHandler(OnSessionTerminate); 

The session-terminated event can be fired when the host has quit (like we did previously) or for just about any other reason why you can no longer be in the session. For example, if you unplugged your network cable, shortly later you would receive this event because no one in the session could be contacted. We should add the code for this handler now:

 private void OnSessionTerminate(object sender, SessionTerminatedEventArgs e) {     // The session was terminated, close our peer object, re-enable our buttons     this.BeginInvoke(new DisconnectCallback(OnDisconnect), null); } 

Hmmm, we've got a new delegate we're calling, plus an entirely new method. Why exactly did we do it this way? First, we'll declare the delegate and the method used:

 private delegate void DisconnectCallback(); private void OnDisconnect() {     // Re-enable the UI     button1.Enabled = true;     button2.Enabled = true;     button3.Enabled = false;     // Notify the user     AddText("The host has quit, you have been disconnected.");     // Dispose of our connection, and set it to null     connection.Dispose();     connection = null; } 

Now you can see the reason for the new delegate and method. We need to do quite a bit here. First, we switch the button states back to the defaults so that we can be allowed to host a new session or join a different session. We then update the UI to inform the user, and finally, since this connection is no longer valid, we dispose it and set it to null.

SHOP TALK: HANDLING HOST MIGRATION

I know what you must be thinking. Just because the first person in the session quit, why does the entire session need to be disbanded? That seems pretty stupid. Luckily, this isn't always the case. DirectPlay has a feature called "Host Migration" that will automatically pick a new host out of the remaining peers if the original host happens to quit. In the design view of our form, add a check box between the Connect and Send Data buttons. The text for this button should be "Migrate the host".

Now we'll need to update a few different places in our application to enable host migration. First, we'll need to see whether our box has been checked before we host, since the peer that calls Host must include the host migration flag if the option is going to be used. Add the following line before the call to Host:

 // Should we allow host migration? desc.Flags = checkBox1.Checked ? SessionFlags.MigrateHost : 0; 

As you see, if the box is checked, we include the migrate host flag; otherwise, we do not. You will also need to include the checkbox control in the OnDisconnect method and the first two button event handlers. You should enable/disable this control just like you do the connect and host buttons.

Lastly, it would be nice if the UI was updated when the host has quit and the host migration has been kicked off. Naturally, there is an event for this as well. Add this handler:

 connection.HostMigrated += new     HostMigratedEventHandler(OnHostMigrate); 

The handler for this event is as follows:

 private void OnHostMigrate(object sender, HostMigratedEventArgs e) {     // The host has been migrated to a new peer     this.BeginInvoke(new AddTextCallback(AddText),         new object[] { "The host was migrated." }); } 

Here, we don't actually do anything other than update the UI. However, if your application had host-specific options, you would want to enable them in this method by checking the NewHostId member of the HostMigratedEventArgs object.



Managed DirectX 9 Graphics and Game Programming, Kick Start
Managed DirectX 9 Kick Start: Graphics and Game Programming
ISBN: B003D7JUW6
EAN: N/A
Year: 2002
Pages: 180
Authors: Tom Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net