赞
踩
翻译自:Microsoft DirectX 8.1 VBDocument
译者:Xuefeng
(未经许可请勿转载)
1. 使用DirectPlay
微软DirectPlay应用程序接口(API)是DirectX的组件之一,它使你能够开发网络应用程序,比如多人游戏。
像其它DirectX组件一样,DirectPlay可以被用于C,C++和Microsoft Visual Basic。
关于DirectPlay的详细介绍将在下面。
1.1. DirectPlay8.1的新特性
DirectPlay 8.1的新特性
Microsoft® DirectX 8.1® is a major release primarily for DirectX graphics. The improvements for Microsoft DirectPlay® are primarily performance enhancements.
To find out more about the new features in DirectX, see What's New in DirectX 8.1.
DirectPlay 8.0的新特性
The networking component of DirectX has undergone a major revision. DirectPlay introduces a new set of interfaces that enable games to have more direct access to the hardware, providing better performance.
The following list describes some of the new DirectPlay features.
Objects have been completely rewritten.
The complexity of creating a networked application has been dramatically simplified by separating the interfaces for creating peer-to-peer and client/server sessions. The objects for creating DirectPlay transport sessions are now:
DirectPlay8Peer
Provides methods for creating peer-to-peer sessions.
DirectPlay8Client
Provides methods for creating the client-side portion of a client/server application.
DirectPlay8Server
Provides methods for creating the server-side portion of a client/server application.
Lobbying is now independent from the rest of DirectPlay.
DirectPlay has removed the requirement that a lobby client work only with a DirectPlay application. This will allow for either the lobby service provider or the application to implement DirectPlay without concern for the other. Lobby implementation is now separated into two simplified objects.
DirectPlay8LobbyClient
This object is used to manage a lobby client and for enumerating and launching lobby-aware applications.
DirectPlay8LobbiedApplication
This object is used to register a lobby launchable application with the system so it can be lobby launched. It also is used to get the connection information from the lobby to enable game launching without querying the user.
添加了语音通信。
DirectPlay Voice提供的一系列对象使应用程序能具有即时语音通信的能力。以下对象都在Dvoice.h头文件中被定义。
DirectPlayVoiceClient8
提供方法在一个DirectPlay Voice会话中创建和管理客户端。
DirectPlayVoiceServer8
提供方法在一个DirectPlay Voice会话中创建和管理主机端(服务器端)。
Used to test DirectPlay Voice audio configurations.
Addressing information has moved from GUID-based data to URL-based data format.
Previous versions of DirectPlay used binary chunks of data with GUID addresses that were difficult to implement and that humans could not read. In DirectX 8.1, DirectPlay introduces the representation of addresses in URL format. DirectPlay provides an object that is used to create and manipulate the new addressing format.
DirectPlay8Address
Provides generic addressing methods used to create and manipulate DirectPlay addresses.
Higher scalability and better memory management have been added.
Increases in consumer bandwidth have dramatically affected network game design and implementation. Improved DirectPlay thread-pool management makes it easier to for the developer to design scalable, more robust applications that can support massive multiplayer online applications.
Better support for firewalls and network address translators has been added.
Writing network games that traverse network address translators (NATs), firewalls, and other Internet connection sharing (ICS) methods can be difficult, particularly for non-guaranteed (UDP) traffic. Because DirectPlay 8.1 has been developed with these issues in mind, it will support NAT solutions where possible. The DirectPlay 8 TCP/IP service provider uses a single, developer-selectable UDP port for game data, making it possible to configure firewalls and NATs appropriately. Additionally, DirectPlay makes use of UDP so that, for client/server games, clients behind some NATs will be able to connect to games without additional configuration.
Test your application under a variety of network conditions with DP8Sim
You can now use a network simulator tool, DP8Sim, to help you predict how your application will perform under network conditions such as high latency or packet loss. DP8Sim is a simulated TCP/IP service provider applied on top of your existing network characteristics. A simple user interface (UI) allows you to easily change the simulated network settings and monitor your application's performance.
1.2. DirectPlay简介
DirectPlay接口为开发者提供了多用户应用环境下(如游戏或聊天等)的开发工具。简单地说,像游戏者中多用户应用程序,有两个基本特点:
¨ 有两个或两个以上的用户,每个用户在他们各自的计算机上运行客户端程序。
¨ 网络链接使用户的计算机能够互相通信,可能会通过一个集中的服务器。
DirectPlay提供了一个接口层使你的应用程序和网络链接最大限度地分离开来。在大多数情况下,你的应用程序只需要简单地调用DirectPlay接口,让DirectPlay处理网络链接的细节。DirectPlay提供许多特性使实现多用户应用程序变得简单,包括:
¨ 创建和管理对等网或客户机/服务器模式的会话。
¨ 在一个会话中管理用户和组。
¨ 在不同的网络链路和变化的网络环境中管理会话中各成员间的通信。
¨ 使应用程序能与Lobby(游戏厅)交互。
¨ 使用户间可以通过语音交谈。
以下内容将告诉你如何在多人游戏中使用DirectPlay。
¨ 创建和管理会话。
¨ DirectPlay的网络通信。
¨ DirectPlay对象的通信。
¨ DirectPlay的Lobby支持。
¨ DirectPlay语音通信。
注意:如果你想通过DirectPlay的语音接口来开发电信设备的话,推荐你先决定你的产品是否要遵循47 U.S.C. Sec. 255标准,以确保你的产品的兼容性。具体请参考www.fcc.gov/cib/dro/,你也可以访问www.microsoft.com/enable以获得更多的可用信息和微软产品信息。
1.2.1. 创建和管理会话
一个游戏会话就是一个特定的多人游戏实例。一个会话有两个或两个以上的游戏玩家同时进行游戏,每个玩家都在各自的计算机上运行客户端程序。一个游戏玩家本身就是游戏中被定义的一个实体,游戏程序必须通过不同的DirectPlay接口和对象来管理这些玩家。
创建一个会话的第一步是收集一组用户,有两条途径:
¨ 许多游戏以Lobby方式(注:即游戏厅,某个用户创建一个“游戏厅”,然后其他用户加入进来)运行在一台远程计算机上,常见于大多数Internet游戏。
¨ 也可以通过各个单独的计算机间的通信来管理游戏,这要求所有的用户必须处在一个局域网中。
一旦会话被创建,游戏就开始了。当会话进行时,一些玩家可能会离开,或者有新的玩家进入,这取决于游戏本身。
在一个多人游戏中,每个玩家的游戏界面必须能够和其他所有玩家的同步,管理一个多人游戏的会话就需要持续不断地在各个玩家间保持通信。例如,当一个玩家移动后,消息必须传到其他所有玩家,以更新这个玩家在游戏中的位置。DirectPlay的核心就是能支持在各个计算机间进行复杂通信的一组API接口。
有两种基本方法定义网络的拓扑结构:对等网和客户机/服务器模式。两种结构各有优劣,因此你需要选择其中合适的一种。
1.2.1.1. 对等网模式
<shapetype id="_x0000_t75" coordsize="21600,21600" o:preferrelative="t" o:spt="75" filled="f" stroked="f" path=" m@4@5 l@4@11@9@11@9@5 xe"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0 "></f><f eqn="sum @0 1 0 "></f><f eqn="sum 0 0 @1 "></f><f eqn="prod @2 1 2 "></f><f eqn="prod @3 21600 pixelWidth "></f><f eqn="prod @3 21600 pixelHeight "></f><f eqn="sum @0 0 1 "></f><f eqn="prod @6 1 2 "></f><f eqn="prod @7 21600 pixelWidth "></f><f eqn="sum @8 21600 0 "></f><f eqn="prod @7 21600 pixelHeight "></f><f eqn="sum @10 21600 0 "></f></formulas><path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype>一个对等网游戏由各个单独的用户计算机组成,通过网络链接。通常一个4人游戏的结构如下:
游戏运行时,每个玩家的计算机都直接与其他玩家的计算机通信。当一个玩家退出时,该计算机必须向其它3台计算机发送消息以更新状态。
基于对等网的游戏通常通过Lobby应用来实现,有两种途径可以在一个会话中实现:
¨ 一个Lobby客户能够直接与其他Lobby客户通信,常用于当所有的玩家都处在局域网中的同一个子网中时。
¨ 一个Lobby作为客户而另一个Lobby作为服务器运行在远程计算机上,常用于基于Internet的游戏。
一旦会话启动,大多数的通信都在玩家的计算机之间。如果存在一个Lobby服务器,它仅仅处理一些诸如更新会话成员(玩家退出或新玩家加入)的任务,否则,服务器一直呆在后台,通常它不知道客户端计算机间的通信。
由于服务器可能不存在或者至少没有直接参与游戏,因此有一个玩家的计算机可以被指派为游戏主机,用于处理一些像允许新玩家加入等的细节任务。
对等网具有简单的特点,只需要游戏玩家的计算机有网络连接和管理会话的方式。其主要缺点是可靠性差,当用户数量上升时,用于处理计算机间的通信量呈几何级数增长,最多用户数量取决于游戏本身和网络带宽。典型的对等网游戏玩家不超过20到30个。
1.2.1.2. 客户机/服务器模式
一个客户机/服务器模式的游戏由一些单独的计算机通过一个集中的服务器连接起来。通常,一个4人的客户机/服务器游戏结构如下:
游戏的进行是通过每个玩家的计算机和服务器交流完成的。服务器负责在各个玩家的计算机间传递信息。例如,当一个玩家离开时,他发送信息给服务器,服务器再向其他所有玩家发送信息来更新游戏状态。服务器有以下责任:
¨ 在会话中扮演集线器的角色。每个客户端计算机仅仅需要向服务器发送信息,服务器负责使所有玩家保持同步。这种安排能充分地减少网络信息堵塞,尤其是在大型游戏中。
¨ 充当游戏主机。服务器承担在对等网中担任主机的计算机的类似的管理任务。
¨ 支持游戏的多种特性。相对于管理游戏玩家,服务器通常要处理更多的任务。在许多游戏中,尤其是大型游戏中,服务器的许多时间被用于处理“游戏世界”的任务,而客户端计算机只负责绘制游戏界面。
一个客户机/服务器游戏通常将一个Lobby客户端程序运行于玩家的计算机上,Lobby客户端程序扮演同服务器交涉的任务,一旦游戏开始,服务器充当主机,在游戏中管理像允许新玩家加入等任务。
以下是客户机/服务器游戏的优点:
¨ 非常高效,尤其是在大型游戏中。通常它优于对等网游戏,因为任意一个玩家只存在一条网络链路通向服务器,这种客户机/服务器拓扑结构更适合非常大型的多人游戏。
¨ 游戏的运行不受客户端计算机处理能力的限制。你可以将游戏运行在一台处理能力非常强大的服务器上,客户端只需要绘制游戏界面。
你可以在集中的服务器上控制游戏的关键特性。例如,你可以经常更新游戏或修补游戏中的缺陷,仅需在服务器端作更改,从而避免了在众多的客户端更新程序。
然而,一旦你开发了一个对等网游戏,本质上就决定了游戏开发的结束。每个客户端很大程度上是自给自足的。而在客户机/服务器游戏中,你可以不断地在服务器上更新以便为玩家提供服务。你也必须提供和维持服务器及相关软件,通过网络链接持续地提供服务。在一个很大的多人游戏中,你必须使服务器提供不间断的或极少间断的服务,否则将冒着中断游戏而惹恼众多玩家的危险。
1.2.2. DirectPlay的网络通信
DirectPlay的主要功能就是为你提供最有效的和可伸缩的通信支持以最大限度地隔离你的应用程序和底层的网络软硬件。如果你需要更新状态,你只需简单地调用相关的DirectPlay接口而不管使用何种网络协议。DirectPlay网络服务支持TCP/IP,IPX,Modem和Serial网络链接。
注意:DirectPlay不支持安全通信。
要在Windows95上使用Modem,你必须安装2.0版的TAPI(电话应用程序接口)。你可以从http://www.microsoft.com下载TAPI 2.0。
DirectPlay的传输协议
DirectPlay的网络连接能力的核心就是DirectPlay协议。这种传输层协议已经由DirectPlay8完全实现,被用于所有的通信。DirectPlay协议的焦点是使你很轻易的向目标应用程序发送信息,不需要知道通信间的具体情况。这个协议提供一系列特性来适应多人游戏,包括:
¨ 可靠的和不可靠的消息传递。可靠的消息将由接收方返回确认信息。你可以定义一种基本的消息传书模型。
¨ 顺序的和无序的数据报传输。顺序的信息传送将按照它们发送的顺序。
¨ 数据分组-组装。如果数据大小超过了特定的网络协议的规定,DirectPlay自动的分组传送它们并在接收端重新组装。
¨ 阻塞控制。DirectPlay自动控制你要发送的数据在目标计算机的接受范围内。这个特性阻止你向目标发送超过其接受能力范围的数据。
¨ 优先发送。为确保最重要的数据被首先发送,DirectPlay能让你指定信息的优先级,如低,中和高优先级。高优先级的信息将首先从消息队列中发送,紧接着是中优先级和低优先级的信息。
¨ 消息超时。为阻止在消息队列中消息的堵塞引起新消息的延迟发送,DirectPlay能让你指定一个消息超时时间。当一个消息超时后,它将从消息队列中移去,不管它是否已经发被发送。
DirectPlay地址
为了发送消息,每个游戏参与者必须有一个唯一的地址。地址能够定位任意一台正在运行游戏程序的计算机,或者你的游戏程序希望与一台指定的计算机交流。
DirectPlay的地址形如URL字符串。这些字符串由一个指定字符,分隔符和数据串以以下形式构成:
x-directplay:/[data string]
数据串包含许多元素以指定发送方和接收方间实现通信的所有信息。在使用中,URL字符串已经包含在DirectPlay地址对象中以在DirectPlay API方法中传递。你可以直接操纵URL字符串,或者使用地址对象提供的方法来控制数据串中的每一元素。
1.2.3. DirectPlay对象的通信
Microsoft DirectPlay主要由一些COM对象组成。每个对象都提供一个或多个接口使你能够控制DirectPlay的各个方面。例如,DirectPlay peer对象被用来管理一个对等网游戏。
You communicate with a DirectPlay object by calling the methods exposed by its interfaces. For instance, to send some data to another user in a peer-to-peer game, you would send a message by calling the DirectPlay8Peer.SendTo method. DirectPlay then takes care of getting the message to its target.
DirectPlay communicates with your application through one or more message handlers. A message handler is an object that DirectPlay calls to notify your application of various events. The documentation describes the methods that are exposed by the object, but you must implement all of the objects methods in your application. You then register the object during startup, and DirectPlay will call the object's methods to notify you when an event has occurred. Additional information about the event is passed through the method's parameters.
1.2.4. DirectPlay的Lobby支持
A lobby is an application whose primary purpose is to enable players to meet and arrange games. It is typically located on a remote computer, and accessed over the Internet. Lobby servers often also perform a variety of other functions, such as hosting chat rooms, posting news and information, and selling merchandise. While lobby servers are convenient and commonly used to arrange multiplayer games, they aren't required. Multiplayer games can also be arranged by direct communication between lobby clients.
There are normally three components that are needed to enable a game to interact with a lobby:
¨ A lobby server
¨ A lobby client
¨ A lobbyable game.
Microsoft® DirectPlay® does not specify how you should implement a lobby server application. Instead, DirectPlay provides support for a lobby client. A lobby client is an application that is implemented by a lobby server vendor, and installed on each user's system. It serves as a link between the user and the lobby. While you could handle such communication directly, you would have to know the specific implementation details of every lobby that might launch your game.
The lobby client application handles the details of communicating with its associated lobby server, using whatever protocols are appropriate. The lobby client communicates with the user and their game applications through a DirectPlay interface. DirectPlay then passes messages to the application. The application can also use a DirectPlay interface to pass messages to the lobby client.
A lobby can launch virtually any application. However, the application must have some specific lobby-aware components to take full advantage of lobby-launching. In particular, a lobbyable application can communicate with the lobby client throughout the course of the session. If an application is registered as lobbyable, the lobby client also automatically receives updates for various changes in game status, such as host migration.
1.2.5. DirectPlay语音通信
The current trend toward team-based multiplayer games makes player-to-player communication an essential part of game play. Historically this has been confined to text-based communication, where players type out the messages to their teammates. Although suitable for slower, turn-based games, text-based communication is at best an inconvenience for real-time games. Not only does it put slow typists at a disadvantage during game play but also it is a significant break in the reality that games attempt to create for the player. An obvious solution to the problem is the use of speech as a means for communication. It requires no training and increases the immersion of the game itself.
The Microsoft® Windows® platform provides all the tools required to provide real-time voice conferencing to video game developers, but it requires a significant amount of effort on the part of the game developer. This, combined with the cost and difficulty of obtaining the rights to compression technology capable of handling extremely low bandwidth situations, has prevented the widespread use of voice in games.
Microsoft DirectPlay® 8.1 provides the game developer with a robust real-time voice conferencing system that requires minimal effort to use.
1.3. 理解DirectPlay
以下讲解DirectPlay提供的平台。你需要了解如何在应用程序中使用DirectPlay的API接口。
1.3.1. 对等网会话
一个对等网会话由一组用户通过网络链接构成。一个Lobby服务器可能被使用,消息的传送是直接在两台计算机之间进行的。和服务器的交流仅限于更新会话成员等。
在一个对等网游戏中,运行游戏的全部条件就是客户端软件。由于没有服务器参与,创建、维持和处理游戏都必须在客户端进行。以下讨论基本的对等网游戏原理。
1.3.1.1. 初始化对等网会话
一个对等网游戏可以有玩家直接启动,或者由一个Lobby客户端程序启动。以下假设游戏基于Lobby,并且可以与其他Lobby客户端通信。
你需要做的第一步是决定你的游戏是否以Lobby方式启动,如果是,先创建一个DirectPlay8LobbiedApplication对象和一个DirectPlay8LobbyEvent对象,并通过DirectPlay8LobbiedApplication.RegisterMessageHandler注册DirectPlay8LobbyEvent对象。DirectPlay8LobbyEvent对象本质上是直接或间接地接收Lobby程序的事件消息并处理这些事件。具体的处理必须由你的程序实现而不是靠DirectPlay。如果游戏以Lobby方式启动,DirectPlay将调用DirectPlay8LobbyEvent.Connect方法。dlNotify参数是一个DPL_MESSAGE_CONNECT结构,包含了连接的具体信息如成员地址等。
你也应该创建一个DirectPlay8Peer对象,并通过DirectPlay8Peer.RegisterMessageHandler注册一个DirectPlay8Event事件处理对象。这些对象将主要同其它客户交互。
1.3.1.2. 为会话选择网络协议
The service provider is your network connection. Most games use either the TCP/IP or modem service provider, but Microsoft® DirectPlay® also provides support for serial and IPX connections.
If your user was connected to the session by a lobby client, you can determine the appropriate service provider by examining the DPL_CONNECTION_SETTINGS type that you receive when DirectPlay calls your DirectPlay8LobbyEvent.Connect. Otherwise, you may need to determine which service provider to use, perhaps by querying the user. You can use the client object's DirectPlay8Client.GetServiceProvider method to enumerate the available service providers. See Using DirectPlay Enumerations for further discussion.
Once you have selected a service provider, you can then create a DirectPlay address object for your user (a device address). You will use this address to identify your device with a number of DirectPlay methods. See DirectPlay Addressing for a detailed discussion of DirectPlay addresses and address objects.
1.3.1.3. 为会话选择一个主机
Although most aspects of peer-to-peer games can be handled by the various users' communicating directly with each other, there are some tasks that must have a single owner. These tasks are handled by the game host. To join a session, you must know the address of the session's host. A common way to select a host is through a lobby server. In that case, when a user's application is connected to the session, the connection settings that you receive when Microsoft® DirectPlay® calls your DirectPlay8LobbyEvent.Connect method include the host's address object. To find out who the session host is:
¨ Check the dwFlags member of the DPL_CONNECTION_SETTINGS structure that is contained in the DPL_MESSAGE_CONNECTION_SETTINGS passed as the dlNotify parameter. If that member is set to DPLCONNECTSETTINGS_HOST, your system is the host.
¨ If the DPLCONNECTSETTINGS_HOST flag is not set, then you can get the address of the host from the pdp8HostAddress member.
You can also create a broadcast session, perhaps on a LAN subnet, by advertising yourself as a session host. To do so call DirectPlay8Peer.SetPeerInfo to set the player's name and then call DirectPlay8Peer.Host to advertise yourself as a potential host. You specify the configuration of the game by assigning values the DPN_APPLICATION_DESC type that is passed through the pdnAppDesc parameter of IDirectPlay8Peer.Host.
To allow your user to examine the available sessions and hosts, you can enumerate the available hosts by calling DirectPlay8Peer.EnumHosts. Once the user has selected a session, you can request a connection.
1.3.1.4. 连接到一个会话
除非你是会话的主机,否则你就必须将玩家连接到会话。为了实现这一点,你必须有会话中主机的地址。If your application was connected by a lobby client, you can obtain the host's address by calling DirectPlay8LobbiedApplication.GetConnectionSettings. You can also obtain the address by enumerating the available hosts. The information returned by the enumeration includes each host's addresses, and a DPN_APPLICATION_DESC structure that describes the associated session.
To ask to join a session, call DirectPlay8Peer.SetPeerInfo to set your player's name, and then call DirectPlay8Peer.Connect with the selected host's address to connect to the session.
When a player attempts to join a session, the Microsoft® DirectPlay® calls the host's DirectPlay8Event.IndicateConnect method. To accept the player into the session, set the method's fRejectMsg parameter to False before returning. Setting fRejectMsg to True rejects the request. In either case, the player's DirectPlay8Event.ConnectComplete method will be called with the response. If the host accepted the connection, the hResultCode member of the DPNMSG_CONNECT_COMPLETE type will be set to 0. If the request was rejected or failed for some other reason, hResultCode will be set to an error code.
Once the new player is connected, DirectPlay announces the new player by calling DirectPlay8Event.CreatePlayer for each member of the session, including the host. The lPlayerID parameter contains the player ID that you will use to send messages to that player.
1.3.1.5. 管理会话
The session host is responsible for managing the session, including:
¨ Managing the list of session members and their network addresses
¨ Deciding whether a new user is allowed to join the session.
¨ Notifying all members when a new user joins the session, and passing them the new user's address.
¨ Providing new users with the current game state
¨ Notifying all users when a user leaves the session
When players attempt to join a session, Microsoft® DirectPlay® will call the host's DirectPlay8Event.IndicateConnect method. To accept the player into the session set fRejectMsg to False. Setting fRejectMsg to any other value rejects the request. In either case, DirectPlay calls the player's DirectPlay8Event.ConnectComplete method with the response to the request.
The host can remove a player from the session by calling DirectPlay8Peer.DestroyPeer. Other members of the session cannot call this method successfully. If you want to allow players to request that another player be removed from the session, you must send the request to the host, and have the host handle the request.
1.3.1.6. 移动主机
While the host must be one of the initial members of the session, they may choose to leave before session is finished. When the host leaves the session, there are two possible outcomes:
¨ 会话终止。
¨
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。