Launching Applications Using a Lobby

Anyone who's ever played games someplace such as MSN Games by Zone.com (http://zone.msn.com) understands the concept of a lobby, even if you don't realize it. A lobby is essentially where a group of players get together before they actually start playing the game. The game can then be "launched" from the lobby and all of the players in that lobby will automatically start the game and connect to each other.

Looking at the DirectPlay assemblies, you may have noticed that this is the first assembly that has "sub-namespaces." To group functionality inside this assembly, there are two extra namespaces included: Lobby and Voice. We will get to the Voice functionality later in this chapter, so for now we will concentrate on the Lobby namespace.

There are two main classes in the Lobby namespace that will control how you interact with lobby-aware applications: Application and Client. The Client class is used to enumerate and launch lobby-enabled applications on your local system. The Application class is used by the application that supports the lobby. Each of these classes has an event model that is similar to the one used by the Peer, Server, and Client classes, as well.

The easiest thing to do is to list the programs that are currently able to be launched via a lobby, and then launch them, so let's try that first. Create a new windows form application, adding a list box with the dock parameter set to fill, so that it encompasses the entire form. Also make sure that you have included the reference to the DirectPlay assembly and included the using clause for the lobby namespace as shown here:

 using Microsoft.DirectX.DirectPlay.Lobby; 

As we've already mentioned, the Client class will control enumerating and launching a lobby-enabled application, so we will need to declare this variable:

 private Client connection = null; 

Before we forget, we should also make sure we dispose of this object when the application quits. Add the following to the Dispose override:

 if (connection != null)     connection.Dispose(); 

You'll notice that the class name (Client) exists both in the core DirectPlay namespace as well as the Lobby namespace. You'll need to make sure that you include both of these namespaces in your using clauses to fully qualify these variables. You may also notice that the other class (Application) also exists in the System.Windows.Forms namespace, so you will need to fully qualify any references to that as well.

Now, let's fill our list box with the names of the applications that are registered as being lobby-aware. Add the following code to your forms constructor:

 // Fill the list box connection = new Client(); foreach(ApplicationInformation ai in connection.GetLocalPrograms()) {     listBox1.Items.Add(ai.ApplicationName); } 

As you see, this is a relatively simple operation. After we create our lobby client object, we simply enumerate through each of the local programs registered and write the name of that application into our list box. Launching these applications is equally easy; add an event handler for the double-click event of the list box. Use the code found in Listing 20.1.

Listing 20.1 Launching an Application
 private void listBox1_DoubleClick(object sender, System.EventArgs e) {     if (listBox1.SelectedItem == null)         return;     foreach(ApplicationInformation ai in connection.GetLocalPrograms())     {         if (ai.ApplicationName == (string)listBox1.SelectedItem)         {             ConnectInformation ci = new ConnectInformation();             ci.GuidApplication = ai.GuidApplication;             ci.Flags = ConnectFlags.LaunchNew;             connection.ConnectApplication(ci,                 System.Threading.Timeout.Infinite, null);             break;         }     } } 

Now, this section of code isn't the most efficient, since we potentially enumerate through the entire list once more, but it's easy to read. If there is an item currently selected, we will go through the list of registered programs until we find the one that is currently selected. Once we find it, we initialize a ConnectInformation structure with the GUID of the application and flags instructing it to launch a new instance.

CONNECTING TO AN EXISTING SESSION

It's also possible to "connect" to an already running application that is waiting for a lobby connection. In order to do this, you must fill out the ConnectionSettings structure and change the flags to ConnectFlags.LaunchNotFound. This will still launch a new instance if a running one was not found.

If you try to compile this application, you will notice that the main method will complain that the Application class is ambiguous. It says this because there are two classes named Application, one in System.Windows.Forms, and another in Microsoft.DirectX.DirectPlay.Lobby. Replace the main method with this one to eliminate this error:

 static void Main() {     using (Form1 frm = new Form1())     {         frm.Show();         System.Windows.Forms.Application.Run(frm);     } } 

Once the application has been successfully launched, a connection handle will be returned to you from the ConnectApplication method. With this handle you can send data to the application via the Send method, which will fire an event in the launched application's Lobby.Application object. You may also call the ReleaseApplication method, which signals that you are done with the application. This will not cause the launched application to quit; it will simply disconnect the lobby client object from the application. The connection handle is no longer valid after this call has been made.



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