Understanding DirectPlay Addresses

Playing single-player games can be fun and exciting, but it's nothing compared to playing against real live people. Even game consoles that were notoriously single player have begun offering online play and matchmaking services. It's rare to see top-of-the-line games coming out nowadays without some form of multiplayer features. There is also an entire class of quite popular games that have no single-player options at all; they are only multiplayer.

Managed DirectX includes the DirectPlay API that can be used for implementing a networking layer. Before we begin the code for this chapter, we will once again need to ensure that we have the right references for DirectPlay. Ensure that you have the Microsoft.DirectX.DirectPlay reference included in your projects, as well as the using clause. All of the code in this chapter assumes this has already been done.

If you have a computer that can connect to the Internet, your computer already has a TCP/IP address. Each computer needs to have a unique "address" that distinguishes it from other computers on a network. While it's true that TCP/IP addresses are not unique, they are unique within a single network. TCP/IP addresses are the standard address of the Internet.

DirectPlay also needs a way to distinguish one computer from another, and each networked computer must have a unique address. The act of creating a unique DirectPlay address isn't all that difficult. Let's look at the code:

 Address address = new Address(); address.ServiceProvider = Address.ServiceProviderTcpIp; 

As the preceding code implies, there is more than one service provider that DirectPlay can use. We've chosen in this instance to use the TCP/IP provider, which is by far the most common service provider available in computers today. The service providers available to DirectPlay by default are

  • TCP/IP

  • IPX

  • BlueTooth

  • Serial connections

  • Direct modem-modem connections

USING URLS AS ADDRESSES

Addresses can also be specified in URL (Uniform Resource Locator) form, much like Web addresses. If you've seen an address like http://www.mycompany.com, you've seen an URL. The first "section" of the URL is the protocol type, in this case http (or Hypertext Transfer Protocol). The Web pages you see are normally HTML (Hypertext Markup Language), thus the name of the protocol. In DirectPlay, these URLs take on a construct; for example, the address we just created could also be specified as

[View full width]

x-directplay:/provider=%7BEBFE7BA0-628D-11D2-AE0F-006097B01411 graphics/ccc.gif%7D;hostname=www.mygameserver.com;port=9798

If you'll notice, the type of this URL is x-directplay, or a DirectPlay address. The rest of the address specifies the components of the address, the service provider GUID, the host name, and the port. There is an URL property on the address object that you can use to get the current URL of the address, or set it to a new value.

We will deal with only the TCP/IP service provider when dealing with DirectPlay. I won't spend the time to go into details of the other service providers; if you don't know about them already, chances are you won't need to write any applications using them. Besides, the DirectPlay object model is designed to work the same, regardless of the service provider in use. All of the code we write using the TCP/IP service provider applies directly to any of the other service providers.

SHOP TALK: USING TCP/IP ADDRESSES

There are four constructors for the address object, the majority of which deal with the TCP/IP service provider. The parameterless constructor that we've already used is the only constructor that doesn't set the service provider automatically. The other three constructors will each set the service provider to TCP/IP. These constructors are as follows:

 public Address ( System.String hostname , System.Int32 port ) public Address ( System.Net.IPAddress address ) public Address ( System.Net.IPEndPoint address ) 

Each of these constructors performs the same operation, just with different data sets. The first creates a TCP/IP address, sets the host name to the string provided, and sets the port to the number specified. The host name can be a computer name, an Internet address (for example, www.mycompany.com), or an IP address (for example, 192.168.2.1). Each TCP/IP connection happens on a particular port (for example, Web browsers request data on port 80), and the second parameter allows you to specify this port. Each of the other two parameters can take existing TCP/IP addresses (provided in the .NET Runtime) and convert them into the equivalent DirectPlay address.

In our first example of code, we never mentioned any "hostname" or "port." How exactly can you add these members to an address? It's quite simple actually. The address object has a method to add various components such as the hostname and port. Each component has a name (a string value) and associated data, either a string, a GUID, an integer, or a byte array. The address class itself has the standard key names attached to itself. Here is the code to manually attach the hostname and port to an existing address:

 Address address = new Address(); address.ServiceProvider = Address.ServiceProviderTcpIp; address.AddComponent(Address.KeyHostname, "www.mygameserver.com"); address.AddComponent(Address.KeyPort, 9798); 

In actuality the parameterized constructors for the Address class do this exact set of steps, replacing the host name and port with the values you specify. There are many default key names that can be used (for example KeyPhoneNumber for direct modem-to-modem connections), plus you can make up your own keys.

It's important to realize that the hostname component in particular is really only important when you are connecting to another computer. DirectPlay is smart enough to figure out your own hostname, so this key is not needed for local addresses.



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