本篇是创建游戏内核(22)【OO改良版】的续篇,关于该内核的细节说明请参阅创建游戏内核(23)。
接口:
BOOL set_app_guid(const GUID* app_guid);
//====================================================================================
// 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 network_msg_handler(PVOID user_contex, DWORD msg_id, PVOID msg_buffer);
private:
DPN_SERVICE_PROVIDER_INFO* m_adapter_list;
ulong m_num_adapters;
} *NETWORK_ADAPTER_PTR;
实现:
// Default application GUID
GUID g_app_guid = { 0x71118060, 0xfdb4, 0x11d4, { 0x90, 0x89, 0xeb, 0x5b, 0x75, 0x97, 0xe5, 0x70 } };
//---------------------------------------------------------------------------------
// Set a new application GUID
//---------------------------------------------------------------------------------
BOOL set_app_guid(const GUID* app_guid)
{
if(app_guid == NULL)
return FALSE;
memcpy(&g_app_guid, app_guid, sizeof(GUID));
return TRUE;
}
////////////////////////////////// Defines for class NETWORK_ADAPTER //////////////////////////////////
//---------------------------------------------------------------------------------
// Callback function that receives all messages from the server, and receives indications
// of session changes from the IDirectPlay8Client interface.
//---------------------------------------------------------------------------------
HRESULT WINAPI NETWORK_ADAPTER::network_msg_handler(PVOID user_contex, DWORD msg_id, PVOID msg_buffer)
{
return S_OK;
}
//---------------------------------------------------------------------------------
// Constructor, initialize member data.
//---------------------------------------------------------------------------------
NETWORK_ADAPTER::NETWORK_ADAPTER()
{
m_adapter_list = NULL;
}
//---------------------------------------------------------------------------------
// Destructor, release resource.
//---------------------------------------------------------------------------------
NETWORK_ADAPTER::~NETWORK_ADAPTER()
{
shutdown();
}
//---------------------------------------------------------------------------------
// Release resource.
//---------------------------------------------------------------------------------
void NETWORK_ADAPTER::shutdown()
{
delete[] m_adapter_list;
m_adapter_list = NULL;
m_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, network_msg_handler, 0)))
goto fail;
// query the required size of the data buffer
if(_client->EnumServiceProviders(&CLSID_DP8SP_TCPIP, NULL, m_adapter_list, &_size, &m_num_adapters, 0)
!= DPNERR_BUFFERTOOSMALL)
goto fail;
// allocate a buffer
if((m_adapter_list = (DPN_SERVICE_PROVIDER_INFO*) new BYTE[_size]) == NULL)
goto fail;
// enumerate again
if(FAILED(_client->EnumServiceProviders(&CLSID_DP8SP_TCPIP, NULL, m_adapter_list, &_size, &m_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 m_num_adapters;
}
//---------------------------------------------------------------------------------
// Get adapter name.
//---------------------------------------------------------------------------------
BOOL NETWORK_ADAPTER::get_name(ulong index, char* name)
{
if(m_adapter_list == NULL || index >= m_num_adapters)
return FALSE;
// name must be at least MAX_PATH bytes in size
wcstombs(name, m_adapter_list[index].pwszName, MAX_PATH);
return TRUE;
}
//---------------------------------------------------------------------------------
// Return pointer to adapter GUID.
//---------------------------------------------------------------------------------
GUID* NETWORK_ADAPTER::get_guid(ulong index)
{
if(m_adapter_list == NULL || index >= m_num_adapters)
return FALSE;
return &m_adapter_list[index].guid;
}
测试代码:
/*****************************************************************************
PURPOSE:
Test for class NETWORK_ADAPTER.
*****************************************************************************/
#include "core_common.h"
#include "core_framework.h"
#include "core_network.h"
class APP : public FRAMEWORK
{
public:
BOOL init()
{
if(! m_adapter.init())
return FALSE;
char buffer[2048] = {0};
char name[MAX_PATH] = {0};
char temp[MAX_PATH] = {0};
for(long i = 0; i < m_adapter.get_num_adapters(); i++)
{
m_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 m_adapter;
};
int PASCAL WinMain(HINSTANCE inst, HINSTANCE, LPSTR cmd_line, int cmd_show)
{
if(! build_window(inst, "AdapterClass", "AdapterTest", WS_OVERLAPPEDWINDOW, 0, 0, 640, 480))
return -1;
APP app;
app.run();
return 0;
}
截图: