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

天行健 君子當(dāng)自強(qiáng)而不息

使用DirectPlay進(jìn)行網(wǎng)絡(luò)互聯(lián)(4)


本篇是使用DirectPlay進(jìn)行網(wǎng)絡(luò)互聯(lián)(3)的續(xù)篇。



客戶端的處理

客戶端并不像服務(wù)器端那么復(fù)雜,它通常只使用兩種消息,即接收數(shù)據(jù)和終止會話消息,以及需要連接和保持單一連接(服務(wù)器端)。另外最主要的就是客戶端應(yīng)用程序必須指定其玩家的位置,以便主機(jī)能夠檢索它們。設(shè)置玩家的信息是通過首先將相關(guān)數(shù)據(jù)填入一個DPN_PLAYER_INFO結(jié)構(gòu)體,然后再調(diào)用 IDirectPlay8Client:: SetClientInfo函數(shù)來實現(xiàn)。

Sets the static settings of a client with an application. Call this method before connecting to relay basic player information to the application. Once the client successfully connects with the application, the server can retrieve information obtained through this method by calling the IDirectPlay8Server::GetClientInfo method.

HRESULT SetClientInfo(
const DPN_PLAYER_INFO *const
pdpnPlayerInfo,
PVOID const pvAsyncContext,
DPNHANDLE *const phAsyncHandle,
const DWORD dwFlags
);

Parameters

pdpnPlayerInfo
[in] Pointer to a DPN_PLAYER_INFO structure that contains the client information to set.
pvAsyncContext
[in] Pointer to the user-supplied context, which is returned in the pvUserContext member of the DPN_MSGID_ASYNC_OP_COMPLETE system message.
phAsyncHandle
[in,out] A DPNHANDLE. A value will be returned. However, Microsoft® DirectPlay® 8.1 does not permit cancellation of this operation, so the value cannot be used.
dwFlags
[in] Flag that controls how this method is processed. The following flag can be set for this method.
DPNSETCLIENTINFO_SYNC
Causes the method to process synchronously.

Return Values

Returns S_OK if this method is processed synchronously and is successful. If the request is processed asynchronously, S_OK can return if the method is instantly processed. By default, this method is run asynchronously and generally returns DPNSUCCESS_PENDING or one of the following error values.

DPNERR_NOCONNECTION
DPNERR_INVALIDFLAGS
DPNERR_INVALIDPARAM
 

Remarks

This method can be called at any time during the session.

The dwPlayerFlags member of the DPN_PLAYER_INFO structure must be set to zero.

Transmission of nonstatic information should be handled with the IDirectPlay8Client::Send method because of the high cost of using the IDirectPlay8Client::SetClientInfo method.

You can modify the client information with this method after connecting to the application. Calling this method after connection generates a DPN_MSGID_CLIENT_INFO system message to all players, informing them that data has been updated.


服務(wù)器端和客戶端都使用相同的應(yīng)用程序GUID,這樣它們才能相互識別。網(wǎng)絡(luò)應(yīng)用程序不能連接的主要原因就是沒有這樣做,所以一定要確保使用相同的應(yīng)用程序GUID。

設(shè)置好客戶端的信息后,需要與服務(wù)器端建立連接,可以調(diào)用 IDirectPlay6Client::Connect來實現(xiàn)。

Establishes the connection to the server. After a connection is established, the communication channel on the interface is open and the application should expect messages to arrive immediately. No messages can be sent by means of the IDirectPlay8Client::Send method until the connection has completed.

Before this method is called, you can obtain an application description by calling IDirectPlay8Client::EnumHosts. When you call EnumHosts, DPN_MSGID_ENUM_HOSTS_RESPONSE messages are sent to your message handler with the IDirectPlay8Address objects and the DPN_APPLICATION_DESC structure for each host found. This information can be passed without modification to the Connect method.

HRESULT Connect(
const DPN_APPLICATION_DESC *const
pdnAppDesc,
IDirectPlay8Address *const pHostAddr,
IDirectPlay8Address *const pDeviceInfo,
const DPN_SECURITY_DESC *const pdnSecurity,
const DPN_SECURITY_CREDENTIALS *const pdnCredentials,
const void *const pvUserConnectData,
const DWORD dwUserConnectDataSize,
void *const pvAsyncContext,
DPNHANDLE *const phAsyncHandle,
const DWORD dwFlags
);

Parameters

pdnAppDesc
[in] Pointer to a DPN_APPLICATION_DESC structure that describes the application. The only member of this structure that you must set is the guidApplication member. Only some of the members of this structure are used by this method. The only member that you must set is guidApplication. You can also set guidInstance, pwszPassword, dwFlags, and dwSize.
pHostAddr
[in] Pointer to an IDirectPlay8Address interface that specifies the addressing information to use to connect to the computer that is hosting. The user can be queried for any missing address information if you set the DPNENUMHOSTS_OKTOQUERYFORADDRESSING flag in the dwFlags parameter.
pDeviceInfo
[in] Pointer to an IDirectPlay8Address object that specifies what network adapter (for example, NIC, modem, and so on) to use to connect to the server. Some service providers allow this parameter to be NULL or be an address object containing only the service provider component. In this case, they will use the most appropriate device to reach the designated host. If you set the DPNCONNECT_OKTOQUERYFORADDRESSING flag in dwFlags, the user can be queried for any missing address information.
pdnSecurity
[in] Reserved. Must be NULL.
pdnCredentials
[in] Reserved. Must be NULL.
pvUserConnectData
[in] Pointer to application-specific data provided to the host or server to further validate the connection. DirectPlay will make a copy of this data when the method is called and therefore you can modify or destroy this data once the connection is complete. This data is sent to the DPN_MSGID_INDICATE_CONNECT message in the pvUserConnectData member. This parameter is optional and you can pass NULL to bypass the connection validation provided by the user code.
dwUserConnectDataSize
[in] Variable of type DWORD that specifies the size of the data contained in pvUserConnectData.
pvAsyncContext
[in] Pointer to the user-supplied context, which is returned in the pvUserContext member of the DPN_MSGID_CONNECT_COMPLETE system message. This parameter is optional and can be set to NULL.
phAsyncHandle
[out] A DPNHANDLE. When the method returns, phAsyncHandle will point to a handle that you can pass to IDirectPlay8Client::CancelAsyncOperation to cancel the operation. This parameter must be set to NULL if you set the DPNCONNECT_SYNC flag in dwFlags.
dwFlags
[in] Flag that describes the connection mode. You can set the following flag.
DPNCONNECT_OKTOQUERYFORADDRESSING
Setting this flag will display a standard Microsoft® DirectPlay® dialog box, which queries the user for more information if not enough information is passed in this method.
DPNCONNECT_SYNC
Process the connection request synchronously.

Return Values

Returns S_OK if this method is processed synchronously and is successful. If the request is processed asynchronously, S_OK will be returned if the method is instantly processed. By default, this method is run asynchronously and generally returns DPNSUCCESS_PENDING or one of the following error values.

DPNERR_HOSTREJECTEDCONNECTION
DPNERR_INVALIDAPPLICATION
DPNERR_INVALIDDEVICEADDRESS
DPNERR_INVALIDFLAGS
DPNERR_INVALIDHOSTADDRESS
DPNERR_INVALIDINSTANCE
DPNERR_INVALIDINTERFACE
DPNERR_INVALIDPASSWORD
DPNERR_NOCONNECTION
DPNERR_NOTHOST
DPNERR_SESSIONFULL
DPNERR_ALREADYCONNECTED
 

Remarks

It is not required to enumerate hosts before calling Connect if you know the appropriate host and device information.

If you do call the IDirectPlay8Client::EnumHosts method and you want to ensure better network address translation (NAT) and proxy support when using the TCP/IP service provider or prevent redialing with the modem service provider, keep the enumeration active when calling Connect. To prevent the enumeration from completing, set the dwEnumCount parameter to INFINITE and do not use the IDirectPlay8Client::CancelAsyncOperation to terminate the enumeration before the connect operation has completed. You should also pass the pAddressSender and pAddressDevice address objects in the DPNMSG_ENUM_HOSTS_RESPONSE message without modification into the pHostAddr and pDeviceInfo parameters of the Connect method. To pass the address objects to Connect outside of the callback function, use IDirectPlay8Address::Duplicate or IDirectPlay8Address::AddRef to prevent the object from being destroyed and store the pointers using thread-safe code. DirectPlay will automatically cancel the enumeration when the connect completes with DPN_OK or when IDirectPlay8Client::Close is called.

Although multiple enumerations can be run concurrently and can be run across the duration of a connection, only one connection is allowed per interface. To establish a connection to more than one application, you must create another interface.

When this method is called, a DPN_MSGID_INDICATE_CONNECT message is posted to the server's message handler. On retrieval of this message, the host can pass back connection reply data to the Connect method. Connection reply data can send a message indicating that the host does not approve the connection. The calling application can then handle this reply appropriately.

If Connect is called synchronously, the following outcomes are possible.

  • Connection Successful. The application will receive a DPN_MSGID_CONNECT_COMPLETE message containing the success code and the Connect method will return with DPN_OK.
  • Connection fails because the server rejects the connection. The application will receive a DPN_MSGID_CONNECT_COMPLETE message containing the DPNERR_HOSTREJECTEDCONNECTION failure code. The Connect method will also return with the error code DPNERR_HOSTREJECTEDCONNECTION. The DPN_MSGID_CONNECT_COMPLETE message provides an opportunity for the client application to inspect any data the server returns with the rejection.
  • Connection fails for any other reason. The application will not receive a DPN_MSGID_CONNECT_COMPLETE message, and the Connect method will return with the appropriate error code.

If Connect is called asynchronously, the method returns immediately with DPNSUCCESS_PENDING. A DPN_MSGID_CONNECT_COMPLETE message will follow after the connection is complete, containing the result of the connection. The only time the method does not return DPNSUCCESS_PENDING is when validation of the supplied parameters fails, in which case the appropriate error code is returned.

When the connection request completes, all outstanding enumerations are canceled with the return of DPNERR_USERCANCEL.

The hResultCode on the completion will indicate S_OK if the Connect() attempt was successful, or an error otherwise. If the Host player returned anything other than S_OK from the DPN_MSGID_INDICATE_CONNECT message, the likely error code in the completion will be DPNERR_HOSTREJECTEDCONNECTION.

To close the connection established with this method, call the IDirectPlay8Client::Close method.

Note  If you set the DPNCONNECT_OKTOQUERYFORADDRESSING flag in dwFlags, the service provider might attempt to display a dialog box to ask the user to complete the address information. You must have a visible window present when the service provider tries to display the dialog box, or your application will lock.


最后一個參數(shù)dwFlags告訴DirectPlay采用異步(0)還是同步(DPNCONNECT_SYNC)方式進(jìn)行連接。使用異步連接方式會立即返回控制權(quán),所以需要等待DPN_MSGID_CONNECT_COMPLETE消息來標(biāo)識和服務(wù)器端的成功連接。采用同步連接方式, Connect函數(shù)就只會在出現(xiàn)錯誤或成功連接時才會返回。

下面這個函數(shù)演示了如何設(shè)置客戶端屬性并建立與服務(wù)器端的連接。
 
// window handles, class.
HWND g_hwnd;
char g_class_name[] = "ClientClass";

// application GUID
GUID g_app_guid = { 0xababbe60, 0x1ac0, 0x11d5, { 0x90, 0x89, 0x44, 0x45, 0x53, 0x54, 0x0, 0x1 } };

IDirectPlay8Client*         g_dp_client;    
// DirectPlay Client
DPN_SERVICE_PROVIDER_INFO*  g_adapter_list; // adapters
DWORD                       g_num_adapters; // number of adapters

BOOL g_is_connected;        
// flag indicates whether connection between client and server is build up
DPNHANDLE g_async_handle;   // async connection handle

//--------------------------------------------------------------------------------
// Start connecting to server which GUID specified by adapter_guid and server IP
// specified by ip.
//--------------------------------------------------------------------------------
BOOL Start_Session(GUID* adapter_guid, char* ip)
{
    
// Make sure there are an invalid adapter and an invalid IP address
    if(adapter_guid == NULL || ip == NULL)
        
return FALSE;

    
// Need to re-assign a network handler as quitting a previous session to clears it.
    // Close the connection first before assigning a new network handler.
    //
    // Closes the open connnection to a session.
    g_dp_client->Close(0);

    
// Initialize DirectPlay Client
    if(FAILED(g_dp_client->Initialize(NULL, Net_Msg_Handle, 0)))
        
return FALSE;

    
// Assign player information

    
char player_name[256];
    WCHAR w_player_name[256];

    GetWindowText(GetDlgItem(g_hwnd, IDC_NAME), player_name, 256);
    mbstowcs(w_player_name, player_name, strlen(player_name) + 1);

    DPN_PLAYER_INFO dpn_player_info;

    ZeroMemory(&dpn_player_info, 
sizeof(DPN_PLAYER_INFO));

    dpn_player_info.dwSize      = 
sizeof(DPN_PLAYER_INFO);
    dpn_player_info.dwInfoFlags = DPNINFO_NAME | DPNINFO_DATA;
    dpn_player_info.pwszName    = w_player_name;

    
// set the static settings of a client with an application, process synchronously.
    g_dp_client->SetClientInfo(&dpn_player_info, NULL, NULL, DPNSETCLIENTINFO_SYNC);

    IDirectPlay8Address* dp_host_address;
    IDirectPlay8Address* dp_device_address;

    
// Create an address object and fill it with information

    // create host address object
    if(FAILED(CoCreateInstance(CLSID_DirectPlay8Address, NULL, CLSCTX_INPROC, IID_IDirectPlay8Address, 
                               (
void**)&dp_host_address)))
        
return FALSE;

    
// create device address object
    if(FAILED(CoCreateInstance(CLSID_DirectPlay8Address, NULL, CLSCTX_INPROC, IID_IDirectPlay8Address, 
                               (
void**)&dp_device_address)))
    {
        dp_host_address->Release();
        
return FALSE;
    }
   
    
// Set the protocol to TCP/IP
    //
    // Sets the service provider GUID in the address object.
    // If a service provider is specified for this address, it is overwrittern by this call.

    // set the protocol to host address
    if(FAILED(dp_host_address->SetSP(&CLSID_DP8SP_TCPIP)))
        
goto fail;

    
// set the protocol to device address
    if(FAILED(dp_device_address->SetSP(&CLSID_DP8SP_TCPIP)))
        
goto fail;

    
// Set the port
    DWORD port = 21234;

    
// Adds a component to the address.
    // If the component is part of the address, it is replaced by the new value in this call.

    // Set port
    if(FAILED(dp_host_address->AddComponent(DPNA_KEY_PORT, &port, sizeof(DWORD), DPNA_DATATYPE_DWORD)))
        
goto fail;

    WCHAR w_ip[128] = {0};

    
// set the host name
    mbstowcs(w_ip, ip, strlen(ip)+1);
    dp_host_address->AddComponent(DPNA_KEY_HOSTNAME, w_ip, (DWORD)((wcslen(w_ip)+1) * 
sizeof(WCHAR)), 
                                  DPNA_DATATYPE_STRING);

    
// Set the adapter
    dp_host_address->AddComponent(DPNA_KEY_DEVICE, adapter_guid, sizeof(GUID), DPNA_DATATYPE_GUID);
    dp_device_address->AddComponent(DPNA_KEY_DEVICE, adapter_guid, 
sizeof(GUID), DPNA_DATATYPE_GUID);
        
    DPN_APPLICATION_DESC app_desc;  
// Describes the settings for a Microsoft DirectPlay application

    // Setup the application description structure

    ZeroMemory(&app_desc, 
sizeof(DPN_APPLICATION_DESC));

    app_desc.dwSize          = 
sizeof(DPN_APPLICATION_DESC);
    app_desc.dwFlags         = DPNSESSION_CLIENT_SERVER;
    app_desc.guidApplication = g_app_guid;
    app_desc.pwszSessionName = L"SercverSession";

    
// Establishes the connection to the server
    if(FAILED(g_dp_client->Connect(&app_desc, dp_host_address, dp_device_address, NULL, NULL, NULL, 0, NULL, 
                                   &g_async_handle, 0)))
        
goto fail;    

    
// setup dialog control information  
    EnableWindow(GetDlgItem(g_hwnd, IDC_ADAPTERS), FALSE);
    EnableWindow(GetDlgItem(g_hwnd, IDC_IP), FALSE);
    EnableWindow(GetDlgItem(g_hwnd, IDC_NAME), FALSE);
    EnableWindow(GetDlgItem(g_hwnd, IDC_CONNECT), TRUE);

    dp_host_address->Release();
    dp_device_address->Release();
    
return TRUE;

fail:
    dp_host_address->Release();
    dp_device_address->Release();
    
return FALSE;
}
 

發(fā)送和接收消息

客戶端接收消息和服務(wù)器端接收消息的處理方法是一樣的,所以只需要關(guān)心消息處理函數(shù)內(nèi)部的實現(xiàn),發(fā)送消息需要調(diào)用IDirectPlay8Server::Send函數(shù)。

Transmits data to the server. The message can be sent synchronously or asynchronously.

HRESULT Send(
const DPN_BUFFER_DESC *const
pBufferDesc,
const DWORD cBufferDesc,
const DWORD dwTimeOut,
void *const pvAsyncContext,
DPNHANDLE *const phAsyncHandle,
const DWORD dwFlags
);

Parameters

pBufferDesc
[in] Pointer to a DPN_BUFFER_DESC structure that describes the data to send.
cBufferDesc
[in] Number of DPN_BUFFER_DESC structures pointed to by pBufferDesc. There can only be one buffer in this version of Microsoft® DirectPlay® .
dwTimeOut
[in] Number of milliseconds to wait for the message to send. If the message has not been sent by the dwTimeOut value, it is deleted from the send queue. If you set this parameter to 0, the message remains in the send queue until it is sent or until the link is dropped.
pvAsyncContext
[in] Pointer to the user-supplied context, which is returned in the pvUserContext member of the DPN_MSGID_SEND_COMPLETE system message.
phAsyncHandle
[in,out] A DPNHANDLE. When the method returns, phAsyncHandle will point to a handle that you can pass to IDirectPlay8Client::CancelAsyncOperation to cancel the operation. This parameter must be set to NULL if you set the DPNSEND_SYNC flag in dwFlags.
dwFlags
[in] Flags that describe send behavior. You can set one or more of the following flags.
DPNSEND_SYNC
Process the Send request synchronously.
DPNSEND_NOCOPY
Use the data in the DPN_BUFFER_DESC structure and do not make an internal copy. This may be a more efficient method of sending data to the server. However, it is less robust, because the sender might be able to modify the message before the receiver has processed it. This flag cannot be combined with DPNSEND_NOCOMPLETE.
DPNSEND_NOCOMPLETE
Does not send DPN_MSGID_SEND_COMPLETE to the message handler. This flag may not be used with DPNSEND_NOCOPY or DPNSEND_GUARANTEED. Additionally, when using this flag pvAsyncContext must be NULL.
DPNSEND_COMPLETEONPROCESS
Send DPN_MSGID_SEND_COMPLETE to the message handler when this message has been delivered to the target and the target's message handler returns from indicating its reception. There is additional internal message overhead when this flag is set, and the message transmission process may become significantly slower. If you set this flag, DPNSEND_GUARANTEED must also be set.
DPNSEND_GUARANTEED
Send the message by a guaranteed method of delivery.
DPNSEND_PRIORITY_HIGH
Sets the priority of the message to high. This flag cannot be used with DPNSEND_PRIORITY_LOW.
DPNSEND_PRIORITY_LOW
Sets the priority of the message to low. This flag cannot be used with DPNSEND_PRIORITY_HIGH.
DPNSEND_NOLOOPBACK
Suppress the DPN_MSGID_RECEIVE system message to your message handler if you are sending to yourself.
DPNSEND_NONSEQUENTIAL
If the flag is not set, messages are delivered to the target application in the order that they are sent, which may necessitate buffering out of sequence messages until the missing messages arrive. Messages are simply delivered to the target application in the order that they are received.

Return Values

Returns S_OK if this method is processed synchronously and is successful. By default, this method is run asynchronously and generally returns DPNSUCCESS_PENDING or one of the following error values.

DPNERR_INVALIDFLAGS
DPNERR_TIMEDOUT

Remarks

This method generates a DPN_MSGID_RECEIVE system message in the server's message handler. The data buffer is contained in the pReceiveData member of the associated structure.

Messages can have one of three priorities: low, normal, and high. To specify a low or high priority for the message, set the appropriate flag in dwFlags. If neither of the priority flags is set, the message will have normal priority. See Basic Networking for a discussion of send priorities.

When the Send request is completed, a DPN_MSGID_SEND_COMPLETE system message is posted to the sender's message handler. The success or failure of the request is contained in the hResultCode member of the associate structure. You can suppress the send completion by setting the DPN_NOCOMPLETE flag in dwflags.

If a player joins a game and needs to send multiple messages immediately, the player should first send a message with the DPNSEND_COMPLETEONPROCESS flag set. When the DPN_MSGID_SEND_COMPLETE message is returned, the application can begin sending messages. If the player does not do this, some of the messages might need to be queued on the receiver and, if too much data arrives, the queue can grow faster than the receiver can handle the messages. This might result in lost data. After a player is established in the game, however, throttling in DirectPlay will control the data flow by using message timeouts or the GetSendQueueInfo method. For more information, see Optimizing Network Usage.

Send completions are typically posted on the source computer as soon as the message is sent. In other words, a send completion does not necessarily mean that the message has been processed on the target. It may still be in a queue. If you want to be certain that the message has been processed by the target, set the DPN_COMPLETEONPROCESS flag in dwFlags. This flag ensures that the send completion will not be sent until the target's message handler has processed the message, and returned.

Note  Do not assume that resources such as the data buffer will remain valid until the method has returned. If you call this method asynchronously, the DPN_MSGID_SEND_COMPLETE message may be received and processed by your message handler before the call has returned. If your message handler deallocates or otherwise invalidates a resource such as the data buffer, that resource may become invalid at any time after the method has been called.


下面這兩個函數(shù)演示了如何發(fā)送和接收消息。
 
// window handles, class.
HWND g_hwnd;
char g_class_name[] = "ClientClass";

// application GUID
GUID g_app_guid = { 0xababbe60, 0x1ac0, 0x11d5, { 0x90, 0x89, 0x44, 0x45, 0x53, 0x54, 0x0, 0x1 } };

IDirectPlay8Client*         g_dp_client;    
// DirectPlay Client
DPN_SERVICE_PROVIDER_INFO*  g_adapter_list; // adapters
DWORD                       g_num_adapters; // number of adapters

BOOL g_is_connected;        
// flag indicates whether connection between client and server is build up
DPNHANDLE g_async_handle;   // async connection handle

//--------------------------------------------------------------------------------
// Send text to all clients.
//--------------------------------------------------------------------------------
void Send_Text_Msg(char* text)
{
    DPNHANDLE async_handle;
    DPN_BUFFER_DESC buffer_desc;
    
char name[64], message[1024];

    
if(g_dp_client == NULL)
        
return;

    
// get the name
    GetWindowText(GetDlgItem(g_hwnd, IDC_NAME), name, 64);

    
// rebuild string based on the name
    sprintf(message, "%s> %s", name, text);

    
// build a data structure
    buffer_desc.dwBufferSize = (DWORD) (strlen(message) + 1);
    buffer_desc.pBufferData  = (BYTE*) message;

    
// Send message (async method - reason for handle)    
    g_dp_client->Send(&buffer_desc, 1, 0, NULL, &async_handle, DPNSEND_NOLOOPBACK);
}

//----------------------------------------------------------------------------------------
// Callback function that receives all messages from the client, and receives indications 
// of session changes from the IDirectPlay8Client interface. 
//----------------------------------------------------------------------------------------
HRESULT WINAPI Net_Msg_Handle(PVOID user_context, DWORD message_id, PVOID msg_buffer)
{   
    DPNMSG_RECEIVE* receive_data;

    
switch(message_id)
    {
    
// Microsoft DirectPlay generates the DPN_MSGID_RECEIVE message when a message has been processed by 
    // the receiver.
    case DPN_MSGID_RECEIVE:
        receive_data = (DPNMSG_RECEIVE*) msg_buffer;

        
// write out text
        SendMessage(GetDlgItem(g_hwnd, IDC_CHATTER), LB_ADDSTRING, 0, (LPARAM) receive_data->pReceiveData);

        
break;
    }

     
// return S_OK to signify the message was handled OK.
    return S_OK;
}
 


獲得玩家的ID號

當(dāng)連接到服務(wù)器端后,在有些情況下客戶端需要使用自己的玩家ID號。要獲得玩家的ID號,客戶端可以解析連接完成消息(DPN_MSGID_CONNECT_COMPLETE),該消息使用以下數(shù)據(jù)結(jié)構(gòu)來包含有關(guān)客戶端到服務(wù)器端連接的必要信息。

Microsoft® DirectPlay® generates the DPN_MSGID_CONNECT_COMPLETE message when the connection attempt has been completed in a peer-to-peer or client/server session. This message is generated whether or not the connection was successful.

The DPNMSG_CONNECT_COMPLETE structure contains information for the DPN_MSGID_CONNECT_COMPLETE system message.

typedef struct _DPNMSG_CONNECT_COMPLETE{
DWORD dwSize;
DPNHANDLE hAsyncOp;
PVOID pvUserContext;
HRESULT hResultCode;
PVOID pvApplicationReplyData;
DWORD dwApplicationReplyDataSize;
} DPNMSG_CONNECT_COMPLETE, *PDPNMSG_CONNECT_COMPLETE;
dwSize
Size of this structure.
hAsyncOp
Asynchronous operation handle.
pvUserContext
User context supplied when the IDirectPlay8Peer::Connect or IDirectPlay8Client::Connect methods are called.
hResultCode
HRESULT describing the result of the connection attempt. See the Return Values section in the IDirectPlay8Peer::Connect or IDirectPlay8Client::Connect method for more information. Additionally, DPNERR_PLAYERNOTREACHABLE will be returned if a player has tried to join a peer-to-peer session where at least one other existing player in the session cannot connect to the joining player.
pvApplicationReplyData
Connection reply data returned from the host or server.
dwApplicationReplyDataSize
Size of the data, in bytes, of the pvApplicationReplyData member.

Return Values

Return DPN_OK.

 

當(dāng)服務(wù)器端調(diào)用IDirectPlay8Server::DestroyClient來銷毀玩家時,客戶端會收到消息 DPN_MSGID_TERMINATE_SESSION。


下面這個函數(shù)演示了如何處理消息DPN_MSGID_CONNECT_COMPLETE和DPN_MSGID_TERMINATE_SESSION。

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

// application GUID
GUID g_app_guid = { 0xababbe60, 0x1ac0, 0x11d5, { 0x90, 0x89, 0x44, 0x45, 0x53, 0x54, 0x0, 0x1 } };

IDirectPlay8Client*         g_dp_client;    
// DirectPlay Client
DPN_SERVICE_PROVIDER_INFO*  g_adapter_list; // adapters
DWORD                       g_num_adapters; // number of adapters

BOOL g_is_connected;        
// flag indicates whether connection between client and server is build up
DPNHANDLE g_async_handle;   // async connection handle

//----------------------------------------------------------------------------------------
// Callback function that receives all messages from the client, and receives indications 
// of session changes from the IDirectPlay8Client interface. 
//----------------------------------------------------------------------------------------
HRESULT WINAPI Net_Msg_Handle(PVOID user_context, DWORD message_id, PVOID msg_buffer)
{    
    
// contains information for the DPN_MSGID_CONNECT_COMPLETE system message
    DPNMSG_CONNECT_COMPLETE* connect_complete;

    
// contains information for the DPN_MSGID_TERMINATE_COMPLETE system message
    DPNMSG_TERMINATE_SESSION* terminate_session;

    
switch(message_id)
    {
    
case DPN_MSGID_CONNECT_COMPLETE:
        connect_complete = (DPNMSG_CONNECT_COMPLETE*) msg_buffer;

        EnableWindow(GetDlgItem(g_hwnd, IDC_CONNECT), TRUE);

        
// Make sure connection complete
        if(connect_complete->hResultCode == S_OK)
        {
            
// setup the dialog information
            EnableWindow(GetDlgItem(g_hwnd, IDC_ADAPTERS), FALSE);
            EnableWindow(GetDlgItem(g_hwnd, IDC_IP), FALSE);
            EnableWindow(GetDlgItem(g_hwnd, IDC_NAME), FALSE);            

            SetWindowText(GetDlgItem(g_hwnd, IDC_CONNECT), "Disconnect");

            
// flag as connected
            g_is_connected = TRUE;
        }
        
else
        {
            
switch(connect_complete->hResultCode)
            {
            
case DPNERR_HOSTREJECTEDCONNECTION:
                MessageBox(g_hwnd, "Host reject this connection", "Error", MB_OK);
                
break;
            
case DPNERR_INVALIDAPPLICATION:
                MessageBox(g_hwnd, "The GUID supplied for the application is invalid.", "Error", MB_OK);
                
break;
            
case DPNERR_INVALIDDEVICEADDRESS:
                MessageBox(g_hwnd, "The address for the local computer or adapter is invalid.", "Error", MB_OK);
                
break;
            
case DPNERR_INVALIDFLAGS:
                MessageBox(g_hwnd, "The flags passed to this method are invalid. ", "Error", MB_OK);
                
break;
            
case DPNERR_INVALIDHOSTADDRESS:
                MessageBox(g_hwnd, "The specified remote address is invalid.", "Error", MB_OK);
                
break;
            
case DPNERR_INVALIDINSTANCE:
                MessageBox(g_hwnd, "The GUID for the application instance is invalid.", "Error", MB_OK);
                
break;
            
case DPNERR_INVALIDINTERFACE:
                MessageBox(g_hwnd, "The interface parameter is invalid.", "Error", MB_OK);
                
break;
            
case DPNERR_INVALIDPASSWORD:
                MessageBox(g_hwnd, 
                    "An invalid password was supplied when attempting to join a session that requires a password.", 
                    "Error", MB_OK);
                
break;
            
case DPNERR_NOCONNECTION:
                MessageBox(g_hwnd, "No communication link was established.", "Error", MB_OK);
                
break;
            
case DPNERR_NOTHOST:
                MessageBox(g_hwnd, "An attempt by the client to connect to a nonhost computer.", "Error", MB_OK);
                
break;
            
case DPNERR_SESSIONFULL:
                MessageBox(g_hwnd, 
                    "The request to connect to the host or server failed because the maximum number of players allotted for the session "

                    "has been reached. ",  "Error", MB_OK);
                
break;
            
case DPNERR_ALREADYCONNECTED:
                MessageBox(g_hwnd, "The object is already connected to the session.", "Error", MB_OK);
                
break;
            }
            
            
// setup the dialog information
            EnableWindow(GetDlgItem(g_hwnd, IDC_ADAPTERS), TRUE);
            EnableWindow(GetDlgItem(g_hwnd, IDC_IP), TRUE);
            EnableWindow(GetDlgItem(g_hwnd, IDC_NAME), TRUE);
            
            SetWindowText(GetDlgItem(g_hwnd, IDC_CONNECT), "Connect");

            
// flag as disconnected
            g_is_connected = FALSE;
        }

        
// clear async handle
        g_async_handle = NULL;

        
break;

    
case DPN_MSGID_TERMINATE_SESSION:
        terminate_session = (DPNMSG_TERMINATE_SESSION*) msg_buffer;

        
// setup the dialog information
        EnableWindow(GetDlgItem(g_hwnd, IDC_ADAPTERS), TRUE);
        EnableWindow(GetDlgItem(g_hwnd, IDC_IP), TRUE);
        EnableWindow(GetDlgItem(g_hwnd, IDC_NAME), TRUE);

        SetWindowText(GetDlgItem(g_hwnd, IDC_CONNECT), "Connect");

        
// clears async handle
        g_async_handle = NULL;

        
// flag as disconnected
        g_is_connected = FALSE;

        
// print a disconnect message
        SendMessage(GetDlgItem(g_hwnd, IDC_CHATTER), LB_ADDSTRING, 0, (LPARAM)"Disconnected.");

        
break;
    }
    
     
// return S_OK to signify the message was handled OK.
    return S_OK;
}
 


終止客戶端會話

當(dāng)客戶端需要從一次會話中斷開連接時,需要明確向DirectPlay傳達(dá)該消息,以使其按正確的步驟關(guān)閉連接。使用IDirectPlay8Client::Close函數(shù),就能將客戶端從一次會話中斷開連接。

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.

HRESULT Close(
const DWORD
dwFlags
);

Parameters

dwFlags
[in] Reserved. Must be 0.

Return Values

Returns S_OK if successful, or the following error value.

DPNERR_UNINITIALIZED
 

Remarks

Calling Close will cancel all outstanding operations, including data sent as guaranteed. To make sure all messages are sent, wait for all outstanding Send calls to complete before calling Close.

If you do not want the application to wait, the application should call IDirectPlay8Client::CancelAsyncOperation to cancel all outstanding sends prior to calling IDirectPlay8Client::Close or doing a final release call on the IDirectPlay8Client interface. Failing to do so causes unpredictable results.


下面給出一個完整的代碼示例:

點擊下載源碼和工程

 
/***************************************************************************************
PURPOSE:
    Client Network Demo
 ***************************************************************************************/


#include <windows.h>
#include <stdio.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[] = "ClientClass";

// application GUID
GUID g_app_guid = { 0xababbe60, 0x1ac0, 0x11d5, { 0x90, 0x89, 0x44, 0x45, 0x53, 0x54, 0x0, 0x1 } };

IDirectPlay8Client*         g_dp_client;    
// DirectPlay Client
DPN_SERVICE_PROVIDER_INFO*  g_adapter_list; // adapters
DWORD                       g_num_adapters; // number of adapters

BOOL g_is_connected;        
// flag indicates whether connection between client and server is build up
DPNHANDLE g_async_handle;   // async connection handle

//--------------------------------------------------------------------------------
// Send text to all clients.
//--------------------------------------------------------------------------------
void Send_Text_Msg(char* text)
{
    DPNHANDLE async_handle;
    DPN_BUFFER_DESC buffer_desc;
    
char name[64], message[1024];

    
if(g_dp_client == NULL)
        
return;

    
// get the name
    GetWindowText(GetDlgItem(g_hwnd, IDC_NAME), name, 64);

    
// rebuild string based on the name
    sprintf(message, "%s> %s", name, text);

    
// build a data structure
    buffer_desc.dwBufferSize = (DWORD) (strlen(message) + 1);
    buffer_desc.pBufferData  = (BYTE*) message;

    
// Send message (async method - reason for handle)    
    g_dp_client->Send(&buffer_desc, 1, 0, NULL, &async_handle, DPNSEND_NOLOOPBACK);
}

//----------------------------------------------------------------------------------------
// Callback function that receives all messages from the client, and receives indications 
// of session changes from the IDirectPlay8Client interface. 
//----------------------------------------------------------------------------------------
HRESULT WINAPI Net_Msg_Handle(PVOID user_context, DWORD message_id, PVOID msg_buffer)
{    
    
// contains information for the DPN_MSGID_CONNECT_COMPLETE system message
    DPNMSG_CONNECT_COMPLETE* connect_complete;
    
    
// contains information for the DPN_MSGID_TERMINATE_COMPLETE system message
    DPNMSG_TERMINATE_SESSION* terminate_session;

    DPNMSG_RECEIVE* receive_data;

    
switch(message_id)
    {
    
case DPN_MSGID_CONNECT_COMPLETE:
        connect_complete = (DPNMSG_CONNECT_COMPLETE*) msg_buffer;

        EnableWindow(GetDlgItem(g_hwnd, IDC_CONNECT), TRUE);

        
// Make sure connection complete
        if(connect_complete->hResultCode == S_OK)
        {
            
// setup the dialog information
            EnableWindow(GetDlgItem(g_hwnd, IDC_ADAPTERS), FALSE);
            EnableWindow(GetDlgItem(g_hwnd, IDC_IP), FALSE);
            EnableWindow(GetDlgItem(g_hwnd, IDC_NAME), FALSE);            

            SetWindowText(GetDlgItem(g_hwnd, IDC_CONNECT), "Disconnect");

            
// flag as connected
            g_is_connected = TRUE;
        }
        
else
        {
            
switch(connect_complete->hResultCode)
            {
            
case DPNERR_HOSTREJECTEDCONNECTION:
                MessageBox(g_hwnd, "Host reject this connection", "Error", MB_OK);
                
break;
            
case DPNERR_INVALIDAPPLICATION:
                MessageBox(g_hwnd, "The GUID supplied for the application is invalid.", "Error", MB_OK);
                
break;
            
case DPNERR_INVALIDDEVICEADDRESS:
                MessageBox(g_hwnd, "The address for the local computer or adapter is invalid.", "Error", MB_OK);
                
break;
            
case DPNERR_INVALIDFLAGS:
                MessageBox(g_hwnd, "The flags passed to this method are invalid. ", "Error", MB_OK);
                
break;
            
case DPNERR_INVALIDHOSTADDRESS:
                MessageBox(g_hwnd, "The specified remote address is invalid.", "Error", MB_OK);
                
break;
            
case DPNERR_INVALIDINSTANCE:
                MessageBox(g_hwnd, "The GUID for the application instance is invalid.", "Error", MB_OK);
                
break;
            
case DPNERR_INVALIDINTERFACE:
                MessageBox(g_hwnd, "The interface parameter is invalid.", "Error", MB_OK);
                
break;
            
case DPNERR_INVALIDPASSWORD:
                MessageBox(g_hwnd, 
                    "An invalid password was supplied when attempting to join a session that requires a password.", 
                    "Error", MB_OK);
                
break;
            
case DPNERR_NOCONNECTION:
                MessageBox(g_hwnd, "No communication link was established.", "Error", MB_OK);
                
break;
            
case DPNERR_NOTHOST:
                MessageBox(g_hwnd, "An attempt by the client to connect to a nonhost computer.", "Error", MB_OK);
                
break;
            
case DPNERR_SESSIONFULL:
                MessageBox(g_hwnd, 
                    
"The request to connect to the host or server failed because the maximum number of players allotted for the session "
                    "has been reached. ",  "Error", MB_OK);
                
break;
            
case DPNERR_ALREADYCONNECTED:
                MessageBox(g_hwnd, "The object is already connected to the session.", "Error", MB_OK);
                
break;
            }
            
            
// setup the dialog information
            EnableWindow(GetDlgItem(g_hwnd, IDC_ADAPTERS), TRUE);
            EnableWindow(GetDlgItem(g_hwnd, IDC_IP), TRUE);
            EnableWindow(GetDlgItem(g_hwnd, IDC_NAME), TRUE);
            
            SetWindowText(GetDlgItem(g_hwnd, IDC_CONNECT), "Connect");

            
// flag as disconnected
            g_is_connected = FALSE;
        }

        
// clear async handle
        g_async_handle = NULL;

        
break;

    
case DPN_MSGID_TERMINATE_SESSION:
        terminate_session = (DPNMSG_TERMINATE_SESSION*) msg_buffer;

        
// setup the dialog information
        EnableWindow(GetDlgItem(g_hwnd, IDC_ADAPTERS), TRUE);
        EnableWindow(GetDlgItem(g_hwnd, IDC_IP), TRUE);
        EnableWindow(GetDlgItem(g_hwnd, IDC_NAME), TRUE);

        SetWindowText(GetDlgItem(g_hwnd, IDC_CONNECT), "Connect");

        
// clears async handle
        g_async_handle = NULL;

        
// flag as disconnected
        g_is_connected = FALSE;

        
// print a disconnect message
        SendMessage(GetDlgItem(g_hwnd, IDC_CHATTER), LB_ADDSTRING, 0, (LPARAM)"Disconnected.");

        
break;

    
// Microsoft DirectPlay generates the DPN_MSGID_RECEIVE message when a message has been processed by 
    // the receiver.
    case DPN_MSGID_RECEIVE:
        receive_data = (DPNMSG_RECEIVE*) msg_buffer;

        
// write out text
        SendMessage(GetDlgItem(g_hwnd, IDC_CHATTER), LB_ADDSTRING, 0, (LPARAM) receive_data->pReceiveData);

        
break;
    }

    
// 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 TCP/IP adapters.
//--------------------------------------------------------------------------------
void Enum_Adapters()
{
    
// return if no server object or GUID
    if(g_dp_client == NULL)
        
return;

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

    
// clear the combo-box
    SendMessage(adapters_ctrl, CB_RESETCONTENT, 0, 0);

    
// free prior adapter list
    delete[] g_adapter_list;
    g_adapter_list = NULL;

    g_num_adapters = 0;

    DWORD size = 0;

    
// query the required size of the data buffer
    HRESULT rv = g_dp_client->EnumServiceProviders(&CLSID_DP8SP_TCPIP, NULL, g_adapter_list, &size, &g_num_adapters, 0);

    
if(rv != DPNERR_BUFFERTOOSMALL)
        
return;

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

    
// enumerate again
    if(SUCCEEDED(g_dp_client->EnumServiceProviders(&CLSID_DP8SP_TCPIP, NULL, g_adapter_list, &size, &g_num_adapters, 0)))
    {
        
char adapter_name[1024];

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

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

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

            
// go to next servicec provider
            adapter_ptr++;
        }
    }

    
// Select first adapter
    //
    // An application sends a CB_SETCURSEL message to select a string in the list of a combo box. 
    // If necessary, the list scrolls the string into view. The text in the edit control of the combo box 
    // changes to reflect the new selection, and any previous selection in the list is removed. 
    //
    // wParam:
    //    Specifies the zero-based index of the string to select. If this parameter is –1, any current selection 
    //    in the list is removed and the edit control is cleared. 
    //
    // lParam:
    //    This parameter is not used. 
    SendMessage(adapters_ctrl, CB_SETCURSEL, 0, 0);    
}

//--------------------------------------------------------------------------------
// Release all resource which allocated for DirectPlay.
//--------------------------------------------------------------------------------
void Release_DirectPlay()
{
    
// release client service provider list memory
    delete[] g_adapter_list;
    g_adapter_list = NULL;
    
    g_num_adapters = 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;
    }
}

//--------------------------------------------------------------------------------
// Start connecting to server which GUID specified by adapter_guid and server IP
// specified by ip.
//--------------------------------------------------------------------------------
BOOL Start_Session(GUID* adapter_guid, char* ip)
{
    
// Make sure there are an invalid adapter and an invalid IP address
    if(adapter_guid == NULL || ip == NULL)
        
return FALSE;

    
// Need to re-assign a network handler as quitting a previous session to clears it.
    // Close the connection first before assigning a new network handler.
    //
    // Closes the open connnection to a session.
    g_dp_client->Close(0);

    
// Initialize DirectPlay Client
    if(FAILED(g_dp_client->Initialize(NULL, Net_Msg_Handle, 0)))
        
return FALSE;

    
// Assign player information

    
char player_name[256];
    WCHAR w_player_name[256];

    GetWindowText(GetDlgItem(g_hwnd, IDC_NAME), player_name, 256);
    mbstowcs(w_player_name, player_name, strlen(player_name) + 1);

    DPN_PLAYER_INFO dpn_player_info;

    ZeroMemory(&dpn_player_info, 
sizeof(DPN_PLAYER_INFO));

    dpn_player_info.dwSize      = 
sizeof(DPN_PLAYER_INFO);
    dpn_player_info.dwInfoFlags = DPNINFO_NAME | DPNINFO_DATA;
    dpn_player_info.pwszName    = w_player_name;

    
// set the static settings of a client with an application, process synchronously.
    g_dp_client->SetClientInfo(&dpn_player_info, NULL, NULL, DPNSETCLIENTINFO_SYNC);

    IDirectPlay8Address* dp_host_address;
    IDirectPlay8Address* dp_device_address;

    
// Create an address object and fill it with information

    // create host address object
    if(FAILED(CoCreateInstance(CLSID_DirectPlay8Address, NULL, CLSCTX_INPROC, IID_IDirectPlay8Address, 
                               (
void**)&dp_host_address)))
        
return FALSE;

    
// create device address object
    if(FAILED(CoCreateInstance(CLSID_DirectPlay8Address, NULL, CLSCTX_INPROC, IID_IDirectPlay8Address, 
                               (
void**)&dp_device_address)))
    {
        dp_host_address->Release();
        
return FALSE;
    }
   
    
// Set the protocol to TCP/IP
    //
    // Sets the service provider GUID in the address object.
    // If a service provider is specified for this address, it is overwrittern by this call.

    // set the protocol to host address
    if(FAILED(dp_host_address->SetSP(&CLSID_DP8SP_TCPIP)))
        
goto fail;

    
// set the protocol to device address
    if(FAILED(dp_device_address->SetSP(&CLSID_DP8SP_TCPIP)))
        
goto fail;

    
// Set the port
    DWORD port = 21234;

    
// Adds a component to the address.
    // If the component is part of the address, it is replaced by the new value in this call.

    // Set port
    if(FAILED(dp_host_address->AddComponent(DPNA_KEY_PORT, &port, sizeof(DWORD), DPNA_DATATYPE_DWORD)))
        
goto fail;

    WCHAR w_ip[128] = {0};

    
// set the host name
    mbstowcs(w_ip, ip, strlen(ip)+1);
    dp_host_address->AddComponent(DPNA_KEY_HOSTNAME, w_ip, (DWORD)((wcslen(w_ip)+1) * 
sizeof(WCHAR)), 
                                  DPNA_DATATYPE_STRING);

    
// Set the adapter
    dp_host_address->AddComponent(DPNA_KEY_DEVICE, adapter_guid, sizeof(GUID), DPNA_DATATYPE_GUID);
    dp_device_address->AddComponent(DPNA_KEY_DEVICE, adapter_guid, 
sizeof(GUID), DPNA_DATATYPE_GUID);
        
    DPN_APPLICATION_DESC app_desc;  
// Describes the settings for a Microsoft DirectPlay application

    // Setup the application description structure

    ZeroMemory(&app_desc, 
sizeof(DPN_APPLICATION_DESC));

    app_desc.dwSize          = 
sizeof(DPN_APPLICATION_DESC);
    app_desc.dwFlags         = DPNSESSION_CLIENT_SERVER;
    app_desc.guidApplication = g_app_guid;
    app_desc.pwszSessionName = L"SercverSession";

    
// Establishes the connection to the server
    if(FAILED(g_dp_client->Connect(&app_desc, dp_host_address, dp_device_address, NULL, NULL, NULL, 0, NULL, 
                                   &g_async_handle, 0)))
        
goto fail;    

    
// setup dialog control information  
    EnableWindow(GetDlgItem(g_hwnd, IDC_ADAPTERS), FALSE);
    EnableWindow(GetDlgItem(g_hwnd, IDC_IP), FALSE);
    EnableWindow(GetDlgItem(g_hwnd, IDC_NAME), FALSE);
    EnableWindow(GetDlgItem(g_hwnd, IDC_CONNECT), TRUE);

    dp_host_address->Release();
    dp_device_address->Release();
    
return TRUE;

fail:
    dp_host_address->Release();
    dp_device_address->Release();
    
return FALSE;
}

//--------------------------------------------------------------------------------
// Stoping hosting server.
//--------------------------------------------------------------------------------
void Stop_Session()
{
    
// Close the connection
    if(g_dp_client)
        g_dp_client->Close(0);

    
// clear async handle
    g_async_handle = NULL;

    
// setup the dialog controls information

    // Enables combo-box control
    EnableWindow(GetDlgItem(g_hwnd, IDC_ADAPTERS), TRUE);
    EnableWindow(GetDlgItem(g_hwnd, IDC_IP), TRUE);
    EnableWindow(GetDlgItem(g_hwnd, IDC_NAME), TRUE);
    
    SetWindowText(GetDlgItem(g_hwnd, IDC_CONNECT), "Connect");

    
// flag as disconnected
    g_is_connected = FALSE;
}

//--------------------------------------------------------------------------------
// Window procedure.
//--------------------------------------------------------------------------------
long WINAPI Window_Proc(HWND hwnd, UINT msg_id, WPARAM wParam, LPARAM lParam)
{
    DPN_SERVICE_PROVIDER_INFO*  adapter_ptr;

    unsigned 
int selected;    
    
char msg[256], ip[16];
    
    
switch(msg_id)
    {
    
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. 
        switch(LOWORD(wParam))
        {
        
case IDC_CONNECT:
            
if(! g_is_connected)
            {
                
// Get adapter to use 
                if((selected = (int) SendMessage(GetDlgItem(hwnd, IDC_ADAPTERS), CB_GETCURSEL, 0, 0)) == LB_ERR)
                    
// invalid selected item
                    break;

                
// get IP address to use
                GetWindowText(GetDlgItem(g_hwnd, IDC_IP), ip, 16);
               
                
// Make sure it's valid and start session
                if(selected < g_num_adapters)
                {
                    
// pointer adapter pointer to selected position
                    adapter_ptr  = g_adapter_list;
                    adapter_ptr += selected;

                    
if(! Start_Session(&adapter_ptr->guid, ip))
                        MessageBox(hwnd, "Unable to start client!", "Error", MB_OK | MB_ICONEXCLAMATION);
                }
            }
            
else
                Stop_Session();

            
break;

        
case IDC_SEND:
            GetWindowText(GetDlgItem(hwnd, IDC_MESSAGE), msg, 256);

            SetWindowText(GetDlgItem(hwnd, IDC_MESSAGE), "");

            Send_Text_Msg(msg);

            
break;
        }   
// end - LOWORD(wParam):

        
break;

    
case WM_DESTROY:
        Stop_Session();
        Release_DirectPlay();
        PostQuitMessage(0);
        
break;

    
default:
        
return (long) DefWindowProc(hwnd, msg_id, wParam, lParam);
    }

    
return 0;
}

//--------------------------------------------------------------------------------
// 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_CLIENT), 0, NULL);    

    
// 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);
        
goto exit;
    }

    
// enumerate all TCP/IP adapters
    Enum_Adapters();

    
// Make sure there's an adapter to use
    if(g_num_adapters == 0)
    {
        MessageBox(g_hwnd, "There is no TCP/IP adapters to use!", "ERROR", MB_OK | MB_ICONEXCLAMATION);
        
goto exit;
    }

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

    
// 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);            
        }             
    }

exit:
    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;
}
 

該程序必須和使用DirectPlay進(jìn)行網(wǎng)絡(luò)互聯(lián)(3)中提及的服務(wù)器端程序配合使用。

運行截圖:



posted on 2007-08-13 22:36 lovedday 閱讀(884) 評論(0)  編輯 收藏 引用

公告

導(dǎo)航

統(tǒng)計

常用鏈接

隨筆分類(178)

3D游戲編程相關(guān)鏈接

搜索

最新評論

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            欧美四级伦理在线| 亚洲成色www8888| 国产精品视频免费一区| 欧美日韩一区二区高清| 欧美婷婷久久| 国产精品v亚洲精品v日韩精品| 欧美激情一区二区三区不卡| 欧美 日韩 国产 一区| 欧美激情1区2区| 国产精品久久久久高潮| 国产日韩精品久久| 亚洲第一福利社区| 亚洲天堂av电影| 久久视频一区二区| 日韩一级精品视频在线观看| 亚洲视频视频在线| 久久夜色精品国产欧美乱| 欧美精品二区| 国产综合久久久久影院| 一本一本久久a久久精品牛牛影视| 亚洲一区在线免费观看| 久久综合婷婷| 亚洲视频欧美在线| 欧美高清视频在线播放| 国产一区二区久久久| 在线亚洲一区观看| 欧美激情网友自拍| 欧美一区二区私人影院日本 | 久久一区欧美| 国产精品久久九九| 亚洲精品国产品国语在线app| 欧美一级黄色网| 91久久精品日日躁夜夜躁欧美 | 亚洲免费观看高清在线观看| 亚洲欧美一区二区视频| 欧美片在线播放| 欧美精品久久久久久| 欧美理论片在线观看| 国产精品国产三级国产普通话99 | 欧美日韩午夜剧场| 伊人久久亚洲美女图片| 午夜亚洲一区| 一区二区三区黄色| 欧美乱大交xxxxx| 激情校园亚洲| 久久精品123| 亚洲女ⅴideoshd黑人| 国产精品igao视频网网址不卡日韩| 亚洲大胆女人| 欧美高清成人| 蜜桃久久精品乱码一区二区| 狠狠色2019综合网| 久久久久久亚洲精品杨幂换脸| 中文精品一区二区三区| 欧美日韩国产片| av成人免费在线| 亚洲精品国精品久久99热一| 免费人成网站在线观看欧美高清| 合欧美一区二区三区| 久久久久成人精品| 久久精品国产在热久久 | 亚洲最黄网站| 亚洲人成网站999久久久综合| 米奇777在线欧美播放| 亚洲国产精品一区二区久| 欧美激情视频一区二区三区免费 | 亚洲一区尤物| 国产精品中文字幕欧美| 欧美一区二区三区在| 欧美一区免费| 亚洲国产精品国自产拍av秋霞| 欧美国产日产韩国视频| 欧美高清在线一区| 亚洲女同在线| 欧美主播一区二区三区| 亚洲国产精品传媒在线观看| 最新成人在线| 国产精品免费区二区三区观看| 欧美在线视频日韩| 久久久久久尹人网香蕉| 亚洲伦理精品| 亚洲性夜色噜噜噜7777| 激情久久久久| 日韩午夜剧场| 国产一区二区久久久| 欧美激情一区二区久久久| 欧美午夜视频在线观看| 久久久99久久精品女同性| 欧美福利视频在线观看| 亚洲你懂的在线视频| 一区二区三区四区五区在线| 亚洲免费av网站| 一本一道久久综合狠狠老精东影业 | 久久久久久综合网天天| 亚洲欧洲午夜| 亚洲午夜电影| 亚洲电影观看| 宅男精品视频| 亚洲国产一区二区精品专区| 亚洲一级一区| 99av国产精品欲麻豆| 欧美怡红院视频| 亚洲午夜精品视频| 美女尤物久久精品| 欧美在线免费播放| 欧美日韩在线观看视频| 免费成人小视频| 国产欧美一区二区三区久久人妖| 亚洲激情专区| 在线精品视频一区二区三四| 亚洲先锋成人| 亚洲一区二区三区激情| 另类酷文…触手系列精品集v1小说| 亚洲一区在线视频| 欧美精品综合| 亚洲承认在线| 在线观看亚洲一区| 欧美在线观看网站| 久久精品观看| 国产精品推荐精品| 亚洲视频999| 亚洲在线视频观看| 欧美日韩免费一区| 亚洲第一精品夜夜躁人人爽| 国产字幕视频一区二区| 午夜欧美视频| 欧美一级大片在线免费观看| 欧美日韩综合久久| 亚洲精品在线观| 一区二区久久| 欧美精品免费播放| 91久久国产综合久久| 亚洲国产精品一区二区第四页av| 久久精品99国产精品日本| 久久久久久久网站| 在线不卡中文字幕| 麻豆精品一区二区综合av| 欧美福利视频在线观看| 亚洲精品国精品久久99热一| 欧美ed2k| 一本大道久久a久久精二百| 中日韩美女免费视频网站在线观看| 欧美美女bbbb| 中文精品99久久国产香蕉| 亚洲欧美激情诱惑| 国产午夜精品美女视频明星a级| 性欧美videos另类喷潮| 久久男人av资源网站| 最新中文字幕亚洲| 欧美日韩亚洲综合一区| 中文在线不卡| 久久久高清一区二区三区| 一本色道久久精品| 国产精品国产三级国产| 午夜精品久久久久久久99水蜜桃| 亚洲欧美日本国产专区一区| 国产日韩欧美91| 免费成人av在线看| 日韩午夜黄色| 久久久亚洲高清| 亚洲美女尤物影院| 国产精品自在线| 麻豆精品网站| 亚洲一区二区三区国产| 久久综合导航| 宅男精品视频| 国内在线观看一区二区三区| 欧美 日韩 国产在线| 一区二区日韩| 麻豆精品传媒视频| 亚洲一区在线免费| 亚洲娇小video精品| 国产精品视频免费观看www| 欧美成人午夜激情| 欧美一区二区在线免费观看| 欧美激情一区二区在线| 欧美在线观看网站| 一本大道久久a久久综合婷婷| 国产亚洲精品自拍| 欧美日一区二区在线观看| 欧美一站二站| 一区二区日本视频| 亚洲国产欧美国产综合一区| 欧美一区二区视频网站| 亚洲精品一区中文| 激情婷婷欧美| 国产日韩欧美三区| 国产精品久久精品日日| 欧美成人精品一区| 久久久天天操| 久久aⅴ乱码一区二区三区| 日韩视频在线观看免费| 欧美国产欧美综合| 美国十次了思思久久精品导航| 欧美亚洲尤物久久| 亚洲免费在线观看| 亚洲午夜av在线| 在线亚洲自拍| 一本久久a久久免费精品不卡| 亚洲电影欧美电影有声小说|