⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 network1.cpp

📁 赤壁之战(游戏原码)
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*						DP_MessageQueueEnd++;
						DP_MessageQueueEnd=DP_MessageQueueEnd%20;
						if(DP_MessageQueueEnd==DP_MessageQueueStart)
							{
								#ifdef	DEBUG
								MessageBox(DP_hWndMain,"Message Queue Full!","  Error",MB_OK);
								#endif
								DP_MessageQueueFull=FULL;
								return	(DP_MESSAGE_QUEUE_FULL);
							}
						SendMessage(DP_hWndMain,DP_MESSAGE_RECEIVED,NULL,NULL);*/
//						return( OK );
					}
				else
					{
						return( OK );
					}
			}
		else
			{
				if(fromID==DP_dcoID)
					{
						for(j=0;j<BUFFER_MAX_BYTE;j++)
							{
								DP_ReadMessage[j]='\0';
							}
					}
				else
					{
						if(status!=DP_OK)
							{
								return ( OK );
							}
					}
			}
    }
	return( OK );
}

  
//该函数是用来发送玩家的消息给网络中的同伴
int	 DP_SendGameMessages(/*DWORD to*/)
{
    int             nBytes;
	HRESULT			hr;
//	int				i;
    
//	strcpy((char*)DP_SendMessageBuff,"a");
	nBytes=sizeof(DP_SendMessageBuff);
    // Broadcast it to everyone in the group.
//	for(i=0;i<MAX_PLAYERS;i++)
//		{
//			if(DP_PlayerInformation[i].DP_dcoID==DP_dcoID)
//				{continue;}
//			if(DP_PlayerInformation[i].DP_dcoID==0)
//				{break;}
			hr=-1;
//			while(hr!=DP_OK)
				{
					hr=DP_lpIDC->/*lpVtbl->*/Send(// DP_lpIDC,
												 DP_dcoID,   // From
												 0, // send to everybody
												 DPSEND_GUARANTEE,
												 (LPSTR)DP_SendMessageBuff,
												 512);       
		
/*	if(hr!=DP_OK)
		{
			#ifdef	DEBUG
			MessageBox(DP_hWndMain,"Send Message Error!"," Error",MB_OK);
			#endif
			return	(DP_ERROR_SEND_MESSAGE);
		}*/

					switch ( hr )
						{
							case	DPERR_INVALIDPARAMS:	
									return( DP_ERROR_INVALID_PARAMS );
									break;
							case	DPERR_INVALIDOBJECT:
									return( DP_ERROR_INVALID_OBJECT );
									break;
							case	DPERR_INVALIDPLAYER:
									return( DP_ERROR_INVALID_PLAYER );
									break;
							case	DPERR_BUSY:
									return( DP_ERROR_BUSY );
									break;

						}
				}
//	hr=DP_lpIDC->/*lpVtbl->*/Send(// DP_lpIDC,
/*									 DP_dcoID,   // From
									 0, // send to everybody
									 DPSEND_GUARANTEED,
									 (LPSTR)DP_SendMessageBuff,
									 nBytes);       

	switch ( hr )
		{
			case	DPERR_INVALIDPARAMS:	
					return( DP_ERROR_INVALID_PARAMS );
					break;
			case	DPERR_INVALIDOBJECT:
					return( DP_ERROR_INVALID_OBJECT );
					break;
			case	DPERR_INVALIDPLAYER:
					return( DP_ERROR_INVALID_PLAYER );
					break;
			case	DPERR_BUSY:
					return( DP_ERROR_BUSY );
					break;
		}*/

//	DP_ReceiveGameMessages();
	memset(DP_SendMessageBuff,0,BUFFER_MAX_BYTE);
	return (OK);

}


//当创建了新的玩家以后,调用此函数用来向其它的玩家发送添加玩家的消息
int		DP_SendAddPlayers( int SendToWho )
{

	int		i;
	int		j;
	int		nBytes;
	int		hr;

	switch( SendToWho )
		{
			case	DP_SEND_TO_SERVER:
					sprintf((char *)DP_SendMessageBuff,"%d %s",ADD_NEW_PLAYER_TO_SERVER,(char *)DP_NickName);
					nBytes=strlen((char *)DP_SendMessageBuff);
					// Broadcast it to everyone in the group.
					hr=-1;
					hr=DP_lpIDC->/*lpVtbl->*/Send(// DP_lpIDC,
													 DP_dcoID,   // From
													 0, // send to everybody
													 DPSEND_GUARANTEE,
													 (LPSTR)DP_SendMessageBuff,
													 nBytes);       

					if((hr!=OK)&&(hr!=DP_ERROR_INVALID_PLAYER))
						{
							#ifdef	DEBUG
							MessageBox(DP_hWndMain,"Send Add New Player Error!"," Error",MB_OK);
							#endif
							return ( hr );
						}
					break;
			case	DP_SEND_TO_ALL:
					if( DP_IsHost !=DP_HOST )
						{
							return( DP_IS_NOT_HOST );
						}
					else
						{
							for(j=0;j<MAX_PLAYERS;j++)
								{
									sprintf((char *)DP_SendMessageBuff,"%d %d %d %s",ADD_PLAYERS_TO_ALL,j,DP_PlayerInformation[j].DP_dcoID,(char *)DP_PlayerInformation[j].DP_NickName);
									i=DP_SendGameMessages();
									if((i!=OK)&&(i!=DP_ERROR_INVALID_PLAYER))
										{
											#ifdef	DEBUG
											MessageBox(DP_hWndMain,"Send Add New Player Error!"," Error",MB_OK);
											#endif
											return ( i );
										}
								}
						}
					break;
		}			

	return ( OK );

}


//用于接收消息的时候,判断是否是添加玩家,是则在玩家信息队列中添加上
int		DP_AddPlayers( DPID		fromID )
{

//	int		j;
	int		k;
//	int		t;
	int		MessageType;
	char	Name[256];
//	char	Event[256];
	int		Number;
	
	
	switch( DP_IsHost )
		{
			case	DP_HOST:
/*					strcpy(str,ADD_NEW_PLAYER_TO_SERVER);
					for(j=0;j<strlen(ADD_NEW_PLAYER_TO_SERVER);j++)
						{
							if(DP_CommBuff[DP_MessageQueueEnd][j]!=str[j])
							{j=-1;break;}
						}*/
					sscanf((char *)DP_ReadMessage,"%d ",&MessageType);
					if(MessageType==ADD_NEW_PLAYER_TO_SERVER)
//					if(j>0)
						{
							for(k=0;k<MAX_PLAYERS;k++)
								{
									if(DP_PlayerInformation[k].DP_dcoID==fromID)
										{
											return ( OK );
										}
								}
							for(k=0;k<MAX_PLAYERS;k++)
								{if(DP_PlayerInformation[k].DP_dcoID==0)break;}
							DP_PlayerInformation[k].DP_dcoID=fromID;
							sscanf((char *)DP_ReadMessage,"%d %s",&MessageType,Name);
							strcpy(DP_PlayerInformation[k].DP_NickName,Name);
/*							while(DP_CommBuff[DP_MessageQueueEnd][j]!=' '){}
							j++;
							t=0;
							while(DP_CommBuff[DP_MessageQueueEnd][j]!='\0')
								{
									DP_PlayerInformation[k].DP_NickName[t]=DP_CommBuff[DP_MessageQueueEnd][j];
									t++;
									j++;
								}
							DP_PlayerInformation[k].DP_NickName[t]='\0';*/
							DP_NumPlayer++;
							for(int	SetName=0;SetName<MAX_PLAYERS;SetName++)
								{
									if(DP_PlayerInformation[SetName].DP_dcoID==0){continue;}
									strcpy(GAME.Players[SetName+1].strName,DP_PlayerInformation[SetName].DP_NickName);
								}
							PostMessage( hwndGame, WM_COMMAND, NET_UPDATESETTING, 0 );
							if(DP_SendAddPlayers( DP_SEND_TO_ALL )!=OK)
								{
									return( DP_ERROR_SEND_MESSAGE );
								}
							return ( OK );
						}
					else
						{
/*							strcpy(str,ADD_PLAYERS_TO_ALL);
							for(j=0;j<strlen(ADD_PLAYERS_TO_ALL);j++)
								{
									if(DP_CommBuff[DP_MessageQueueEnd][j]!=str[j])
									{j=-1;break;}
								}*/
							sscanf((char *)DP_ReadMessage,"%d ",&MessageType);
							if(MessageType==ADD_PLAYERS_TO_ALL)
//							if(j>0)
								{return( OK );}
							else
								{return( DP_NOT_ADD_PLAYER );}
						}
					break;
			case	DP_JOIN:
/*					strcpy(str,ADD_NEW_PLAYER_TO_SERVER);
					for(j=0;j<strlen(ADD_NEW_PLAYER_TO_SERVER);j++)
						{
							if(DP_CommBuff[DP_MessageQueueEnd][j]!=str[j])
							{j=-1;break;}
						}*/
					sscanf((char *)DP_ReadMessage,"%d ",&MessageType);
					if(MessageType==ADD_NEW_PLAYER_TO_SERVER)
//					if(j>0)
						{return( OK );}
					else
						{
/*							strcpy(str,ADD_PLAYERS_TO_ALL);
							for(j=0;j<strlen(ADD_PLAYERS_TO_ALL);j++)
								{
									if(DP_CommBuff[DP_MessageQueueEnd][j]!=str[j])
									{j=-1;break;}
								}*/
							sscanf((char *)DP_ReadMessage,"%d ",&MessageType);
							if(MessageType==ADD_PLAYERS_TO_ALL)
//							if(j>0)
								{
									int		ID;
									sscanf((char *)DP_ReadMessage,"%d %d %d %s",&MessageType,&Number,&ID,Name);
									DP_PlayerInformation[Number].DP_dcoID=ID;
									strcpy(DP_PlayerInformation[Number].DP_NickName,Name);

									for(int	SetName=0;SetName<MAX_PLAYERS;SetName++)
										{
											if(DP_PlayerInformation[SetName].DP_dcoID==0){continue;}
											strcpy(GAME.Players[SetName+1].strName,DP_PlayerInformation[SetName].DP_NickName);
										}
//									PostMessage( hwndGame, WM_COMMAND, NET_UPDATESETTING, 0 );

								}
							else
								{return( DP_NOT_ADD_PLAYER );}
						}
					break;
			default	:
/*					strcpy(str,ADD_NEW_PLAYER_TO_SERVER);
					for(j=0;j<strlen(ADD_NEW_PLAYER_TO_SERVER);j++)
						{
							if(DP_CommBuff[DP_MessageQueueEnd][j]!=str[j])
							{j=-1;break;}
						}*/
					sscanf((char *)DP_ReadMessage,"%d ",&MessageType);
					if(MessageType==ADD_NEW_PLAYER_TO_SERVER)
//					if(j>0)
						{return( OK );}
					else
						{
/*							strcpy(str,ADD_PLAYERS_TO_ALL);
							for(j=0;j<strlen(ADD_PLAYERS_TO_ALL);j++)
								{
									if(DP_CommBuff[DP_MessageQueueEnd][j]!=str[j])
									{j=-1;break;}
								}*/
							sscanf((char *)DP_ReadMessage,"%d ",&MessageType);
							if(MessageType==ADD_PLAYERS_TO_ALL)
//							if(j>0)
								{return( OK );}
							else
								{return( DP_NOT_ADD_PLAYER );}
						}

					break;

//			SendMessage(DP_hWndMain,DP_MESSAGE_ADD_PLAYER,NULL,NULL);
		}

	return( DP_NOT_ADD_PLAYER );
}


//从消息队列中读取一条消息返回给调用者
int		DP_ReadMessageQueue( void )
{
//	int i;


	if(DP_MessageQueueStart!=DP_MessageQueueEnd)
		{
			memcpy(DP_ReadMessage,DP_CommBuff[DP_MessageQueueStart],strlen((char *)DP_CommBuff[DP_MessageQueueStart])+1);
//			for(i=0;i<=BUFFER_MAX_BYTE;i++)
//				{DP_CommBuff[DP_MessageQueueStart][i]='\0';}
			memset(DP_CommBuff[DP_MessageQueueStart],0,BUFFER_MAX_BYTE-1);
			DP_MessageQueueStart++;
			DP_MessageQueueStart=DP_MessageQueueStart%20;
			DP_MessageQueueFull=NO_FULL;
			return( OK );
		}
	else
		{
			return( DP_MESSAGE_QUEUE_EMPTY );
		}

	return( OK );

}

	
//当删除一个玩家时,调用此函数用来向其它的玩家发送删除玩家的消息
int		DP_SendDestroyPlayers( void )
{

	int		i;

	sprintf((char *)DP_SendMessageBuff,"%d ",DESTROY_PLAYER);
	i=DP_SendGameMessages();
	if(i!=OK)
		{
			#ifdef	DEBUG
			MessageBox(DP_hWndMain,"Send Destroy Player Error!"," Error",MB_OK);
			#endif
			return ( i );
		}
	return ( OK );

}


//用于接收消息的时候,判断是否是删除玩家,是则从玩家信息队列中删除它
int		DP_DestroyPlayers( DPID		fromID )
{

	int		i;
//	int		j;
	int		k;
	int		MessageType;

	
/*	strcpy(str,DESTROY_PLAYER);
	for(j=0;j<strlen(DESTROY_PLAYER);j++)
		{
			if(DP_CommBuff[DP_MessageQueueEnd][j]!=str[j])
				{j=-1;break;}
		}*/
	sscanf((char *)DP_ReadMessage,"%d ",&MessageType);
	if(MessageType==DESTROY_PLAYER)
		{
			for(k=0;k<DP_NumPlayer;k++)
				{
					if(DP_PlayerInformation[k].DP_dcoID==fromID)
						{
/*							for(j=k;j<DP_NumPlayer;j++);
								{
									DP_PlayerInformation[k].DP_dcoID=DP_PlayerInformation[k+1].DP_dcoID;
									i=0;
									while(DP_PlayerInformation[k+1].DP_NickName[i]!='\0')
										{
											DP_PlayerInformation[k].DP_NickName[i]=DP_PlayerInformation[k+1].DP_NickName[i];
											i++;
										}
									DP_PlayerInformation[k].DP_NickName[i]='\0';
								}*/
							DP_PlayerInformation[k].DP_dcoID=0;
							for(i=0;i<MAX_NAME_LENGTH;i++)
								{
									DP_PlayerInformation[k].DP_NickName[i]='\0';
								}
							DP_NumPlayer=DP_NumPlayer-1;
							return ( OK );
						}
				}
/*			DP_PlayerInformation[DP_NumPlayer].DP_dcoID=fromID;
			while(DP_CommBuff[DP_MessageQueueEnd][j]!=' '){}
			j++;
			k=0;
			while(DP_CommBuff[DP_MessageQueueEnd][j]!='\0')
				{
					DP_PlayerInformation[DP_NumPlayer].DP_NickName[k]=DP_CommBuff[DP_MessageQueueEnd][j];
					k++;
					j++;
				}
			DP_PlayerInformation[DP_NumPlayer].DP_NickName[k]='\0';
			if(DP_SendAddPlayers()!=OK)
				{
					return( DP_ERROR_SEND_MESSAGE );
				}
			return ( OK );*/
			return ( DP_ERROR_DEL_EMPTY_PLAYER );
		}

	return( DP_NOT_DESTROY_PLAYER );
}


//本函数是用来查询本玩家在整个Session中的具体位置
int	DP_QueryOwnInfor()
{
	int		i;

	for(i=0;i<MAX_PLAYERS;i++)
		{
			if(DP_PlayerInformation[i].DP_dcoID==DP_dcoID)
				{i=i+1;return(i);break;}
		}
	return(DP_NOT_FIND_PLAYER);
}
		



//本函数是用来查询本玩家选用的网络服务的类型
int	DP_QueryProvider()
{

	if(DP_ProviderNO>0)
		{return(DP_ProviderNO);}
	else
		{return(DP_ERROR_QUERY_PROVIDER_INFOR);}
}
		
			

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -