青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

天行健 君子當自強而不息

使用DirectPlay進行網絡互聯(1)


了解網絡互聯

網絡是指多臺計算機互聯以進行數據傳輸及通信的系統。除了兩個或更多的計算機之外,網路還需要有網絡互聯軟件(或一個網路操作系統)、網絡適配器以及電纜。網絡適配器有各種形狀和大小,但是一般都采用調制解調器的形狀。實際上,調制解調器就是一個網路適配器,它能夠將一臺計算機通過世界上最大的網絡-- -互聯網連接到數百萬臺計算機上。

網路模型

網絡互聯模型有三種基本類型:服務器端、客戶端以及點對點。

使用服務器端模型,可以建立一個中央網絡互聯系統。其他計算機使用客戶端模型連接到服務器端后,就可以向服務器端發送數據以及從服務器端接收數據。客戶端沒有其他客戶端的信息,不直接與它們連接,客戶端都只知道服務器端的信息,而服務器端則擁有所有客戶端的信息以及適合這些客戶端之間的路由信息。服務器端和客戶端常常成對進行描述,即服務器端/客戶端(C/S)模型,但是在使用DirectPlay時,將二者分開是有必要的,因為服務器端和客戶端是由兩個獨立的組件組成的。

下圖演示了服務器端和客戶端模型之間的關系:



點對點(peer-to-peer)這種網絡互聯模型與服務器端模型或客戶端模型正好相反,計算機相互之間直接進行連接。每一臺新的計算機加入網絡會話中,都會建立一個新的連接,所以每臺計算機都能直接連接到其他計算機。連接到網絡的時段稱為會話,一次會話會有與之相關的屬性,如密碼、最大連接數等。

如下圖所示,一個4臺計算機的網絡擁有12個連接(每臺計算機與其他三臺計算機之間都有一個連接)。



游戲廳

可以將游戲廳服務器看作在線玩家的會議大廳,一個游戲廳允許所有玩家登錄、通信以及加入他們喜愛的一些游戲。一旦游戲廳服務器連滿了玩家,就停止對游戲廳的循環(這樣做是為了節省網絡帶寬)。網絡帶寬(network bandwidth)指的是一個網絡連接能夠輕松處理的數據量,高網絡帶寬連接能比低網路帶寬連接更快地處理大量的網絡數據。

響應時間和延遲

帶寬引出了兩個術語:響應時間和延遲。響應時間是完成一個操作所花的時間(越低越好)的量化。延遲是用來描述網絡通信的遲滯的術語,即數據從發送到它被接收到所花的時間。

低延遲表示網絡數據迅速地被接收。高延遲(最不希望出現的事情)表示網絡數據被延遲或者根本沒有被發送到。延遲是一個主要問題,特別是使用了互聯網時,就必須處理這個問題。

通信協議

網絡可以有各種方式進行相互通信,但是要連接到另一個,兩個系統都必須采用相同的協議。目前最流行的協議是TCP/IP協議(傳輸控制協議 /Internet協議),它被廣泛應用于互聯網。通信協議也被稱作服務器提供者,無論服務器提供者是一種諸如IPX、TCP/IP的協議,還是一種諸如調制解調器或者串行電纜的設備,都可以把它看作是網絡互聯的連接體。

TCP/IP協議是一種在網絡上傳輸數據包的方法。它將數據分割成很多小數據包,再加上發送方和接收方的地址以及包的數目,便于重組數據。

如下圖所示:

信息在傳輸過程中丟失(頻繁發生的事情),TCP/IP允許網絡重發數據包。當出現延遲時,這些數據包可能以錯誤的順序被接收,比如舊的數據包在新的數據包之后到達。好在無需擔心,因為TCP/IP會重發丟失的包并且重組無序的包。

尋址

在TCP/IP協議下,一個系統被分配一個由4個數字(0 -255之間)組成的網絡地址(IP地址),數字之間用點分割。一個IP地址就像下面這樣:

64.120.53.2

IP地址對我們來說不好辨認,但是網絡卻可以根據每一個數值成功地傳遞數據。稍加運算就會發現,這4個數字的組合總共可以提供4294967296個可能的地址。為了增加地址數量,網絡使用附加的稱為端口的地址值,數據被傳送到其上。

如果將IP地址比喻成一個郵政室(mailroom),那么該郵政室(IP地址)描述了與網絡相連的單個計算機系統,并且該計算機系統只被分配了惟一的IP 地址。在郵政室中有很多箱柜(端口),郵件被分類到其中。每一個箱柜(端口)屬于一個特定的工作人員(一個特定的應用程序)。一些應用程序可以擁有多個端口,數據只能被一個同相應IP地址以及相應端口所匹配的系統接收。一種稱為數據路由器(data router)的設備將接收到的網絡數據傳送到它所知道的系統,或者將數據傳送到另一個網絡連接。

下圖演示了路由的過程,說明網絡數據可以通過數據路由器進行傳遞。



DirectPlay概述

要在工程中使用DirectPlay,必須包含DPlay8.h和DPAddr.h,還要鏈接DPlayX.lib和 DXGuid.lib。

網絡對象

使用DirectPlay,需要使用前面提到的網絡模型:客戶端、服務器端和點對點。每一種網絡模型都有自己的接口對象,如下所示:

IDirectPlay8Client:客戶端網絡對象,連接到一個服務器端。
IDirectPlay8Server:服務器端網絡對象,連接多個客戶端。
IDirectPlay8Peer:   點對點網絡對象,連接客戶端其他客戶端。
IDirectPlay8Address:包含(以及構造)網絡地址的對象。

來看看IDirectPlay8Client的簡要說明:

Applications use the methods of the IDirectPlay8Client interface to create and manage client applications for client/server sessions.

The methods of the IDirectPlay8Client interface can be organized into the following groups.

Session Management Close
  Connect
  EnumHosts
  EnumServiceProviders
  GetApplicationDesc
  GetCaps
  GetSPCaps
  SetCaps
  SetSPCaps
Message Management GetSendQueueInfo
  Initialize
  ReturnBuffer
  Send
Client Information SetClientInfo
Server Information GetServerInfo
Miscellaneous CancelAsyncOperation
  RegisterLobby
  GetConnectionInfo
  GetServerAddress

來看看IDirectPlay8Server的簡要說明:

Applications use the methods of the IDirectPlay8Server interface to create and manage the server for a Microsoft® DirectPlay® client/server transport session

The methods of the IDirectPlay8Server interface can be organized into the following groups.

Session Management Close
  EnumServiceProviders
  GetApplicationDesc
  GetCaps
  GetSPCaps
  GetSendQueueInfo
  Host
  Initialize
  ReturnBuffer
  SendTo
  SetApplicationDesc
  SetCaps
  SetServerInfo
  SetSPCaps
Client Management DestroyClient
  GetClientInfo
  GetPlayerContext
Group Management AddPlayerToGroup
  CreateGroup
  DestroyGroup
  EnumPlayersAndGroups
  EnumGroupMembers
  GetGroupContext
  GetGroupInfo
  RemovePlayerFromGroup
  SetGroupInfo
Miscellaneous CancelAsyncOperation
  GetClientAddress
  GetConnectionInfo
  GetLocalHostAddresses
  RegisterLobby


要連接到遠程網絡系統(或主持一次會話),需要使用 IDirectPlay8Address構造一個網絡地址,IDirectPlay8Address的惟一用途就是構造并包含單個網絡地址。會話指的是主持或加入一個可連接網路系統的時段,終止連接時,會話也就終止了。要主持游戲會話或連接到遠程系統,首先需要創建網絡對象并給它分配地址。要主持游戲會話,只需要等待其他系統(也就是使用這些系統的人)連接上來即可。連接建立之后,系統就可以和遠程系統開始相互傳輸游戲相關的網絡消息, DirectPlay把這些遠程系統稱作玩家。

將玩家進行分組

在DirectPlay 術語中,“玩家”是指通過網絡連接到其他計算機的單個連接(通常是一個游戲玩家)。一臺計算機可以有多個玩家,但一般只有一個。實際上,服務器端也被標識為玩家,以便于識別。每一個玩家都會接收到一個標識號碼(玩家ID),系統使用這個標識號碼來將消息傳遞到每個玩家。這些號碼是追蹤玩家的惟一可信任方法,所以需要讓程序對它們進行相應地處理。

對于大型游戲,可能有成千上萬已連接的玩家。為了在游戲中更好地處理這些玩家,可以將一些或所有玩家編制到一些組中。采用組的概念可以減少編程的繁瑣,主要原因在于可以將一個游戲區域(比如一張地圖或某個級別)中的多個玩家劃分為一組,并且一次向整個組發送網絡數據,而不是單獨發送給各個玩家。使用組還有很多其他原因,但這個原因是最主要的。一個組中包含多少玩家以及創建多少個組完全沒有限制,組也可以屬于其他組。

如下圖所示:



帶消息的網絡互聯

消息是分了類的數據包,其中包含簡單的結構。每一個消息都有特定的含義,且都有一個與之對應的宏,而且還和所使用的網絡模型有關。要接收消息,網絡對象必須給自己指定一個回調函數,以便于每次消息到達時進行調用。為了確保得到平滑的數據流,該函數根據數據類型進行分析并盡快地返回。發送消息,需要使用各個網絡對象的發送函數。這些函數易于使用,并且提供了許多發送選項,包括保證發送(guaranteed delivery)、安全編碼(secure encryption)以及同步或異步發送。

同步與異步

DirectPlay提供的第一個發送選項是同步或異步發送消息的功能,也就是說系統在發出發送數據指定之后交回控制權(異步),還是直到所有的數據都成功發送之后再交回控制權(同步)。很多時候使用異步方法,因為它不會像同步方法那樣阻塞系統。

安全性

要引起注意的是任何時候都可能有人在截取并記錄游戲的網絡數據。因此,就需要采用一種安全的方式編碼消息數據,使得那些解碼的黑客非常難讀懂先前的信息。使用安全網絡發送的不好之處在于它會稍稍減慢系統速度,因為必須在數據發送之前先編碼信息,然后在接收到之后進行解碼。

保證發送

就像一些快遞公司保證送到包裹一樣, DirectPlay也能保證送到消息。可以將一組信息標識為保證的,其余確定的就是DirectPlay將一直執行發送操作直到發送成功,以保證將其發送到目標地址(除非斷線),使用保證發送是通過在調用函數時指定一個惟一的標志來實現的。保證發送的不好之處在于速度,保證發送在實際的游戲狀況下非常慢,游戲采用UDP(用戶數據報協議)發送方法,就不用關心數據是否被接收到(和TCP發送方法相反,它保證數據的發送)。

節流

有時系統會因為試圖處理流數據而過載,盡管這樣, DirectPlay有一個內置的消息節流器,它丟棄了發送隊列中低優先級的信息。

下圖有助于更形象地理解節流機制的概念,想象一隊人在鎮上熱鬧的夜總會前等待,如果每個顧客表示一條消息,那么當非常忙的時候,保鏢(節流機制)就必須拒絕那些隊列中較不重要的顧客。



使用GUID識別應用程序

如何從眾多的網絡應用程序中區分出自己的網絡應用程序呢?解決這個問題的方法就是給自己的應用程序指定一個惟一的號碼,并且只允許使用相同號碼的應用程序相互進行連接。這個特殊的號碼就是 Windows程序員熟悉的GUID(全局惟一標識符)。創建應用程序之前,花一點時間給它設置一個惟一的GUID,并且保證所有通過網絡進行連接的該應用程序使用相同的GUID。

初始化網絡對象

無論是服務器端、客戶端還是單點對象,使用DirectPlay的第一步都是創建網絡對象。要初始化每一個網絡模型接口,必須使用CoCreateInstance函數,可能用到的類ID和引用標識符如下所示:

CLSID_DirectPlay8Address  IID_IDirectPlay8Address
CLSID_DirectPlay8Client IID_IDirectPlay8Client
CLSID_DirectPlay8Peer IID_IDirectPlay8Peer
CLSID_DirectPlay8Server IID_IDirectPlay8Server

無論創建的是什么網絡對象(客戶端、單點或服務器端),都需要創建與之匹配的網絡回調函數,在網絡消息被接收到的時候會調用該回調函數。

該回調函數使用說明如下:

PFNDPNMESSAGEHANDLER is an application-defined callback function used by the IDirectPlay8Peer, IDirectPlay8Client, and IDirectPlay8Server IDirectPlay8LobbyClient and IDirectPlay8LobbiedApplication interfaces to process messages.

typedef HRESULT (WINAPI *PFNDPNMESSAGEHANDLER)( 
PVOID pvUserContext,
DWORD dwMessageType,
PVOID pMessage
);

Parameters

pvUserContext
Pointer to the application-defined structure that will be passed to this callback function. This is defined in the pvUserContext parameter of the Initialize method.
dwMessageType
One of the following message types that are generated by the IDirectPlay8Peer, IDirectPlay8Client, and IDirectPlay8Server interfaces. Each interface uses a different subset of the available messages. Refer to the interface documentation for details.
  • DPN_MSGID_ADD_PLAYER_TO_GROUP
  • DPN_MSGID_ASYNC_OP_COMPLETE
  • DPN_MSGID_CLIENT_INFO
  • DPN_MSGID_CONNECT_COMPLETE
  • DPN_MSGID_CREATE_GROUP
  • DPN_MSGID_CREATE_PLAYER
  • DPN_MSGID_DESTROY_GROUP
  • DPN_MSGID_DESTROY_PLAYER
  • DPN_MSGID_ENUM_HOSTS_QUERY
  • DPN_MSGID_ENUM_HOSTS_RESPONSE
  • DPN_MSGID_GROUP_INFO
  • DPN_MSGID_HOST_MIGRATE
  • DPN_MSGID_INDICATE_CONNECT
  • DPN_MSGID_INDICATED_CONNECT_ABORTED
  • DPN_MSGID_PEER_INFO
  • DPN_MSGID_RECEIVE
  • DPN_MSGID_REMOVE_PLAYER_FROM_GROUP
  • DPN_MSGID_RETURN_BUFFER
  • DPN_MSGID_SEND_COMPLETE
  • DPN_MSGID_SERVER_INFO
  • DPN_MSGID_TERMINATE_SESSION

    Additionally, if the application supports Microsoft® DirectPlay® lobby functionality, this parameter can specify one of the following message types that are generated by the IDirectPlay8LobbyClient and IDirectPlay8LobbiedApplication interfaces. Each interface uses a different subset of the available messages. Refer to the interface documentation for details.

  • DPL_MSGID_CONNECT
  • DPL_MSGID_CONNECTION_SETTINGS
  • DPL_MSGID_DISCONNECT
  • DPL_MSGID_RECEIVE
  • DPL_MSGID_SESSION_STATUS
pMessage
Structure containing message information.

Return Values

See the documentation for the individual messages for appropriate return values. Unless otherwise noted, this function should return S_OK.

Remarks

This function must be threadsafe because it might be called reentrantly through multiple threads.

Callback messages from the same player are serialized. Once you receive a message from a player, you will not receive another until you have handled the first message, and the callback function has returned.

The message structures have the same name as the message type except the "DPN_MSGID" is replaces with "DPNMSG". For example, the DPN_MSGID_CONNECTION_TERMINATED message type uses the DPNMSG_CONNECTION_TERMINATED message structure to convey the actual message information.

When implementing this callback function, first look at the message type returned in the dwMessageType parameter and then cast the message structure (pMessage) to that type to obtain message information. Some messages don't have a defined structure because they have no parameters. For these messages, the pMessage parameter is NULL.


要初始化網絡對象,需要調用Initialize函數。

Registers an entry point in the client's code that receives the messages from the IDirectPlay8Client interface and from the server. This method must be called before calling any other methods of this interface.

HRESULT Initialize(
PVOID const
pvUserContext,
const PFNDPNMESSAGEHANDLER pfn,
const DWORD dwFlags
);

Parameters

pvUserContext
[in] Pointer to the user-provided context value in calls to the message handler. Providing a user-context value can be useful to differentiate messages coming from multiple interfaces to a common message handler.
pfn
[in] Pointer to a PFNDPNMESSAGEHANDLER callback function that receives all messages from the server, and receives indications of session changes from the IDirectPlay8Client interface.
dwFlags
[in] You may specify the following flag.
DPNINITIALIZE_DISABLEPARAMVAL
Disable parameter validation for the current object.

Return Values

Returns S_OK if successful, or one of the following error values.

DPNERR_INVALIDFLAGS
DPNERR_INVALIDPARAM
 

Remarks

This is the first method you should call after using CoCreateInstance to obtain the IDirectPlay8Client interface.


下面這個函數創建了DirectPlay Client對象并且初始化該對象。
 
//--------------------------------------------------------------------------------
// Create DirectPlay Client and initialize it.
//--------------------------------------------------------------------------------
BOOL Init_DirectPlay_Client()
{
    
// create DirectPlay client component
    if(FAILED(CoCreateInstance(CLSID_DirectPlay8Client, NULL, CLSCTX_INPROC, IID_IDirectPlay8Client, 
                               (
void**)&g_dp_client)))
        
return FALSE;

    
// Assign a message handler to network component
    //
    // Registers an entry point in the client's code that receives the messages from the IDirectPlay8Client
    // interface and from the server.
    if(FAILED(g_dp_client->Initialize(NULL, Net_Msg_Handle, 0)))
        
return FALSE;

    
return TRUE;
}

指定設備

雖然已經選擇了一個服務提供者,但系統中可能有一個以上的設備使用它。當一個網絡適配器和調制解調器都連接到互聯網,并且都使用TCP/IP協議時,就會出現這種情況,這是就必須對設備進行枚舉并進行選擇。

枚舉函數獲取了所有可用的服務提供者,并將它們以可用的方式組織在一起。DirectPlay枚舉和典型的 Windwos枚舉方法有所不同,在查詢過程中找到實例對象并不是每次都調用回調函數,而是從包含了以數組形式存儲的每個服務提供者的緩沖區中尋找,如下圖所示:



處理枚舉可以使用EnumServiceProviders函數。

Enumerates the registered service providers available to the application.

HRESULT EnumServiceProviders(
const GUID *const
pguidServiceProvider,
const GUID *const pguidApplication,
DPN_SERVICE_PROVIDER_INFO *const pSPInfoBuffer,
PDWORD const pcbEnumData,
PDWORD const pcReturned,
const DWORD dwFlags
);

Parameters

pguidServiceProvider
[in] Pointer to a variable of type GUID that specifies a service provider. This optional parameter forces the enumeration of subdevices for the specified service provider. You should normally set this value to NULL, to enumerate all available service providers.
pguidApplication
[in] Pointer to a variable of type GUID that specifies an application. If a pointer is passed in this parameter, only service providers who can be connected to the application are enumerated. You can also pass NULL to enumerate the registered service providers for the system.
pSPInfoBuffer
[out] Pointer to an array of DPN_SERVICE_PROVIDER_INFO structures that will be filled with service provider information.
pcbEnumData
[out] Pointer to DWORD, which is filled with the size of the pSPInfoBuffer array, in bytes, if the buffer is too small.
pcReturned
[out] Pointer to a variable of type DWORD that specifies the number of DPN_SERVICE_PROVIDER_INFO structures returned in the pSPInfoBuffer array.
dwFlags
[in] The following flag can be specified.
DPNENUMSERVICEPROVIDERS_ALL
Enumerates all the registered service providers for the system, including those that are not available to the application or do not have devices installed.

Return Values

Returns S_OK if successful, or one of the following error values.

DPNERR_BUFFERTOOSMALL
DPNERR_INVALIDPARAM
 

Remarks

Call this method initially by specifying NULL in the pguidServiceProvider parameter to determine the base service providers available to the system. Specific devices for a service provider can be obtained by passing a pointer to a service provider GUID in the pguidServiceProvider. This is useful, for example, when using the Modem Connection for Microsoft® DirectPlay® service provider. You can choose among different modems for dialing out and select specific modems for hosting.

If the pEnumData buffer is not big enough to hold the requested service provider information, the method returns DPNERR_BUFFERTOOSMALL and the cbEnumData parameter contains the required buffer size. Typically, the best strategy is to call the method once with a zero-length buffer to determine the required size. Then call the method again with the appropriate-sized buffer.

Normally, this method will return only those service providers that can be used by the application. For example, if the IPX networking protocol is not installed, DirectPlay will not return the IPX service provider. To have DirectPlay return all service providers, even those that cannot be used by the application, set the DPNENUMSERVICEPROVIDERS_ALL flag in dwFlags.


pSPInfoBuffer是指向DPN_SERVICE_PROVIDER_INFO結構體數組的指針,該函數將用枚舉的信息填充該結構體。

Used when enumerating information for a specific service provider.

typedef struct _DPN_SERVICE_PROVIDER_INFO{
DWORD dwFlags;
GUID guid;
WCHAR* pwszName;
PVOID pvReserved;
DWORD dwReserved;
} DPN_SERVICE_PROVIDER_INFO, *PDPN_SERVICE_PROVIDER_INFO;

Members

dwFlags
Reserved. Must be 0.
guid
GUID for the service provider.
pwszName
Name of the service provider.
pvReserved
Reserved. Must be 0.
dwReserved
Reserved. Must be 0.

下面這個函數演示了如何枚舉系統中的服務提供者:
 
HWND g_hwnd;    // window handles

IDirectPlay8Client* g_dp_client;    
// directplay client

// service provider list, used when enumerating information for a specifice service provider.
DPN_SERVICE_PROVIDER_INFO*  g_sp_list;  

DWORD g_sp_number;      
// service provider number

//--------------------------------------------------------------------------------
// Enumerate all available service provider.
//--------------------------------------------------------------------------------
void Enum_Service_Provider()
{
    
// return is no server object
    if(g_dp_client == NULL)
        
return;

    
// get a handler to the list box
    HWND listbox = GetDlgItem(g_hwnd, IDC_SERVICE_PROVIDERS);

    
// clear the list box
    SendMessage(listbox, LB_RESETCONTENT, 0, 0);

    
// release service provider list memory
    delete[] g_sp_list;
    g_sp_list = NULL;
    
    
// query the required size of the data buffer
    
    DWORD sp_list_size = 0;

    
// enumerates the registerd service providers available to the application
    HRESULT rv = g_dp_client->EnumServiceProviders(NULL, NULL, g_sp_list, &sp_list_size, &g_sp_number, 0);

    
if(rv != DPNERR_BUFFERTOOSMALL)
        
return;

    
// allocate a buffer
    if((g_sp_list = (DPN_SERVICE_PROVIDER_INFO*) new BYTE[sp_list_size]) == NULL)
        
return;

    
// enumerate again
    if(SUCCEEDED(g_dp_client->EnumServiceProviders(NULL, NULL, g_sp_list, &sp_list_size, &g_sp_number, 0)))
    {
        
// enumeration is complete, scan through entries.

        
char sp_name[1024];

        DPN_SERVICE_PROVIDER_INFO* sp_ptr = g_sp_list;
        
        
for(DWORD i = 0; i < g_sp_number; i++)
        {
            
// convert wide string into multi-byte string
            wcstombs(sp_name, sp_ptr->pwszName, 1024);

            
// Add the service provider into box
            //
            // An application sends an LB_ADDSTRING message to add a string to a list box. 
            // If the list box does not have the LBS_SORT style, the string is added to the end of the list. 
            // Otherwise, the string is inserted into the list and the list is sorted.  
            SendMessage(listbox, LB_ADDSTRING, 0, (LPARAM)sp_name);

            
// go to next service provider in buffer
            sp_ptr++;
        }
    }
}

下面給出一個完整實例來運用剛才介紹的知識,由于DirectX9 SDK已不包含DirectPlay,所以需要安裝DirectX8 SDK,并在Visual Studio中進行相應設置。

點擊下載源碼和工程

 
/***************************************************************************************
PURPOSE:
    Enum Network Adapters Demo
 ***************************************************************************************/


#include <windows.h>
#include <dplay8.h>
#include <dpaddr.h>
#include "resource.h"

#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "dplayx.lib")

#pragma warning(disable : 4996)

#define Safe_Release(p) if((p)) (p)->Release();

// window handles, class.
HWND g_hwnd;
char g_class_name[] = "EnumClass";

IDirectPlay8Client* g_dp_client;    
// directplay client

// service provider list, used when enumerating information for a specifice service provider.
DPN_SERVICE_PROVIDER_INFO*  g_sp_list;  

DWORD g_sp_number;      
// service provider number

//----------------------------------------------------------------------------------------
// callback function that receives all messages from the server, and receives indications 
// of session changes from the IDirectPlay8Client interface. 
//----------------------------------------------------------------------------------------
HRESULT WINAPI Net_Msg_Handle(PVOID user_context, DWORD message_id, PVOID msg_buffer)
{
    
// return S_OK to signify the message was handled OK.
    return S_OK;
}

//--------------------------------------------------------------------------------
// Create DirectPlay Client and initialize it.
//--------------------------------------------------------------------------------
BOOL Init_DirectPlay_Client()
{
    
// create DirectPlay client component
    if(FAILED(CoCreateInstance(CLSID_DirectPlay8Client, NULL, CLSCTX_INPROC, IID_IDirectPlay8Client, 
                               (
void**)&g_dp_client)))
        
return FALSE;

    
// Assign a message handler to network component
    //
    // Registers an entry point in the client's code that receives the messages from the IDirectPlay8Client
    // interface and from the server.
    if(FAILED(g_dp_client->Initialize(NULL, Net_Msg_Handle, 0)))
        
return FALSE;

    
return TRUE;
}

//--------------------------------------------------------------------------------
// Enumerate all available service provider.
//--------------------------------------------------------------------------------
void Enum_Service_Provider()
{
    
// return is no server object
    if(g_dp_client == NULL)
        
return;

    
// get a handler to the list box
    HWND listbox = GetDlgItem(g_hwnd, IDC_SERVICE_PROVIDERS);

    
// clear the list box
    SendMessage(listbox, LB_RESETCONTENT, 0, 0);

    
// release service provider list memory
    delete[] g_sp_list;
    g_sp_list = NULL;
    
    
// query the required size of the data buffer
    
    DWORD sp_list_size = 0;

    
// enumerates the registerd service providers available to the application
    HRESULT rv = g_dp_client->EnumServiceProviders(NULL, NULL, g_sp_list, &sp_list_size, &g_sp_number, 0);

    
if(rv != DPNERR_BUFFERTOOSMALL)
        
return;

    
// allocate a buffer
    if((g_sp_list = (DPN_SERVICE_PROVIDER_INFO*) new BYTE[sp_list_size]) == NULL)
        
return;

    
// enumerate again
    if(SUCCEEDED(g_dp_client->EnumServiceProviders(NULL, NULL, g_sp_list, &sp_list_size, &g_sp_number, 0)))
    {
        
// enumeration is complete, scan through entries.

        
char sp_name[1024];

        DPN_SERVICE_PROVIDER_INFO* sp_ptr = g_sp_list;
        
        
for(DWORD i = 0; i < g_sp_number; i++)
        {
            
// convert wide string into multi-byte string
            wcstombs(sp_name, sp_ptr->pwszName, 1024);

            
// Add the service provider into box
            //
            // An application sends an LB_ADDSTRING message to add a string to a list box. 
            // If the list box does not have the LBS_SORT style, the string is added to the end of the list. 
            // Otherwise, the string is inserted into the list and the list is sorted.  
            SendMessage(listbox, LB_ADDSTRING, 0, (LPARAM)sp_name);

            
// go to next service provider in buffer
            sp_ptr++;
        }
    }
}

//--------------------------------------------------------------------------------
// Enumerate all adapters which specified by sp_guid.
//--------------------------------------------------------------------------------
void Enum_Adapters(GUID* sp_guid)
{
    
// return if no server object or GUID
    if(g_dp_client == NULL || sp_guid == NULL)
        
return;

    
// get a handle of the list box
    HWND listbox = GetDlgItem(g_hwnd, IDC_ADAPTERS);

    
// clear the list box
    SendMessage(listbox, LB_RESETCONTENT, 0, 0);

    DPN_SERVICE_PROVIDER_INFO* adapter_list = NULL;
    DWORD adapter_number = 0;
    DWORD adapter_list_size = 0;

    
// query the required size of the data buffer
    HRESULT rv = g_dp_client->EnumServiceProviders(sp_guid, NULL, adapter_list, &adapter_list_size, &adapter_number, 0);

    
if(rv != DPNERR_BUFFERTOOSMALL)
        
return;

    
// allocate a buffer
    if((adapter_list = (DPN_SERVICE_PROVIDER_INFO*) new BYTE[adapter_list_size]) == NULL)
        
return;

    
// enumerate again
    if(SUCCEEDED(g_dp_client->EnumServiceProviders(sp_guid, NULL, adapter_list, &adapter_list_size, &adapter_number, 0)))
    {
        
char adapter_name[1024];

        
// enumeration is complete, scan through entries.
        DPN_SERVICE_PROVIDER_INFO* adapter_ptr = adapter_list;

        
for(DWORD i = 0; i < adapter_number; i++)
        {
            
// convert wide string into multi-byte string
            wcstombs(adapter_name, adapter_ptr->pwszName, 1024);

            
// add the adapter name int listbox
            SendMessage(listbox, LB_ADDSTRING, 0, (LPARAM)adapter_name);

            
// go to next servicec provider
            adapter_ptr++;
        }
    }
    
    
// delete the list memory resources
    delete[] adapter_list;
}

//--------------------------------------------------------------------------------
// Release all resource which allocated for DirectPlay.
//--------------------------------------------------------------------------------
void Release_DirectPlay()
{
    
// release client service provider list memory
    delete[] g_sp_list;
    g_sp_list = NULL;
    
    g_sp_number = 0;

    
// release client component
    if(g_dp_client != NULL)
    {
        
// Closes the open connection to a session. This method must be called on any object that is successfully 
        // initialized with a call to the IDirectPlay8Client::Initialize method.
        g_dp_client->Close(0);

        g_dp_client->Release();

        g_dp_client = NULL;
    }
}

//--------------------------------------------------------------------------------
// Window procedure.
//--------------------------------------------------------------------------------
long WINAPI Window_Proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    
switch(msg)
    {
    
case WM_COMMAND:
        
// The WM_COMMAND message is sent when the user selects a command item from a menu, when a control sends a 
        // notification message to its parent window, or when an accelerator keystroke is translated. 
        //
        // wParam:
        //    The high-order word specifies the notification code if the message is from a control. 
        //    If the message is from an accelerator, this value is 1. If the message is from a menu, this value is zero. 
        //    The low-order word specifies the identifier of the menu item, control, or accelerator. 
        //
        // lParam:
        //     Handle to the control sending the message if the message is from a control. 
        //     Otherwise, this parameter is NULL. 

        // An application sends the LBN_SELCHANGE notification message when the selection in a list box is about to 
        // change. The parent window of the list box receives this notification message through the WM_COMMAND message. 
        if(LOWORD(wParam) == IDC_SERVICE_PROVIDERS && HIWORD(wParam) == LBN_SELCHANGE)
        {
            
// Get the selection from the list
            //
            // Send an LB_GETCURSEL message to retrieve the index of the currently selected item, 
            // if any, in a single-selection list box.         
            unsigned int selected = (unsigned int) SendMessage(GetDlgItem(hwnd, IDC_SERVICE_PROVIDERS), LB_GETCURSEL, 0, 0);

            
// enumerate the adapters
            DPN_SERVICE_PROVIDER_INFO* sp_ptr = g_sp_list;
            sp_ptr += selected;
            
            
// enumerate adapter with specified guid
            Enum_Adapters(&sp_ptr->guid);
        }

        
return 0;

    
case WM_DESTROY:
        Release_DirectPlay();
        PostQuitMessage(0);
        
return 0;
    }

    
return (long) DefWindowProc(hwnd, msg, wParam, lParam);
}

//--------------------------------------------------------------------------------
// Main function, routine entry.
//--------------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE inst, HINSTANCE, LPSTR cmd_line, int cmd_show)
{
    WNDCLASS            win_class;
    MSG                 msg;    

    
// create window class and register it
    win_class.style         = CS_HREDRAW | CS_VREDRAW;
    win_class.lpfnWndProc   = Window_Proc;
    win_class.cbClsExtra    = 0;
    win_class.cbWndExtra    = DLGWINDOWEXTRA;
    win_class.hInstance     = inst;
    win_class.hIcon         = LoadIcon(inst, IDI_APPLICATION);
    win_class.hCursor       = LoadCursor(NULL, IDC_ARROW);
    win_class.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
    win_class.lpszMenuName  = NULL;
    win_class.lpszClassName = g_class_name;    

    
if(! RegisterClass(&win_class))
        
return FALSE;

    
// create the main window
    g_hwnd = CreateDialog(inst, MAKEINTRESOURCE(IDD_ENUM), 0, NULL);

    ShowWindow(g_hwnd, cmd_show);
    UpdateWindow(g_hwnd);

    
// initialize COM
    //
    // initialize the COM library on the current thread and identifies the concurrency model as single-thread
    // apartment (STA).
    CoInitialize(0);

    
// Initialzie DirectPlay and enumerate service providers.
    if(! Init_DirectPlay_Client())
    {
        MessageBox(NULL, "Error initializing DirectPlay.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
        
return 0;
    }

    
// enumerate all available service provider
    Enum_Service_Provider();

    
// start message pump, waiting for signal to quit.
    ZeroMemory(&msg, sizeof(MSG));

    
while(msg.message != WM_QUIT)
    {
        
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);            
        }             
    }

    UnregisterClass(g_class_name, inst);

    
// release COM system
    //
    // Closes the COM library on the current thread, unloads all DLLs loaded by the thread, frees any other
    // resources that the thread maintains, and forces all RPC connections on the thread to close.
    CoUninitialize();
    
    
return (int) msg.wParam;
}
 

運行截圖:



posted on 2007-08-06 19:50 lovedday 閱讀(2283) 評論(0)  編輯 收藏 引用


只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


公告

導航

統計

常用鏈接

隨筆分類(178)

3D游戲編程相關鏈接

搜索

最新評論

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            久久精品国产清高在天天线| 欧美一区二区黄色| 亚洲无人区一区| 亚洲精品美女在线| 亚洲精品久久久久久久久久久久 | 久久久人成影片一区二区三区| 亚洲欧美国产制服动漫| 先锋影音国产一区| 久久精品国产清高在天天线| 久久久综合网| 欧美久色视频| 欧美日韩国产一级片| 国产精品久久久久久久久久免费看 | 国产日韩欧美成人| 激情偷拍久久| 亚洲精品在线观看视频| 亚洲一区二区三区777| 性久久久久久久久| 蜜桃精品久久久久久久免费影院| 免费成人在线视频网站| 亚洲另类自拍| 久久黄色网页| 欧美日韩国产成人高清视频| 国产欧美一区二区精品婷婷 | 亚洲电影免费观看高清完整版| 亚洲国产精品国自产拍av秋霞| 日韩午夜在线观看视频| 亚洲欧美日韩国产| 欧美国产日本| 亚洲欧美日韩中文在线制服| 美女视频黄免费的久久| 国产伦精品一区二区三区照片91 | 亚洲另类在线视频| 久久国产直播| 亚洲三级性片| 一区二区三区日韩精品| 免费人成网站在线观看欧美高清| 国产精品电影在线观看| 亚洲国产网站| 久久精品国产久精国产爱| 一本色道精品久久一区二区三区| 久久久精品性| 国产日韩欧美中文| 亚洲亚洲精品在线观看 | 国产一区视频网站| 中文欧美字幕免费| 亚洲人体影院| 欧美韩日一区二区| 亚洲人成人99网站| 欧美激情精品久久久久久变态 | 国产精品美女久久久久久2018| 亚洲国产日韩欧美| 久热国产精品视频| 久久精品av麻豆的观看方式| 国产精品视频免费一区| 夜夜嗨网站十八久久| 亚洲国产成人久久综合| 蜜臀av在线播放一区二区三区| 国产欧美不卡| 欧美一级电影久久| 亚洲欧美在线免费| 国产精品一区二区三区成人| 亚洲视频精品在线| 一区二区电影免费观看| 国产精品www| 中文无字幕一区二区三区| 日韩视频一区二区| 国产精品99免费看| 欧美在线观看网址综合| 午夜精品久久久久久久蜜桃app| 国产精品久久久一区麻豆最新章节| 亚洲视频在线播放| 亚洲视频第一页| 国产精品欧美日韩一区| 亚洲视频一区| 国产精品日韩在线播放| 欧美一级黄色网| 久久成人免费日本黄色| 在线成人欧美| 亚洲国产精品激情在线观看| 欧美日韩高清区| 性视频1819p久久| 久久精品国产欧美激情| 亚洲黄色成人网| 夜夜精品视频| 黑人巨大精品欧美黑白配亚洲| 美日韩精品视频免费看| 欧美国产精品v| 亚洲欧美精品伊人久久| 欧美一区二区三区日韩| 最新精品在线| 亚洲一区二区三区中文字幕| 黄网站色欧美视频| 日韩一本二本av| 国语自产精品视频在线看8查询8 | 在线精品国产成人综合| 亚洲精品在线视频观看| 国产伦精品免费视频| 欧美国产亚洲精品久久久8v| 欧美日韩在线直播| 男女激情久久| 欧美性色综合| 欧美韩日高清| 国产精品每日更新在线播放网址| 可以看av的网站久久看| 欧美日韩中文字幕在线视频| 久久久蜜桃一区二区人| 欧美乱人伦中文字幕在线| 久久国产精品99国产精| 欧美巨乳在线观看| 欧美成人免费视频| 国产欧美日韩综合| 亚洲靠逼com| 亚洲国产精品一区在线观看不卡| 在线视频免费在线观看一区二区| 激情懂色av一区av二区av| 一区二区三区 在线观看视频| 在线精品一区| 午夜精品久久久久久久99樱桃| 一区二区欧美激情| 久色婷婷小香蕉久久| 欧美专区在线观看一区| 欧美天天视频| 亚洲精品影视| 艳妇臀荡乳欲伦亚洲一区| 久久免费视频在线| 久久久亚洲国产美女国产盗摄| 欧美三级视频在线播放| 亚洲国产日韩欧美综合久久| 精品成人a区在线观看| 小处雏高清一区二区三区| 亚洲欧美日本另类| 国产精品久久久91| 日韩亚洲欧美综合| 亚洲无限av看| 欧美日韩一区在线播放| 9国产精品视频| 亚洲成人在线视频播放| 欧美日本在线看| 亚洲高清不卡在线观看| 在线观看日韩av| 久久免费视频网站| 欧美 日韩 国产在线| 亚洲第一网站| 男人的天堂成人在线| 亚洲激情电影在线| 亚洲欧洲一区二区天堂久久| 免费国产自线拍一欧美视频| 欧美成人日韩| 亚洲美女淫视频| 欧美日韩视频专区在线播放 | 欧美综合77777色婷婷| 久久久国产精品一区二区三区| 国产亚洲精品一区二555| 欧美亚洲综合另类| 久久夜色精品国产噜噜av| 韩国美女久久| 免费试看一区| 日韩一本二本av| 久久av红桃一区二区小说| 国产一区二区福利| 老色批av在线精品| 99国产精品| 久久超碰97中文字幕| 亚洲第一精品电影| 欧美午夜无遮挡| 久久都是精品| 亚洲国产精品精华液网站| 亚洲一级免费视频| 国产主播一区二区三区| 欧美freesex交免费视频| 一本久久a久久免费精品不卡| 欧美在线免费观看| 亚洲国产一区视频| 国产精品久久久久久久久动漫 | 久久久国产精品亚洲一区| 欧美好骚综合网| 亚洲香蕉网站| 亚洲片在线观看| 国产嫩草一区二区三区在线观看| 久久精品国产免费| 亚洲天堂男人| 欧美国产精品v| 性做久久久久久久久| 亚洲国产精品第一区二区| 国产精品xxxav免费视频| 久久久久免费| 亚洲一区二区视频在线| 亚洲国产裸拍裸体视频在线观看乱了| 午夜精品久久久久| 日韩一级在线| 影音先锋国产精品| 国产欧美精品一区aⅴ影院| 欧美精品一区二区在线播放| 欧美在线91| 亚洲欧美日韩精品久久久久| 亚洲欧洲免费视频| 欧美高清视频在线观看| 欧美在线免费观看亚洲| 亚洲自拍啪啪|