ゲームサーバ側通信フレームワーク(C++とSocket)
34564 ワード
これは小型で複数の対戦ゲームサーバーのエンドコードで、修正されています.
ファイル1:stdafx.h
ファイル2:stdafx.cpp
ファイル3:DSObject.h
ファイル4:DSObject.cpp
ファイル5:DSGameServer.h
ファイル6:DSGameServer.cpp
ファイル7:DSMain.cpp
以上のファイルをソースコードに保存し、VC++6.0でコンパイルします.コンパイル前にLinkにws 2_を追加することに注意してください.32.libライブラリ.
ファイル1:stdafx.h
//-------------------------------------------------------------------------
//stdafx.h
//
// DreamShip
//
//-------------------------------------------------------------------------
#ifndef _STDAFX_
#define _STDAFX_
#include
#include
#include
#include
#include
#include
#include "DSGameServer.h"
#include "DSObject.h"
//-------------------------------------------------------------------
//
// ds_Search
//
//
//
//------------------------------------------------------------------
float DS_ReturnFloat(char *ds_Dest,char ds_Search) ;
int DS_ReturnInt(char *ds_Dest,char ds_Search) ;
int DS_ReturnString(char *ds_Dest,char ds_Search,char *buf ) ;
int DS_ReturnCharPosition( char *ds_Dest,char ds_Search );
//
void DS_PrintTime( long time );
void DS_GetCurrentTime( char timeBuf[] );
void DS_PrintCurrentTime( char *ds_Msg );
#endif
ファイル2:stdafx.cpp
//----------------------------------------------------------------------------------
//stdafx.cpp
//
//
//----------------------------------------------------------------------------------
#include "stdafx.h"
//-------------------------------------------------------------------
//
// ds_Search
//
//
//
//------------------------------------------------------------------
float DS_ReturnFloat(char *ds_Dest,char ds_Search)
{
long time = timeGetTime();
int i=0;
while( ds_Dest[i] != '\0' )
{
if( ds_Dest[i] == ds_Search )
{
int j = 0;
i++;
char buf[50];
while( ds_Dest[i] != '*' )
{
if( ds_Dest[i] =='\0' )
return -100.0f ;
buf[j]=ds_Dest[i] ;
i++ ;
j++ ;
}
buf[j]='\0';
if( buf[0] >= '0' && buf[0] <= '9' )
return (float)atof(buf);
else
return -10000.0f;
}
i++ ;
}
printf(" :%d
",timeGetTime()-time);
return -10000.0f ;
}
//-------------------------------------------------------------------
//
// ds_Search
//
//
//
//------------------------------------------------------------------
int DS_ReturnInt(char *ds_Dest,char ds_Search)
{
long time = timeGetTime();
int i=0;
while( ds_Dest[i] != '\0' )
{
if( ds_Dest[i] == ds_Search )
{
int j = 0;
i++;
char buf[50];
while( ds_Dest[i] != '*' )
{
if( ds_Dest[i] =='\0' )
return -100 ;
buf[j]=ds_Dest[i] ;
i++ ;
j++ ;
}
buf[j]='\0';
if( buf[0] >= '0' && buf[0] <= '9' )
return atoi(buf);
else
return -10000;
}
i++ ;
}
printf(" :%d
",timeGetTime()-time);
return -10000;
}
//-------------------------------------------------------------------
//
// ds_Search
//
//
//
//------------------------------------------------------------------
int DS_ReturnString(char *ds_Dest,char ds_Search,char *buf )
{
long time = timeGetTime();
int i=0;
while( ds_Dest[i] != '\0' )
{
if( ds_Dest[i] == ds_Search )
{
int j = 0;
i++;
while( ds_Dest[i] != '*' )
{
if( ds_Dest[i] =='\0' )
return -1 ;
buf[j]=ds_Dest[i] ;
i++ ;
j++ ;
}
buf[j]='\0';
return 1;
}
i++ ;
}
printf(" :%d
",timeGetTime()-time);
return 0;
}
//---------------------------------------------------------------------------
//
//
//
//----------------------------------------------------------------------------
int DS_ReturnCharPosition(char *ds_Dest,char ds_Search)
{
long time = timeGetTime();
int i=0;
while( ds_Dest[i] != '\0' )
{
if( ds_Dest[i] == ds_Search )
return i ;
i++ ;
}
printf(" :%d
",timeGetTime()-time);
return -1;
}
//---------------------------------------------------------------------------
//
//
//
//----------------------------------------------------------------------------
void DS_PrintTime( long time )
{
printf( " :%d
" , timeGetTime()-time );
}
//-------------------------------------------------
//
//
//
//------------------------------------------------------
void DS_GetCurrentTime(char timeBuf[])
{
struct tm *p;
long ltime;
time(<ime);
p=localtime(<ime);
strftime(timeBuf,25,"%a %d %b %Y %H:%M:%S",p);
}
//---------------------------------------------------
//
//
//
//------------------------------------------
void DS_PrintCurrentTime( char *ds_Msg )
{
char buf[29];
struct tm *p;
long ltime;
// _strtime(buf);
// printf(" :\t\t\t\t%s
", buf);
// _strdate(buf);
// printf(" :\t\t\t\t%s
", buf);
time(<ime);
p=localtime(<ime);
strftime(buf,29,"%a %d %b %Y %H:%M:%S GMT",p);
printf("%s :%s
",ds_Msg,buf);
}
ファイル3:DSObject.h
//-------------------------------------------------------------
//DSObject.h
//
//
//---------------------------------------------------------------
#ifndef _DSOBJECT_
#define _DSOBJECT_
//DSENEMY DSBULLET
//DSPLAYER DSHEAD
enum OBJECT_TYPE { DSENEMY,DSBULLET,DSPLAYER,DSHEAD,DSNULL};
enum NETOBJECT_TYPE{ DSRED,DSBLUE,DSNETOTHERS,DSNETHEAD,DSNETNULL};
struct D3DXVECTOR3
{
float x ;
float y ;
float z ;
};
class DSObject
{
public :
DSObject( OBJECT_TYPE ds_Type );
DSObject();
~DSObject();
virtual HRESULT DS_InitObject();//
virtual void DS_FrameMove( );//
virtual void DS_RenderScene();//
virtual void DS_CleanUp();//
//
virtual HRESULT DS_NetFrameMove();
//
virtual void DS_NetRenderScene();
public:
DSObject *ds_Previous; //
DSObject *ds_Next; //
OBJECT_TYPE ds_Type; //
bool ds_Active;//
int ds_Health;//
int ds_ID ;// ID
NETOBJECT_TYPE ds_NetType ;//
char ds_Username[20]; //
float ds_Radius;//
D3DXVECTOR3 ds_CurrentPos;//
int ds_Score ;//
};
#endif
ファイル4:DSObject.cpp
//-------------------------------------------------------------
//DSObject.cpp
//
// DSEnemy DSPlayer DSBullet
//---------------------------------------------------------------
#include "stdafx.h"
//---------------------------------------------------------------
// :DSObject()
// :
//----------------------------------------------------------------
DSObject::DSObject( OBJECT_TYPE ds_Type )
{
this->ds_Type = ds_Type;
ds_Active = true;
this->ds_NetType = DSNETNULL ;
this->ds_ID = -1000 ;
strcpy(ds_Username," ");
ds_Previous = NULL ;
ds_Next = NULL ;
ds_Score = 0 ;
}
//---------------------------------------------------------------
// :DSObject()
// :
//----------------------------------------------------------------
DSObject::DSObject()
{
ds_Active = true;
strcpy(ds_Username," ");
ds_Previous = NULL ;
ds_Next = NULL ;
ds_Score = 0 ;
}
//---------------------------------------------------------------
// :~DSObject()
// :
//----------------------------------------------------------------
DSObject::~DSObject()
{
DS_CleanUp();
}
//---------------------------------------------------------------
// :DS_InitObject()
// :
//----------------------------------------------------------------
HRESULT DSObject::DS_InitObject()
{
return S_OK;
}
//---------------------------------------------------------------
// :DS_FrameMove()
// :
//----------------------------------------------------------------
void DSObject::DS_FrameMove( )
{
return ;
}
//---------------------------------------------------------------
// :DS_CleanUp()
// :
//----------------------------------------------------------------
void DSObject::DS_CleanUp()
{
return ;
}
//---------------------------------------------------------------
// :DS_RenderScene()
// :
//----------------------------------------------------------------
void DSObject::DS_RenderScene( )
{
return ;
}
//---------------------------------------------------------------
// :DS_NetFrameMove()
// :
//----------------------------------------------------------------
HRESULT DSObject::DS_NetFrameMove()
{
return S_OK ;
}
//----------------------------------------------------------------
//DS_NetRenderScene()
//
//----------------------------------------------------------------
void DSObject::DS_NetRenderScene( )
{
return ;
}
ファイル5:DSGameServer.h
//-----------------------------------------------------------------------------
// DSGameServer.h
//
//
//------------------------------------------------------------------------------
#ifndef _DSGAMESERVER_
#define _DSGAMESERVER_
//
#define MAX_NUM 150
//
#define DEFAULTPORT 12345
#include "DSObject.h"
//
// ID
// IP
struct dsClientInformation
{
SOCKET ds_Sock ;//
sockaddr_in ds_Client ;//
int ds_ID ;// ID
DWORD ds_RecvThreadID ;//
// DWORD ds_SendThreadID ;//
bool ds_Active ;
};
//
//
class DSGameServer
{
public:
DSGameServer();
~DSGameServer();
int DS_ProcessGameServer( );//
int DS_SendMessage( int ID ,char *buf );//
int DS_CheckSocket();// ID
void DS_CleanSocket( int ID );// ID
void DS_SendAll( char *buf,int ID = -1 ) ;//
public:
static DWORD WINAPI DS_ListenThread(void *data);//
SOCKET ds_ListenSocket; // socket
sockaddr_in ds_Server; //
dsClientInformation ds_AcceptSocket[MAX_NUM] ;//
//
public:
//
int DS_AddObject( DSObject *ds_New );
int DS_RemoveObject( DSObject *ds_New );
void DS_Print();
// ( )
int DS_ProcessData( char *ds_NetData );
//
int DS_ParseMsgSTC( char *ds_Msg );
//
DSObject* DS_PlayerProcess( int ID,char *ds_Msg,int type = 0 );
int DS_CheckState(int ID);//
public:
DSObject *ds_Head ;//
int ds_EnemyNum ;
int ds_ObjectNum ;
int ds_RedNum ;
int ds_BlueNum ;
int ds_RedCurrentNum ;//
int ds_BlueCurrentNum ;//
bool ds_IsPassword ;//
char ds_Password[50];//
};
#endif
ファイル6:DSGameServer.cpp
#include "stdafx.h"
DSGameServer *ds_GameDebugServer ;
//-----------------------------------------------------------------------------
// :
// :
// Win32
//
//-----------------------------------------------------------------------------
DSGameServer::DSGameServer()
{
WSADATA wsaData;
//
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
printf("
");
return ;
}
//
ds_ListenSocket=socket(AF_INET,SOCK_STREAM,0);
if(ds_ListenSocket==INVALID_SOCKET)
{
printf(" ; :%d
",WSAGetLastError());
return ;
}
//
ds_Server.sin_family=AF_INET;
ds_Server.sin_port=htons( DEFAULTPORT );
ds_Server.sin_addr.s_addr=htonl(INADDR_ANY);
//
if(bind(ds_ListenSocket,(LPSOCKADDR)&ds_Server,sizeof(ds_Server))==SOCKET_ERROR)
{
printf(" :%d
",WSAGetLastError());
return ;
}
//
if(listen(ds_ListenSocket,5)==SOCKET_ERROR)
{
printf(" :%d
",WSAGetLastError());
return ;
}
//
for(int i=0;ids_NetType = DSNETHEAD ;
ds_Head->ds_Type = DSHEAD ;
ds_Head->ds_ID = -1 ;
ds_EnemyNum = 0 ;
ds_ObjectNum = 0;
ds_RedNum = 0 ;
ds_BlueNum = 0 ;
ds_IsPassword = false ;
strcpy(ds_Password,"");
}
//-----------------------------------------------------------------------------
// :
// :
//
//-----------------------------------------------------------------------------
DSGameServer::~DSGameServer( )
{
if( ds_ListenSocket != NULL )
closesocket( ds_ListenSocket );
WSACleanup();
}
//-----------------------------------------------------------------------------
// :
// : ID
//
//-----------------------------------------------------------------------------
int DSGameServer::DS_CheckSocket( )
{
for(int i=0;i>>>>>>>>
IP :[%s], :[%d]
",
inet_ntoa(ds_AcceptSocket[index].ds_Client.sin_addr),
ntohs(ds_AcceptSocket[index].ds_Client.sin_port));
//
char ds_PassBuf[100];
sprintf(ds_PassBuf,"#IP i%d*p0*",ds_AcceptSocket[index].ds_ID );
DS_SendMessage(ds_AcceptSocket[index].ds_ID,ds_PassBuf);
//
int ThreadID; // id
// createthread
ThreadID = (int)CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)(DSGameServer::DS_ListenThread), (void *)&ds_AcceptSocket[index], 0, &ds_AcceptSocket[index].ds_RecvThreadID);
ThreadID = ThreadID ? 0 : 1; // , 0
if(ThreadID) // ThreadID 0,
{
printf("
");
ExitThread(ds_AcceptSocket[index].ds_RecvThreadID );
}
printf("******** ID[%d] *********
",index );
}
else
{
SOCKET ds_Accept=accept(ds_ListenSocket,(struct sockaddr*)&ds_Client,&ds_Len);
if(ds_Accept==INVALID_SOCKET)
{
printf(" : %d
",WSAGetLastError());
break;
}
printf("
IP :[%s], :[%d]
",inet_ntoa(ds_Client.sin_addr),
ntohs(ds_Client.sin_port));
send( ds_Accept,"#FF i0*m *",strlen("#FF m "),0 );
closesocket( ds_Accept );
printf("<<<<<<<<<>>>>>>>>>>>>>..
");
}
}
return 0;
}
//-----------------------------------------------------------------------------
// :
// : select IO
//
//-----------------------------------------------------------------------------
DWORD WINAPI DSGameServer::DS_ListenThread(void *data)
{
dsClientInformation *ds_GameSocket = (dsClientInformation *)data ;
while(true)
{
if( ds_GameSocket->ds_Sock == NULL )
{
ds_GameDebugServer->DS_CleanSocket( ds_GameSocket->ds_ID );
continue ;
}
//
char buf[1024] ;
fd_set ds_Read;// select IO
FD_ZERO(&ds_Read);
FD_SET(ds_GameSocket->ds_Sock,&ds_Read);
select(0,&ds_Read,NULL,NULL,NULL) ;
if(FD_ISSET(ds_GameSocket->ds_Sock,&ds_Read) )
{
int result = recv( ds_GameSocket->ds_Sock, buf,sizeof(buf),0 ) ;
if ( result > 0 )
{
buf[result] = 0 ;
printf("
ID[%d] : %s",ds_GameSocket->ds_ID,buf) ;
ds_GameDebugServer->DS_SendAll(buf,ds_GameSocket->ds_ID) ;
ds_GameDebugServer->DS_ProcessData( buf );
fflush(0) ;
}
else
{
ds_GameDebugServer->DS_CleanSocket( ds_GameSocket->ds_ID );
}
}
}
return 1;
}
//-----------------------------------------------------------------------------
// :
// :
//
//-----------------------------------------------------------------------------
int DSGameServer::DS_SendMessage( int ID , char *buf )
{
if( ID<0 )
return 0 ;
printf(" ID%d :%s
",ID,buf);
int iSend = send(ds_AcceptSocket[ID].ds_Sock,buf,strlen(buf),0 ) ;
if( iSend == SOCKET_ERROR )//==WSAECONNABORTED || iSend == WSAECONNRESET
{
printf(" ID[%d] DS_SendMessage
",ID) ;
//DS_CleanSocket( ID );
ds_AcceptSocket[ID].ds_Sock = NULL ;
}
return 1;
}
//----------------------------------------------------
//
//
//----------------------------------------------------
void DSGameServer::DS_SendAll(char *buf,int ID )
{
printf(" :%s
",buf);
for(int i=0 ; i>>>>>>>>>>
",ID);
DSObject *p = ds_Head->ds_Next ;
while( p )
{
if( p->ds_ID == ID )
{
printf("
");
DS_RemoveObject( p );
break ;
}
p = p->ds_Next ;
}
ds_AcceptSocket[ID].ds_Active = false ;
closesocket( ds_AcceptSocket[ID].ds_Sock );
ds_AcceptSocket[ID].ds_Sock = NULL ;
printf(" %d
",ds_AcceptSocket[ID].ds_RecvThreadID );
ExitThread(ds_AcceptSocket[ID].ds_RecvThreadID );
printf("***************************************
");
printf("<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>
");
}
//---------------------------------------------------
//
// :
// : next
// previous
// :
//
//-----------------------------------------------------------------------
int DSGameServer::DS_AddObject( DSObject *ds_New )
{
if( ds_New == NULL )
return 0;
DSObject *p;
p = ds_Head;
//
while( p->ds_Next != NULL )
p = p->ds_Next ;
// next
// previous
p->ds_Next = ds_New ;
ds_New->ds_Previous = p ;
p = NULL ;
free( p );
//
//
if( ds_New->ds_Type == DSENEMY )
ds_EnemyNum++;
if(ds_New->ds_NetType == DSRED )
{
ds_RedCurrentNum ++ ;
ds_RedNum ++ ;
}
if(ds_New->ds_NetType == DSBLUE )
{
ds_BlueCurrentNum++ ;
ds_BlueNum ++ ;
}
ds_ObjectNum++;
return 1;
}
//-----------------------------------------------------------------------------
// : //*************
// :
// 1. p->ds_Previous->ds_Next = NULL
// 2.
// p->ds_Previous->ds_Next = p->ds_Next ;
// p->ds_Next->ds_Previous = p->ds_Previous ;
// :
//
//-----------------------------------------------------------------------------
int DSGameServer::DS_RemoveObject( DSObject *ds_New )
{
//
if( ds_New == NULL )
return 0;
DSObject *p;
p = ds_Head->ds_Next ;
//
while( p != NULL )
{
//
if( p == ds_New )
{
//
if(ds_New->ds_Next != NULL )
{
p->ds_Previous->ds_Next = p->ds_Next ;
p->ds_Next->ds_Previous = p->ds_Previous ;
}
//
else
p->ds_Previous->ds_Next = NULL ;
//
p->ds_Previous = NULL ;
p->ds_Next = NULL ;
ds_New = NULL ;
//
if(p->ds_Type == DSENEMY )
ds_EnemyNum-- ;
if( p->ds_NetType == DSRED )
{
ds_RedCurrentNum --;
ds_RedNum -- ;
}
if( p->ds_NetType == DSBLUE )
{
ds_BlueCurrentNum -- ;
ds_BlueNum -- ;
}
ds_ObjectNum--;
free(p);
return 1 ;
}
p = p->ds_Next ;
}
//******************************
//
free( p );
return 0;
}
//--------------------------------------------------------------------
//
//------------------------------------------------------------------
void DSGameServer::DS_Print()
{
DSObject *p;
p = ds_Head->ds_Next ;
printf("ID
");
while( p )
{
printf(" %d %d %d %d %d
",p->ds_ID,p->ds_NetType,
p->ds_Health,p->ds_Active, p->ds_Score,p->ds_CurrentPos.x,p->ds_CurrentPos.y,p->ds_CurrentPos.z );
p = p->ds_Next ;
}
p = NULL ;
free(p);
printf(" :%d : %d : %d
",ds_RedNum,ds_BlueNum,ds_ObjectNum);
}
//---------------------------------------------------------------------------------
//
//
//
//
// # ,
//
//
//--------------------------------------------------------------------------------
int DSGameServer::DS_ProcessData( char *ds_NetData )
{
char ds_Dest[1024] ;
strcpy( ds_Dest,ds_NetData );
printf("%s
",ds_Dest);
while(true)
{
int ds_Count = DS_ReturnCharPosition(ds_Dest,'#');
//
if( ds_Count == -1 )
return 0 ;
char ds_Msg[100] ;
ds_Msg[0] = '#' ;
int count = ds_Count +1 ;
int temp = 1 ;
while( ds_Dest[count] != '#' && ds_Dest[count] != '\0' )
{
ds_Msg[temp++] = ds_Dest[count++] ;
}
ds_Msg[temp] ='\0';
//
printf(" :%s
",ds_Msg);
if( ds_Msg[temp-1] == '*' )
DS_ParseMsgSTC(ds_Msg);
ds_Dest[ds_Count] = '@';
}
return 1 ;
}
//------------------------------------------------------------------------------------
//
//
//ID
//------------------------------------------------------------------------------------
int DSGameServer::DS_CheckState(int ID)
{
if(ds_RedNum >0 && ds_BlueNum >0 )
return 0 ;
char send[20];
//
if(ds_RedNum <= 0)
sprintf(send,"#ST i0*mBlue Success!*");
//
else if(ds_BlueNum <= 0)
sprintf(send,"#ST i1*mBlue Success!*");
DS_SendAll(send);
//
float x = 12.0f ;
float z = 12.0f ;
DSObject *p ;
p = ds_Head->ds_Next ;
while( p )
{
p->ds_Active = true ;
p->ds_Health = 100 ;
srand( timeGetTime() );
p->ds_CurrentPos.y =rand()%3+4.0f ;
if( p->ds_NetType == DSRED )
{
p->ds_CurrentPos.x = x+rand()%20 ;
p->ds_CurrentPos.z = z+rand()%20 ;
}
else
{
p->ds_CurrentPos.x = -x-rand()%20 ;
p->ds_CurrentPos.z = -z-rand()%20 ;
}
p = p->ds_Next ;
}
p = NULL ;
free( p ) ;
printf("
");
DS_Print();
return 1 ;
}
//----------------------------------------------------------------------------------------
//
//
//
//
//---------------------------------------------------------------------------------------
int DSGameServer::DS_ParseMsgSTC( char *ds_Msg )
{
//
//
// 7
if( strlen(ds_Msg) < 7 )
{
printf("
");
return -2 ;
}
long time = timeGetTime() ;
// ds_Temp
char ds_Temp[3];
for( int i = 0 ; i<3 ; i++ )
ds_Temp[i] = ds_Msg[i] ;
ds_Temp[3] = '\0' ;
// ID
int ID ;
if( (ID = DS_ReturnInt(ds_Msg,'i') ) == -10000 )
{
printf(" ID -10000
");
return -2 ;
}
//
// :
//
//
//
if( strcmp(ds_Temp ,"#OP" ) == 0 )
{
printf("<<<<<<<<<<>>>>>>>>>>>>>>>>
");
//
if( ds_GameDebugServer->ds_IsPassword )
{
char password[50];
//
if( DS_ReturnString(ds_Msg,'?',password ) == 0 )
strcpy(password,"");
if( strcmp(ds_GameDebugServer->ds_Password,password ) != 0 )
{
ds_GameDebugServer->DS_SendMessage( ID,"#FP i0*m *");
ds_GameDebugServer->DS_CleanSocket( ID );
return -2 ;
}
}
//
// 1
//2
//3
DSObject *p ;
if ( ( p = ds_GameDebugServer->DS_PlayerProcess(ID,ds_Msg,1 ) ) == NULL )
{
printf(" ID[%d]
",ID );
return -2 ;
}
DS_PrintCurrentTime(" ");
ds_GameDebugServer->DS_Print();
printf("***************************************************
");
//
char ds_NewInfor[300];
sprintf(ds_NewInfor,"#NC i%d*t%d*x%f*y%f*z%f*u%s*",
p->ds_ID,p->ds_NetType,p->ds_CurrentPos.x,
p->ds_CurrentPos.y,p->ds_CurrentPos.z,
p->ds_Username );
ds_GameDebugServer->DS_SendAll( ds_NewInfor,ID );
DS_PrintCurrentTime(" ");
ds_GameDebugServer->DS_Print();
printf("***************************************************
");
//
p = ds_Head->ds_Next ;
while( p )
{
if( p->ds_ID != ID )
{
char ds_NewInfor[300];
sprintf(ds_NewInfor,"#NC i%d*t%d*x%f*y%f*z%f*u%s*h%d*s%d*",
p->ds_ID,p->ds_NetType,p->ds_CurrentPos.x,
p->ds_CurrentPos.y,p->ds_CurrentPos.z,
p->ds_Username,p->ds_Health,p->ds_Score );
ds_GameDebugServer->DS_SendMessage( ID,ds_NewInfor );
ZeroMemory(ds_NewInfor,300);
}
p = p->ds_Next ;
}
p = NULL ;
free( p );
//
ds_AcceptSocket[ID].ds_Active = true ;
DS_PrintCurrentTime(" ");
ds_GameDebugServer->DS_Print();
printf("***************************************************
");
return 1 ;
}
//
// :
//ID
//
//
else if( strcmp(ds_Temp ,"#CU" ) == 0 )
{
printf("<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>
") ;
DSObject *p ;
if( ( p = ds_GameDebugServer->DS_PlayerProcess(ID,ds_Msg,0) ) == NULL )
{
printf("
");
return -2 ;
}
ds_GameDebugServer->DS_SendAll( ds_Msg,ID );
return 1 ;
}
//
// :
//ID
// Active = false ;
else if( strcmp(ds_Temp ,"#CD" ) == 0 )
{
printf("<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>
");
DS_Print();
printf("*****************************************************
");
DSObject *p ;
if( ( p = ds_GameDebugServer->DS_PlayerProcess(ID,ds_Msg,0) ) == NULL )
{
printf("
");
return -2 ;
}
p->ds_Health = 0 ;
p->ds_Active = false ;
//
//
ds_GameDebugServer->DS_SendAll( ds_Msg,ID );
//
if (p->ds_NetType == DSRED )
ds_RedNum -- ;
else if(p->ds_NetType == DSBLUE )
ds_BlueNum-- ;
if(ds_RedNum >0 && ds_BlueNum >0 )
return 0 ;
char send[60];
//
if(ds_RedNum <= 0)
sprintf(send,"#ST i0*mBlue Success!*");
//
else if(ds_BlueNum <= 0)
sprintf(send,"#ST i1*mRed Success!*");
printf(" :%s
",send);
for(int i=0 ; ids_Next ;
while( p )
{
p->ds_Active = true ;
p->ds_Health = 100 ;
srand( timeGetTime() );
p->ds_CurrentPos.y =rand()%3+4.0f ;
if( p->ds_NetType == DSRED )
{
p->ds_CurrentPos.x = x+rand()%20 ;
p->ds_CurrentPos.z = z+rand()%20 ;
}
else
{
p->ds_CurrentPos.x = -x-rand()%20 ;
p->ds_CurrentPos.z = -z-rand()%20 ;
}
p = p->ds_Next ;
}
p = NULL ;
free( p ) ;
printf("
");
DS_Print();
return 1 ;
}
//
//
else if( strcmp(ds_Temp,"#HC") == 0)
{
printf("<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>
");
ds_GameDebugServer->DS_PlayerProcess(ID,ds_Msg,3);
return 1 ;
}
//
// :
//
else if( strcmp(ds_Temp ,"#DD" ) == 0 )
{
printf("<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>
",ID);
//
//
ds_GameDebugServer->DS_CleanSocket( ID ) ;
ds_GameDebugServer->DS_SendAll( ds_Msg,ID ) ;
return 1 ;
}
else if( strcmp(ds_Temp,"#BN") == 0 )
{
//
// #NS
DSObject *p ;
p = ds_Head->ds_Next ;
while( p )
{
char ds_NewInfor[300];
sprintf(ds_NewInfor,"#NS i%d*t%d*x%f*y%f*z%f*h%d*s%d*u%s*",
p->ds_ID,p->ds_NetType,p->ds_CurrentPos.x,
p->ds_CurrentPos.y,p->ds_CurrentPos.z,
p->ds_Health,p->ds_Score,p->ds_Username );
ds_GameDebugServer->DS_SendMessage( ID,ds_NewInfor );
ZeroMemory(ds_NewInfor,300);
p = p->ds_Next ;
}
p = NULL ;
free( p );
//
ds_GameDebugServer->DS_SendMessage( ID,"#OK i0*" );
return 1 ;
}
//
// :<2>*I2*m* >
//0 1 2 i
else if( strcmp(ds_Temp,"#CH" ) == 0 )
{
printf("<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
");
return 1 ;
}
return 0 ;
}
//-----------------------------------------------------------------------------------
//
//type = 0 ;
//type = 1 ;
//type = 2 ;
//------------------------------------------------------------------------------------
DSObject* DSGameServer::DS_PlayerProcess( int ID,char *ds_Msg,int type )
{
// 1
int count = 0 ;
//
DSObject *p = ds_Head ;
while( p )
{
if( p->ds_ID == ID )
{
count = 1 ;
break ;
}
p = p->ds_Next ;
}
//
if( count == 1 && ( type == 1 || type == 0 ) ) //type !=2 && type!=3
{
int ds_tempInt ;//
float ds_tempFloat ;//
//
if( ( ds_tempInt = DS_ReturnInt(ds_Msg,'s') ) != -10000 )
p->ds_Score = ds_tempInt ;
//
if( ( ds_tempInt = DS_ReturnInt(ds_Msg,'h') ) != -10000 )
p->ds_Health = ds_tempInt ;
// X
if( ( ds_tempFloat = DS_ReturnFloat(ds_Msg,'x') ) != -10000.0f )
p->ds_CurrentPos.x = ds_tempFloat ;
// y
if( ( ds_tempFloat = DS_ReturnFloat(ds_Msg,'y') ) != -10000.0f )
p->ds_CurrentPos.y = ds_tempFloat ;
// z
if( ( ds_tempFloat = DS_ReturnFloat(ds_Msg,'z') ) != -10000.0f )
p->ds_CurrentPos.z = ds_tempFloat ;
return p ;
}
else if( count == 0 && type == 1 )
{
int ds_tempInt ;//
float ds_tempFloat ;//
DSObject *ds_New = new DSObject( );
ds_New->ds_ID = ID ;
//
if( ( ds_tempInt = DS_ReturnInt(ds_Msg,'s') ) != -10000 )
ds_New->ds_Score = ds_tempInt ;
//
if( ( ds_tempInt = DS_ReturnInt(ds_Msg,'h') ) != -10000 )
ds_New->ds_Health = ds_tempInt ;
//
if( ( ds_tempInt = DS_ReturnInt(ds_Msg,'t') ) != -10000 )
ds_New->ds_NetType =(NETOBJECT_TYPE) ds_tempInt ;
// X
if( ( ds_tempFloat = DS_ReturnFloat(ds_Msg,'x') ) != -10000.0f )
ds_New->ds_CurrentPos.x = ds_tempFloat ;
// y
if( ( ds_tempFloat = DS_ReturnFloat(ds_Msg,'y') ) != -10000.0f )
ds_New->ds_CurrentPos.y = ds_tempFloat ;
// z
if( ( ds_tempFloat = DS_ReturnFloat(ds_Msg,'z') ) != -10000.0f )
ds_New->ds_CurrentPos.z = ds_tempFloat ;
if( DS_ReturnString(ds_Msg,'u',ds_New->ds_Username ) == 0 )
strcpy(ds_New->ds_Username," ");
DS_AddObject( ds_New );
// ds_New = NULL ;
//free( ds_New );
return ds_New ;
}
//
else if( count == 0 && type ==2 )
{
DSObject *ds_Player = new DSObject();
ds_Player->ds_ID = ID ;
ds_Player->ds_NetType = DSRED ;
ds_Player->ds_Type = DSPLAYER ;
DS_AddObject(ds_Player);
return ds_Player ;
}
//
else if( count == 1 && type == 3 )
{
int ds_tempInt ;
if( ( ds_tempInt = DS_ReturnInt(ds_Msg,'c') ) != -10000 )
p->ds_Health -= ds_tempInt*25 ;
p = NULL ;
free( p );
return NULL ;
}
return NULL ;
}
ファイル7:DSMain.cpp
#include "stdafx.h"
void main()
{
DSGameServer *ds_GameServer = new DSGameServer();
DS_PrintCurrentTime(" ");
printf("*******************DreamShip *****************************************
");
printf("******************* **********************************************
");
ds_GameServer->DS_ProcessGameServer();
}
以上のファイルをソースコードに保存し、VC++6.0でコンパイルします.コンパイル前にLinkにws 2_を追加することに注意してください.32.libライブラリ.