现在的位置: 首页 > 综合 > 正文

[译]使用DirectPlay

2013年10月25日 ⁄ 综合 ⁄ 共 9152字 ⁄ 字号 评论关闭

翻译自:Microsoft DirectX 8.1 VB Document
译者:
Xuefeng
(未经许可请勿转载)

1.        使用DirectPlay

微软DirectPlay应用程序接口(API)DirectX的组件之一,它使你能够开发网络应用程序,比如多人游戏。

像其它DirectX组件一样,DirectPlay可以被用于CC++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对象的通信。

¨         DirectPlayLobby支持。

¨         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.   对等网模式


一个对等网游戏由各个单独的用户计算机组成,通过网络链接。通常一个4人游戏的结构如下:

游戏运行时,每个玩家的计算机都直接与其他玩家的计算机通信。当一个玩家退出时,该计算机必须向其它3台计算机发送消息以更新状态。

基于对等网的游戏通常通过Lobby应用来实现,有两种途径可以在一个会话中实现:

¨         一个Lobby客户能够直接与其他Lobby客户通信,常用于当所有的玩家都处在局域网中的同一个子网中时。

¨         一个Lobby作为客户而另一个Lobby作为服务器运行在远程计算机上,常用于基于Internet的游戏。

一旦会话启动,大多数的通信都在玩家的计算机之间。如果存在一个Lobby服务器,它仅仅处理一些诸如更新会话成员(玩家退出或新玩家加入)的任务,否则,服务器一直呆在后台,通常它不知道客户端计算机间的通信。

由于服务器可能不存在或者至少没有直接参与游戏,因此有一个玩家的计算机可以被指派为游戏主机,用于处理一些像允许新玩家加入等的细节任务。

对等网具有简单的特点,只需要游戏玩家的计算机有网络连接和管理会话的方式。其主要缺点是可靠性差,当用户数量上升时,用于处理计算机间的通信量呈几何级数增长,最多用户数量取决于游戏本身和网络带宽。典型的对等网游戏玩家不超过2030个。

1.2.1.2.   客户机/服务器模式

一个客户机/服务器模式的游戏由一些单独的计算机通过一个集中的服务器连接起来。通常,一个4人的客户机/服务器游戏结构如下:

游戏的进行是通过每个玩家的计算机和服务器交流完成的。服务器负责在各个玩家的计算机间传递信息。例如,当一个玩家离开时,他发送信息给服务器,服务器再向其他所有玩家发送信息来更新游戏状态。服务器有以下责任:

¨         在会话中扮演集线器的角色。每个客户端计算机仅仅需要向服务器发送信息,服务器负责使所有玩家保持同步。这种安排能充分地减少网络信息堵塞,尤其是在大型游戏中。

¨         充当游戏主机。服务器承担在对等网中担任主机的计算机的类似的管理任务。

¨         支持游戏的多种特性。相对于管理游戏玩家,服务器通常要处理更多的任务。在许多游戏中,尤其是大型游戏中,服务器的许多时间被用于处理游戏世界的任务,而客户端计算机只负责绘制游戏界面。

一个客户机/服务器游戏通常将一个Lobby客户端程序运行于玩家的计算机上,Lobby客户端程序扮演同服务器交涉的任务,一旦游戏开始,服务器充当主机,在游戏中管理像允许新玩家加入等任务。

以下是客户机/服务器游戏的优点:

¨         非常高效,尤其是在大型游戏中。通常它优于对等网游戏,因为任意一个玩家只存在一条网络链路通向服务器,这种客户机/服务器拓扑结构更适合非常大型的多人游戏。

¨         游戏的运行不受客户端计算机处理能力的限制。你可以将游戏运行在一台处理能力非常强大的服务器上,客户端只需要绘制游戏界面。

你可以在集中的服务器上控制游戏的关键特性。例如,你可以经常更新游戏或修补游戏中的缺陷,仅需在服务器端作更改,从而避免了在众多的客户端更新程序。

然而,一旦你开发了一个对等网游戏,本质上就决定了游戏开发的结束。每个客户端很大程度上是自给自足的。而在客户机/服务器游戏中,你可以不断地在服务器上更新以便为玩家提供服务。你也必须提供和维持服务器及相关软件,通过网络链接持续地提供服务。在一个很大的多人游戏中,你必须使服务器提供不间断的或极少间断的服务,否则将冒着中断游戏而惹恼众多玩家的危险。

 

1.2.2.    DirectPlay的网络通信

DirectPlay的主要功能就是为你提供最有效的和可伸缩的通信支持以最大限度地隔离你的应用程序和底层的网络软硬件。如果你需要更新状态,你只需简单地调用相关的DirectPlay接口而不管使用何种网络协议。DirectPlay网络服务支持TCP/IPIPXModemSerial网络链接。

注意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.    DirectPlayLobby支持

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.

【上篇】
【下篇】

抱歉!评论已关闭.