The Event Handlers


That's all fine and good, but where did you connect? Fact is, currently, you haven't. The FindHosts method searches the hostname you've specified for valid sessions of this game. If it finds any, the FindHostResponse event is fired (which is why you hooked that method earlier). See Listing 16.6 for the implementation of this method, which is where the connection will occur.

Listing 16.6. Connecting to a Found Session
 /// <summary> /// Fired when a host has been found /// </summary> private void OnFoundHost(object sender, FindHostResponseEventArgs e) {     // If you've already connected, don't try again     if (isConnected)     {         return;     }     // Try to connect now     isConnected = true;     Peer connection = sender as Peer;     connection.Connect(e.Message.ApplicationDescription, e.Message.AddressSender,         e.Message.AddressDevice, null, 0); } 

The find method actually searches a particular subnet of the network for hosts, depending on the hostname that you've entered. When more than one host is found, you only want to try to connect to one, so first you check whether you're already connected, and if so, you simply leave the method. If you are not connected, first mark yourself as connected, and then try to connect. You see that you can get the Peer object from the event-handler parameters, as well as all the parameters to the Connect method. What if the connection fails? Currently, you set the connected Boolean value to true, and it will never try to connect again. See Listing 16.7 for the implementation of the connection-complete event handler.

Listing 16.7. Connection Is Complete
 /// <summary> /// Fired when the connection is completed /// </summary> private void OnConnectComplete(object sender, ConnectCompleteEventArgs e) {     // Was the connection completed successfully?     if (e.Message.ResultCode == ResultCode.Success)     {         isConnected = true;         if (remotePlayer != null)         {             SendNewColor(remotePlayer.Color);         }     }     else     {         // Well that sucks, the connection wasn't made         System.Diagnostics.Debugger.Log(9, GameEngine.GameName,             string.Format("The connection failed.  Result was {0}.",             e.Message.ResultCode));         // Let the game engine know         isConnected = false;     } } 

This event will let you know whether the connection was successful. If it wasn't, you just spew some debug text and reset your variable. If the result was successful, and you have a remote player, you send the color of that remote player to the other peer in the session. I show you this method in a few moments. Before you get there, however, look through the other event handlers you need to implement (in Listing 16.8).

Listing 16.8. The Event Handlers
 /// <summary> /// Fired when a new player was created, including yourself /// </summary> private void OnPlayerCreated(object sender, PlayerCreatedEventArgs e) {     // See if this player is the host     if (e.Message.PlayerContext as string != SessionName)     {         // Store the player ID         remotePlayerId = e.Message.PlayerID;         // This player is not, notify the game engine         if (NewPlayer != null)         {             NewPlayer(this, e);         }         // Update the player name too         PlayerInformation info = networkDevice.GetPeerInformation(             e.Message.PlayerID);         remotePlayer.Name = info.Name;     } } /// <summary> /// Fired when a new player was destroyed, including yourself /// </summary> private void OnPlayerDestroyed(object sender, PlayerDestroyedEventArgs e) {     // See if this player is the host     if (e.Message.PlayerContext as string != SessionName)     {         // Remove the player ID         remotePlayerId = 0;         // This player is not, notify the game engine         if (DestroyedPlayer != null)         {             DestroyedPlayer(this, e);         }     } } /// <summary> /// Fired when the networking session was lost /// </summary> private void OnSessionLost(object sender, SessionTerminatedEventArgs e) {     // No longer connected.     isConnected = false; } 

You'll notice that the Receive event handler is not handled currently. I address this part in the next section because it's pretty tightly coupled with sending data. In the meantime, look at the three new handlers implemented here. When the session is lost (maybe the host has quit or the Internet went down), you simply mark yourself as no longer connected: nothing major. In the case of a player being created or destroyed, you first check the player's context variable. Notice here is where you're checking whether it is the local player by comparing this context value to the session name you passed in to the host call. If this player is not the local player, then you store (or clear out) the player ID number and fire the appropriate event to let the game engine know about the status. If it is a new player, you call GetPeerInformation to get the player name of the remote player to render it while playing.



Beginning 3D Game Programming
Beginning 3D Game Programming
ISBN: 0672326612
EAN: 2147483647
Year: 2003
Pages: 191
Authors: Tom Miller

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