Detecting Users Joining and Leaving Your Session

Now that clients are connecting to the server, it would be nice to update the UI on the server side to indicate when a client has connected (and left, for that matter). However, currently no way to do this has been discussed. As luck would have it, there are two events you can hook to give you this information. In your initialization method for the server, you will need to add the following event hooks:

 connection.PlayerCreated += new     PlayerCreatedEventHandler(OnPlayerCreated); connection.PlayerDestroyed += new     PlayerDestroyedEventHandler(OnPlayerDestroyed); 

Whenever a player either joins or leaves the session, the appropriate event will be fired. The implementations of the event handlers are pretty similar as well. Add them from Listing 19.4 to your server application.

Listing 19.4 Player Management Event Handlers
 private void OnPlayerCreated(object sender, PlayerCreatedEventArgs e) {     try     {         string playerName = ((Server)sender).GetClientInformation             (e.Message.PlayerID).Name;         string newtext = string.Format             ("Accepted new connection from {0}, UserID: 0x{1}",             playerName, e.Message.PlayerID.ToString("x"));         this.BeginInvoke(new AddTextCallback(AddText),             new object[] { newtext });     }     catch { /* Ignore this, probably the server */ } } private void OnPlayerDestroyed(object sender, PlayerDestroyedEventArgs e) {     string newtext = string.Format         ("DirectPlayer UserID: 0x{0} has left the session.",         e.Message.PlayerID.ToString("x"));     this.BeginInvoke(new AddTextCallback(AddText),         new object[] { newtext }); } 

The player-destroyed handler is quite simple, as all it does is update the UI to notify you that the user has left the session. The player-created handler has a method that has never actually been discussed before, though. In the client's application, you set the client name with a call to SetClientInformation; in the server you retrieve this name with the call to GetClientInformation. When a server first starts a session, a default "hidden" player is created. Since there is no client associated with this player, the call to SetClientInformation will naturally fail. You can simply ignore that case for this example since it is expected.

SHOP TALK: CONTROLLING WHO JOINS YOUR SESSION

It's entirely possible that there are certain users who you do not want joining your session. However, as it stands right now, it looks like just about anyone could join any DirectPlay session, assuming they knew even a small amount of information, such as the application GUID and the port the application is running on. Naturally, there has to be a way to prevent "rogue" applications from joining your session without needing to require a password.

Before the player-created event you just hooked is fired, there is another event fired: IndicateConnect. The event arguments object for this event (IndicateConnectEventArgs) includes a property, RejectMessage, that defaults to false. Based on the information you've received in this event handler, you can decide whether or not you want to allow this connection. If you do wish to allow the connection, you can simply do nothing, and the connection will be allowed. However, if you decide that this user shouldn't be allowed to connect to your session, you can simply set the RejectMessage property to true. Doing this will deny the client access to your session.

What if you don't even want your session to be found, though? You already know that when a host is found, you receive a FindHostsResponse event; however, before that event is fired on the client, the server will receive a FindHostsQuery event. You can use the same strategy as used previously to stop the clients from even receiving the FindHostsResponse event. You can have total control over who is allowed to see and join your session using a combination of these two events.

Peer session hosts have these same abilities.



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