Creating a Peer Connection

With the basics of addressing out of the way, we can move on to actually getting a connection made, and really start to network. The types of networking that DirectPlay supports would be peer-to-peer and client/server. In this chapter, we will discuss the peer-to-peer connections.

So what is a peer-to-peer connection? As the name implies, each member (or peer) of a session is connected to every other member (or peer) in the session. For small sessions, this method works quite well. There are many examples of peer-to-peer networks that are quite popular today. Many of the file-sharing programs out there today (Kazaa, Napster, and so on) are peer-to-peer networks.

Before you can begin a peer-to-peer session in DirectPlay, you must first decide a few things. What service provider do you want the connection to use? Will you be "hosting" this connection, or will you connect to an existing session? You can think of the first peer in a session as the "master" peer, or host of the session.

The main class that we will use for our session is (I'm sure you've guessed it) the Peer object. It's always easier to see things visually while looking at code, so let's start writing some now. Declare a variable for our peer object, and our addresses:

 private Peer connection = null; private Address deviceAddress = null; 

You'll notice that we have our address declared. We will maintain this address to hold the connection information for our local machine. We also maintain a peer object for our connection.

In the design view for your form, create two buttons in the upper right corner. The first will be to "Host" a session, while the second will be to "Connect" to an existing session. Add a third button to the upper right corner of your form; this will be to send any data throughout the session. This third button should be disabled at first (we don't want to send any data before we are in a session). Finally, create a large label to fill up the bottom portion of the UI. This will be our status screen. With the plumbing work out of the way, we're ready to start networking.

We will create one initialization function to set up our peer object and any default state. Let's examine the function in Listing 18.1 now.

Listing 18.1 Initializing a Peer Object
 private void InitializeDirectPlay(bool host) {     // Create our peer object     connection = new Peer();     // Check to see if we can create a TCP/IP connection     if (!IsServiceProviderValid(Address.ServiceProviderTcpIp))     {         // Nope, can't, quit this application         MessageBox.Show("Could not create a TCP/IP service provider.", "Exiting",             MessageBoxButtons.OK, MessageBoxIcon.Information);         this.Close();     }     // Create a new address for our local machine     deviceAddress = new Address();     deviceAddress.ServiceProvider = Address.ServiceProviderTcpIp; } 

This method will get much larger before we're done. For now though, what is it doing? Well, first we create our peer object using the default constructor. There is one other constructor available that takes in initialization flags that we didn't use. These flags can be one or more of the values found in Table 18.1.

Table 18.1. Peer Creation Flags

FLAG

DESCRIPTION

DisableParamaterValidation

Using this flag will disable the parameter validation for this connection. This will increase performance; commonly, retail versions of applications will set this flag.

DisableLinkTuning

Using this flag will disable DirectPlay from attempting to tune the rate it sends data. All messages will be pushed into the network as soon as possible.

HintLanSession

Opens a larger send window for games running on a LAN.

None

Default options, same as using the default constructor.

These options can be quite useful, but for this simple application, they're a little overboard, so we'll just use the default constructor. We want to use the TCP/IP service provider for this application, but how do we know if this provider exists on this machine? Well, we can check, and that's what this IsServiceProviderValid method will do. Look at this method:

 private bool IsServiceProviderValid(Guid provider) {     // Ask DirectPlay for the service provider list     ServiceProviderInformation[] providers =         connection.GetServiceProviders(true);     // For each service provider in the returned list...     foreach (ServiceProviderInformation info in providers)     {         // Compare the current provider against the passed provider         if (info.Guid == provider)             return true;     }     // Not found     return false; } 

This method will retrieve every available service provider on the system and return an array of information about each one. If we had passed in "true" as the parameter, rather than false, it would enumerate all providers, including those that weren't available at the time. Part of the information returned is the GUID of the service provider, which we can compare to the one we're passing in. If we find this GUID in the returned array, we can assume this service provider exists; otherwise, it does not.

Finally, if we find that the service provider for TCP/IP is not valid, we will inform the user and quit the application. Otherwise, we will create our device address using the TCP/IP service provider. You must also ensure that you dispose of the peer object when you are finished with it; otherwise there is a good chance bad things will happen when shutting down your application. There is already a default override for Dispose, so you can simply add the following code to the beginning of this method:

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

Now we need to implement our code for hosting or joining a session.



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