📄 network1.cpp
字号:
#include "network.h"
#include "marco.h"
LPDIRECTPLAY DP_lpIDC=NULL; // DirectPlay Object
DPID DP_dcoID=0; // 用于存储本玩家在网络中ID值
LPGUID DP_g_lpGuid; // 用于保存本游戏的GUID的指针
HANDLE DP_dphEvent = NULL;
GUID DP_pGuid=CB_GUID; //用于保存本游戏的GUID
//用于保存DirectPlay所提供的网络服务的信息
struct EnumCallback DP_EnumCb[10];
//用于保存在网络中已经存在的Session的一些信息
struct EnumSessionCallback DP_EnumSessionCb[256];
//用于保存网络对战中每个玩家的名字和网络ID值
PlayerInfo DP_PlayerInformation[MAX_PLAYERS];
HWND DP_hWndMain=NULL;//用于保存游戏中所使用的窗口句柄
int DP_IsHost;//用于标示本进程在游戏中是否为主机,留着可能扩充使用
BYTE DP_CommBuff[200][BUFFER_MAX_BYTE];//此二维数组用做循环消息队列,存储网络消息
BYTE DP_SendMessageBuff[BUFFER_MAX_BYTE];//用于存储玩家要发送的消息
BYTE DP_ReadMessage[BUFFER_MAX_BYTE];//用于将消息队列中的消息读出并传给调用者
int DP_ProviderNO=-1;//用于返回玩家对网络服务的选择
int DP_CreateOrConnect=-1;//用于返回玩家对建立还是联接游戏的选择
int DP_SessionNO=-1;//用于返回玩家对已经创建的游戏Session的选择
int DP_MessageQueueStart;//标示消息队列的起始序号
int DP_MessageQueueEnd;//标示消息队列的结束序号
int DP_MessageQueueFull;//标示消息队列是否满了
int DP_NumPlayer=0;//标示已经有的游戏人数
LPSTR DP_FullName;//玩家给Session起的名字
LPSTR DP_NickName;//玩家给本人起的在游戏中的名字
int DP_ProviderNum=0;
//////////////////////////用于给GAME结构赋值////////////////////////////
extern struct GAME_STRUCT GAME;
extern HWND hwndGame;//用于发送消息给LHJ!
//////////////////////////用于给GAME结构赋值////////////////////////////
//网络部分和外部模块的接口
int DP_NetworkInterface(int Action, HWND MainWndHandle)
{
int i=0;
switch( Action )
{
case DP_INIT_NETWORK_STEP_1:
i=DP_RemoteCreateStep1( MainWndHandle );
if( i!=OK )
{
return (i);
}
break;
case DP_INIT_NETWORK_STEP_2:
i=DP_RemoteCreateStep2();
if( i!=OK )
{
return(i);
}
break;
case DP_CREATE_SESSION:
i=DP_CreateSession();
if( i!=OK )
{
return (i);
}
break;
case DP_GET_SESSION:
i=DP_GetSession();
if( i!=OK )
{
return (i);
}
break;
case DP_CONNECT_SESSION:
i=DP_ConnectSession();
if( i!=OK )
{
return (i);
}
break;
case DP_SEND_MESSAGE:
i=DP_SendGameMessages();
if( i!=OK )
{
return (i);
}
break;
case DP_RECEIVE_MESSAGE:
i=DP_ReceiveGameMessages();
if( i!=OK )
{
return (i);
}
break;
case DP_DESTROY_GAME:
i=DP_DestroyGame();
if( i!=OK )
{
return (i);
}
break;
case DP_READ_MESSAGE_QUEUE:
i=DP_ReadMessageQueue();
if( i!=OK )
{
return (i);
}
break;
case DP_QUERY_OWN_INFORMATION:
i=DP_QueryOwnInfor();
return (i);
break;
case DP_QUERY_PROVIDER:
i=DP_QueryProvider();
return(i);
break;
default :
#ifdef DEBUG
MessageBox(NULL,"Do not know this input Action!"," Error",MB_OK);
#endif
return ( DP_ERROR_INPUT_ACTION );
break;
}
return ( OK );
}
//用于在玩家离开游戏时,释放各指针和恢复一些值的初始值
int DP_DestroyGame( void )
{
HRESULT hr;
int i;
int j;
//将DirectPlay的对象指针释放掉
if( DP_dcoID != 0 )
{
hr = DP_lpIDC->/*lpVtbl->*/DestroyPlayer(//DP_lpIDC,
DP_dcoID);
//判断是否正确的释放了DirectPlay的对象指针
if ( hr!=DP_OK )
{
#ifdef DEBUG
MessageBox(DP_hWndMain,"Free DirectPlay Object Point Error!"," Error",MB_OK);
#endif
return (DP_ERROR_FREE_DIRECTPLAY_OBJECT);
}
}
DP_SendDestroyPlayers();
DP_ProviderNO=-1;
DP_CreateOrConnect=-1;
DP_SessionNO=-1;
DP_ProviderNum=0;
if(DP_lpIDC!=NULL)
{
DP_lpIDC->Release();
DP_lpIDC=NULL;
}
DP_dcoID=0;
for(i=0;i<10;i++)
{
DP_EnumCb[i].PointGUID.Data1=0;
DP_EnumCb[i].PointGUID.Data2=0;
DP_EnumCb[i].PointGUID.Data3=0;
DP_EnumCb[i].PointGUID.Data4[0]=(BYTE)'\0';
DP_EnumCb[i].PointGUID.Data4[1]=(BYTE)'\0';
DP_EnumCb[i].PointGUID.Data4[2]=(BYTE)'\0';
DP_EnumCb[i].PointGUID.Data4[3]=(BYTE)'\0';
DP_EnumCb[i].PointGUID.Data4[4]=(BYTE)'\0';
DP_EnumCb[i].PointGUID.Data4[5]=(BYTE)'\0';
DP_EnumCb[i].PointGUID.Data4[6]=(BYTE)'\0';
DP_EnumCb[i].PointGUID.Data4[7]=(BYTE)'\0';
for(j=0;j<256;j++)
{DP_EnumCb[i].PointDesc[j]=(BYTE)'\0';}
}
for(i=0;i<256;i++)
{
for(j=0;j<DPSESSIONNAMELEN;j++)
{
DP_EnumSessionCb[i].PointDPGameDesc.szSessionName[j]='\0';
}
DP_EnumSessionCb[i].PointDPGameDesc.dwSession=0;
}
for(i=0;i<MAX_PLAYERS;i++)
{
for(j=0;j<MAX_NAME_LENGTH;j++)
{
DP_PlayerInformation[i].DP_NickName[j]='\0';
}
DP_PlayerInformation[i].DP_dcoID=0;
}
DP_hWndMain=NULL;
DP_IsHost=DP_OTHER;
for(i=0;i<20;i++)
{
for(j=0;j<BUFFER_MAX_BYTE;j++)
{DP_CommBuff[i][j]=(BYTE)'\0';}
}
for(i=0;i<BUFFER_MAX_BYTE;i++)
{
DP_SendMessageBuff[i]=(BYTE)'\0';
}
for(i=0;i<BUFFER_MAX_BYTE;i++)
{
DP_ReadMessage[i]=(BYTE)'\0';
}
DP_MessageQueueStart=0;
DP_MessageQueueEnd=0;
DP_MessageQueueFull=NO_FULL;
DP_NumPlayer=0;
delete (DP_FullName);
DP_FullName=(LPSTR)NULL;
delete (DP_NickName);
DP_NickName=(LPSTR)NULL;
return ( OK );
}
//用于最开始时,获得系统提供的各种服务的类型
int DP_RemoteCreateStep1( HWND MainWndHandle )
{
// DPSESSIONDESC dpDesc;
// HRESULT hr;
int status;
int i;
int j;
//这一部份是对网络模块的一些变量进行初始化
DP_ProviderNO=-1;
DP_CreateOrConnect=-1;
DP_SessionNO=-1;
DP_ProviderNum=0;
// DP_lpIDC=NULL;
DP_dcoID=0;
for(i=0;i<10;i++)
{
DP_EnumCb[i].PointGUID.Data1=0;
DP_EnumCb[i].PointGUID.Data2=0;
DP_EnumCb[i].PointGUID.Data3=0;
DP_EnumCb[i].PointGUID.Data4[0]=(BYTE)'\0';
DP_EnumCb[i].PointGUID.Data4[1]=(BYTE)'\0';
DP_EnumCb[i].PointGUID.Data4[2]=(BYTE)'\0';
DP_EnumCb[i].PointGUID.Data4[3]=(BYTE)'\0';
DP_EnumCb[i].PointGUID.Data4[4]=(BYTE)'\0';
DP_EnumCb[i].PointGUID.Data4[5]=(BYTE)'\0';
DP_EnumCb[i].PointGUID.Data4[6]=(BYTE)'\0';
DP_EnumCb[i].PointGUID.Data4[7]=(BYTE)'\0';
for(j=0;j<256;j++)
{DP_EnumCb[i].PointDesc[j]=(BYTE)'\0';}
}
for(i=0;i<256;i++)
{
for(j=0;j<DPSESSIONNAMELEN;j++)
{
DP_EnumSessionCb[i].PointDPGameDesc.szSessionName[j]='\0';
}
DP_EnumSessionCb[i].PointDPGameDesc.dwSession=0;
}
for(i=0;i<MAX_PLAYERS;i++)
{
for(j=0;j<MAX_NAME_LENGTH;j++)
{
DP_PlayerInformation[i].DP_NickName[j]='\0';
}
DP_PlayerInformation[i].DP_dcoID=0;
}
DP_hWndMain=NULL;
DP_IsHost=DP_OTHER;
for(i=0;i<20;i++)
{
for(j=0;j<BUFFER_MAX_BYTE;j++)
{DP_CommBuff[i][j]=(BYTE)'\0';}
}
for(i=0;i<BUFFER_MAX_BYTE;i++)
{
DP_SendMessageBuff[i]=(BYTE)'\0';
}
for(i=0;i<BUFFER_MAX_BYTE;i++)
{
DP_ReadMessage[i]=(BYTE)'\0';
}
DP_MessageQueueStart=0;
DP_MessageQueueEnd=0;
DP_MessageQueueFull=NO_FULL;
DP_NumPlayer=0;
//将本游戏的窗口句柄(在游戏初始化的时候,由其他模块生成)保存起来
//另外还要判断一下传过来的窗口句柄是否为空
if( MainWndHandle==NULL )
{
#ifdef DEBUG
MessageBox(NULL,"The Windows Handle is empty!"," Error",MB_OK);
#endif
return (DP_WINDOWS_HANDLE_EMPTY);
}
DP_hWndMain=MainWndHandle;
// Be sure we aren't already initialized.
//确定本玩家并不在网络中
if (DP_lpIDC != NULL)
{
#ifdef DEBUG
MessageBox(DP_hWndMain,"You have be in network!"," Error",MB_OK);
#endif
DP_lpIDC->Release();
DP_lpIDC=NULL;
// return(DP_ALREADY_IN_NETWORK);
}
//得到DirectPlay提供的网络服务类型
status=DP_GetProvider(DP_hWndMain);
if(status!=OK)
{
#ifdef DEBUG
MessageBox(DP_hWndMain,"Get Network Service Error!"," Error",MB_OK);
#endif
return(DP_ERROR_GET_NETWORK_SERVICE);
}
return ( OK );
}
//当玩家对系统提供的各种服务的类型作出了选择以后,建立此项服务
int DP_RemoteCreateStep2( void )
{
if(DP_ProviderNO<0)
{
#ifdef DEBUG
MessageBox(DP_hWndMain,"Input Provider Choice Error!"," Error",MB_OK);
#endif
return (DP_ERROR_INPUT_PROVIDER);
}
//建立玩家所选择的网络服务
if (DP_DlgProcChooseProvider(DP_ProviderNO)==FALSE)
{
#ifdef DEBUG
MessageBox(DP_hWndMain,"Choose Network Service Error!"," Error",MB_OK);
#endif
return(DP_ERROR_CREATE_NETWORK_SERVICE);
}
//对本地ID值进行判断可能,因为可能存在网络建立没有错误,但是本地ID值却仍然为空的现象
//这步判断只是为了增加安全性,按理说上面的判断通过了,就不应该出现本地ID值为空的现象
if (DP_lpIDC == NULL)
{
#ifdef DEBUG
MessageBox(DP_hWndMain,"Player ID can not create!"," Error",MB_OK);
#endif
return(DP_PLAYER_ID_EMPTY);
}
return ( OK );
}
/*int RemoteCreateStep3( void )
{
//判断玩家的选择是建立游戏或者是联接游戏
//如果传过来的参数出现错误,则返回出错信息给调用者
if((DP_CreateOrConnect!=DP_CREATEGAME)&&(DP_CreateOrConnect!=DP_CONNECTGAME))
{
#ifdef DEBUG
MessageBox(DP_hWndMain,"Choice Create or Connect Error!","Error",MB_OK);
#endif
return FALSE;
}
//根据玩家的选择,创建一个Session或者联入一个Session中
switch(DP_CreateOrConnect)//CreateGame())
{
//创建一个Session
case DP_CREATEGAME://1: // Create
DP_IsHost = TRUE;
memset(&dpDesc, 0x00, sizeof(DPSESSIONDESC));
dpDesc.dwSize = sizeof(dpDesc);
dpDesc.dwMaxPlayers = MAX_PLAYERS;
dpDesc.dwFlags = DPOPEN_CREATESESSION;
dpDesc.guidSession = DP_pGuid;
strcpy( dpDesc.szSessionName, DP_FullName);
OutputDebugString( "Step 4: Create, Open\n" );
hr = DP_lpIDC->Open(//DP_lpIDC,
&dpDesc);
if (hr!= DP_OK)
{
DP_lpIDC->Release();
DP_lpIDC = NULL;
#ifdef DEBUG
MessageBox(DP_hWndMain,"Create Game Error!"," Error",MB_OK);
#endif
return(FALSE);
}
break;
//联入一个Session
case DP_CONNECTGAME://2: // Connect
DP_IsHost = FALSE;
DP_g_lpGuid = (LPGUID) &DP_pGuid;
OutputDebugString( "Step 5: Connect GetGame\n" );
if (DP_GetGame(DP_hWndMain)==DP_ERROR_ENUM_SESSION)
{
#ifdef DEBUG
MessageBox(DP_hWndMain,"Enumerate Session Error!"," Error",MB_OK);
#endif
return (DP_ERROR_ENUM_SESSION);
}
if(DP_DlgProcSelSession(DP_SessionNO)==FALSE)
{
#ifdef DEBUG
MessageBox(DP_hWndMain,"Open Session Error!"," Error",MB_OK);
#endif
return(DP_ERROR_OPEN_SESSION);
}
if (DP_lpIDC == NULL)
{
#ifdef DEBUG
MessageBox(DP_hWndMain,"Player ID is empty!"," Error",MB_OK);
#endif
return(DP_PLAYER_ID_EMPTY);
}
break;
default:
#ifdef DEBUG
MessageBox(DP_hWndMain,"Input Session Choice Error!"," Error",MB_OK);
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -