?? network1.cpp
字號:
#include "network.h"
#include "marco.h"
LPDIRECTPLAY DP_lpIDC=NULL; // DirectPlay Object
DPID DP_dcoID=0; // 用于存儲本玩家在網(wǎng)絡(luò)中ID值
LPGUID DP_g_lpGuid; // 用于保存本游戲的GUID的指針
HANDLE DP_dphEvent = NULL;
GUID DP_pGuid=CB_GUID; //用于保存本游戲的GUID
//用于保存DirectPlay所提供的網(wǎng)絡(luò)服務(wù)的信息
struct EnumCallback DP_EnumCb[10];
//用于保存在網(wǎng)絡(luò)中已經(jīng)存在的Session的一些信息
struct EnumSessionCallback DP_EnumSessionCb[256];
//用于保存網(wǎng)絡(luò)對戰(zhàn)中每個玩家的名字和網(wǎng)絡(luò)ID值
PlayerInfo DP_PlayerInformation[MAX_PLAYERS];
HWND DP_hWndMain=NULL;//用于保存游戲中所使用的窗口句柄
int DP_IsHost;//用于標示本進程在游戲中是否為主機,留著可能擴充使用
BYTE DP_CommBuff[200][BUFFER_MAX_BYTE];//此二維數(shù)組用做循環(huán)消息隊列,存儲網(wǎng)絡(luò)消息
BYTE DP_SendMessageBuff[BUFFER_MAX_BYTE];//用于存儲玩家要發(fā)送的消息
BYTE DP_ReadMessage[BUFFER_MAX_BYTE];//用于將消息隊列中的消息讀出并傳給調(diào)用者
int DP_ProviderNO=-1;//用于返回玩家對網(wǎng)絡(luò)服務(wù)的選擇
int DP_CreateOrConnect=-1;//用于返回玩家對建立還是聯(lián)接游戲的選擇
int DP_SessionNO=-1;//用于返回玩家對已經(jīng)創(chuàng)建的游戲Session的選擇
int DP_MessageQueueStart;//標示消息隊列的起始序號
int DP_MessageQueueEnd;//標示消息隊列的結(jié)束序號
int DP_MessageQueueFull;//標示消息隊列是否滿了
int DP_NumPlayer=0;//標示已經(jīng)有的游戲人數(shù)
LPSTR DP_FullName;//玩家給Session起的名字
LPSTR DP_NickName;//玩家給本人起的在游戲中的名字
int DP_ProviderNum=0;
//////////////////////////用于給GAME結(jié)構(gòu)賦值////////////////////////////
extern struct GAME_STRUCT GAME;
extern HWND hwndGame;//用于發(fā)送消息給LHJ!
//////////////////////////用于給GAME結(jié)構(gòu)賦值////////////////////////////
//網(wǎng)絡(luò)部分和外部模塊的接口
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 );
}
//用于在玩家離開游戲時,釋放各指針和恢復(fù)一些值的初始值
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 );
}
//用于最開始時,獲得系統(tǒng)提供的各種服務(wù)的類型
int DP_RemoteCreateStep1( HWND MainWndHandle )
{
// DPSESSIONDESC dpDesc;
// HRESULT hr;
int status;
int i;
int j;
//這一部份是對網(wǎng)絡(luò)模塊的一些變量進行初始化
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.
//確定本玩家并不在網(wǎng)絡(luò)中
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提供的網(wǎng)絡(luò)服務(wù)類型
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 );
}
//當(dāng)玩家對系統(tǒng)提供的各種服務(wù)的類型作出了選擇以后,建立此項服務(wù)
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);
}
//建立玩家所選擇的網(wǎng)絡(luò)服務(wù)
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值進行判斷可能,因為可能存在網(wǎng)絡(luò)建立沒有錯誤,但是本地ID值卻仍然為空的現(xiàn)象
//這步判斷只是為了增加安全性,按理說上面的判斷通過了,就不應(yīng)該出現(xiàn)本地ID值為空的現(xiàn)象
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 )
{
//判斷玩家的選擇是建立游戲或者是聯(lián)接游戲
//如果傳過來的參數(shù)出現(xiàn)錯誤,則返回出錯信息給調(diào)用者
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;
}
//根據(jù)玩家的選擇,創(chuàng)建一個Session或者聯(lián)入一個Session中
switch(DP_CreateOrConnect)//CreateGame())
{
//創(chuàng)建一個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;
//聯(lián)入一個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
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -