本篇是创建游戏内核(22)的续篇,涉及到的DirectPlay基础知识请参阅使用DirectPlay进行网络互联(1)。
网络内核包括三个类:NETWORK_ADAPTER,NETWORK_SERVER,NETWORK_CLIENT。
使用NETWORK查询网络适配器
NETWORK_ADAPTER类用于枚举系统上安装了的TCP/IP设备。为了同客户端建立连接,必须知道设备的GUID,这正是NETWORK_ADAPTER类的用途。
来看看NETWORK_ADAPTER类的定义:
//====================================================================================
// This class encapsulate for network adapters.
//====================================================================================
typedef class NETWORK_ADAPTER
{
public:
NETWORK_ADAPTER();
~NETWORK_ADAPTER();
BOOL init();
void shutdown();
long get_num_adapters();
BOOL get_name(ulong index, char* name);
GUID* get_guid(ulong index);
static HRESULT WINAPI net_msg_handler(PVOID user_contex, DWORD msg_id, PVOID msg_buffer);
private:
DPN_SERVICE_PROVIDER_INFO* _adapter_list;
ulong _num_adapters;
} *NETWORK_ADAPTER_PTR;
接着是它的实现:
//---------------------------------------------------------------------------------
// Callback function that receives all messages from the server, and receives indications
// of session changes from the IDirectPlay8Client interface.
//---------------------------------------------------------------------------------
HRESULT WINAPI NETWORK_ADAPTER::net_msg_handler(PVOID user_contex, DWORD msg_id, PVOID msg_buffer)
{
return S_OK;
}
//---------------------------------------------------------------------------------
// Constructor, initialize member data.
//---------------------------------------------------------------------------------
NETWORK_ADAPTER::NETWORK_ADAPTER()
{
_adapter_list = NULL;
}
//---------------------------------------------------------------------------------
// Destructor, release resource.
//---------------------------------------------------------------------------------
NETWORK_ADAPTER::~NETWORK_ADAPTER()
{
shutdown();
}
//---------------------------------------------------------------------------------
// Release resource.
//---------------------------------------------------------------------------------
void NETWORK_ADAPTER::shutdown()
{
delete[] _adapter_list;
_adapter_list = NULL;
_num_adapters = 0;
}
//---------------------------------------------------------------------------------
// Enumerate adapter lists.
//---------------------------------------------------------------------------------
BOOL NETWORK_ADAPTER::init()
{
IDirectPlay8Client* client = NULL;
DWORD size = 0;
// free allocated resource first
shutdown();
// create DirectPlay client component
if(FAILED(CoCreateInstance(CLSID_DirectPlay8Client, NULL, CLSCTX_INPROC, IID_IDirectPlay8Client, (void**) &client)))
return FALSE;
// assign a message handler to network component
if(FAILED(client->Initialize(NULL, net_msg_handler, 0)))
goto fail;
// query the required size of the data buffer
if(client->EnumServiceProviders(&CLSID_DP8SP_TCPIP, NULL, _adapter_list, &size, &_num_adapters, 0)
!= DPNERR_BUFFERTOOSMALL)
goto fail;
// allocate a buffer
if((_adapter_list = (DPN_SERVICE_PROVIDER_INFO*) new BYTE[size]) == NULL)
goto fail;
// enumerate again
if(FAILED(client->EnumServiceProviders(&CLSID_DP8SP_TCPIP, NULL, _adapter_list, &size, &_num_adapters, 0)))
goto fail;
client->Release();
return TRUE;
fail:
client->Release();
return FALSE;
}
//---------------------------------------------------------------------------------
// Get number of adapters.
//---------------------------------------------------------------------------------
long NETWORK_ADAPTER::get_num_adapters()
{
return _num_adapters;
}
//---------------------------------------------------------------------------------
// Get adapter name.
//---------------------------------------------------------------------------------
BOOL NETWORK_ADAPTER::get_name(ulong index, char* name)
{
if(_adapter_list == NULL || index >= _num_adapters)
return FALSE;
// name must be at least MAX_PATH bytes in size
wcstombs(name, _adapter_list[index].pwszName, MAX_PATH);
return TRUE;
}
//---------------------------------------------------------------------------------
// Return pointer to adapter GUID.
//---------------------------------------------------------------------------------
GUID* NETWORK_ADAPTER::get_guid(ulong index)
{
if(_adapter_list == NULL || index >= _num_adapters)
return FALSE;
return &_adapter_list[index].guid;
}
测试代码:
/*****************************************************************************
PURPOSE:
Test for class NETWORK_ADAPTER.
*****************************************************************************/
#include "Core_Global.h"
class APP : public APPLICATION
{
public:
BOOL init()
{
if(! _adapter.init())
return FALSE;
char buffer[2048] = {0};
char name[MAX_PATH] = {0};
char temp[MAX_PATH] = {0};
for(long i = 0; i < _adapter.get_num_adapters(); i++)
{
_adapter.get_name(i, name);
sprintf(temp, "%d: %s\n", i, name);
strcat(buffer, temp);
}
MessageBox(NULL, buffer, "adapter_test", MB_OK);
return TRUE;
}
BOOL frame()
{
return TRUE;
}
BOOL shutdown()
{
return TRUE;
}
private:
NETWORK_ADAPTER _adapter;
};
int PASCAL WinMain(HINSTANCE inst, HINSTANCE, LPSTR cmd_line, int cmd_show)
{
APP app;
return app.run();
}
下载源码和工程
程序截图: