• <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>

            天行健 君子當自強而不息

            使用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 閱讀(2270) 評論(0)  編輯 收藏 引用

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            久久精品国产久精国产思思 | 亚洲国产成人久久精品影视| jizzjizz国产精品久久| 亚洲国产二区三区久久| 久久99精品九九九久久婷婷| 婷婷伊人久久大香线蕉AV| 天天久久狠狠色综合| 波多野结衣AV无码久久一区| av午夜福利一片免费看久久| 久久久久久国产精品免费免费| 色欲av伊人久久大香线蕉影院| 亚洲国产精品久久66| 日韩精品久久无码人妻中文字幕| 91久久精品视频| 国产高潮国产高潮久久久| yy6080久久| 久久人人爽人人爽人人片AV麻豆| 久久99国产精品尤物| 久久青青国产| 久久WWW免费人成—看片| 久久国产精品-国产精品| 无码AV中文字幕久久专区| 欧美精品国产综合久久| 日本亚洲色大成网站WWW久久| 久久免费小视频| 久久青青草原国产精品免费| 久久精品人成免费| 性色欲网站人妻丰满中文久久不卡| 久久无码国产| 久久精品国产国产精品四凭| 久久国产一区二区| 久久精品国产91久久综合麻豆自制 | 久久国产精品久久精品国产| 久久精品国产亚洲AV嫖农村妇女 | 亚洲一区中文字幕久久| 久久亚洲国产欧洲精品一| 久久精品aⅴ无码中文字字幕不卡| 青青草原精品99久久精品66| 久久婷婷五月综合色奶水99啪| 久久久久久人妻无码| 精品999久久久久久中文字幕 |