📄 rainsock.cpp
字号:
// RainSock.cpp : implementation file
//
#include "stdafx.h"
#include "rain.h"
#include "rainsock.h"
#include "raindoc.h"
#include "rainview.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CRainDoc *PDocument;
/////////////////////////////////////////////////////////////////////////////
// CRainSock
CRainSock::CRainSock()
{
}
CRainSock::~CRainSock()
{
}
void CRainSock::setConnectState(int state)
{
connectState=state;
}
int CRainSock::getConnectState()
{
return connectState;
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CRainSock, CAsyncSocket)
//{{AFX_MSG_MAP(CRainSock)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
/////////////////////////////////////////////////////////////////////////////
// CRainSSSock
CRainSSSock::CRainSSSock()
{
isSockCreated=0;
init();
}
CRainSSSock::~CRainSSSock()
{
}
void CRainSSSock::init()
{
usrIP="";
usrName="";
rightChar=0;
rightStr=0;
if(isSockCreated)
{
Close();
isSockCreated=0;
}
setConnectState(CONNECTION_LOST);
}
void CRainSSSock::OnReceive(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
char buffer[1024];
int ret;
struct command *pComm;
struct in_addr addr;
if(nErrorCode)
{
#if DEBUG_RAIN
TRACE("OnReceive error code:%x\n",nErrorCode);
#endif
}
#if DEBUG_RAIN
TRACE("OnReceive\n");
#endif
setConnectState(CONNECTION_SAFE);
ret=sizeof(struct command);//
ret=Receive(buffer,sizeof(struct command),0);//
pComm=(struct command *)buffer;
switch(pComm->type)
{
case COM_C_ACK:
addr.S_un.S_addr=pComm->content.c_ack.usrIP;
usrIP=inet_ntoa(addr);
usrName=pComm->content.c_ack.usrName;
break;
case COM_C_HIT:
if(PDocument->server.checkUsrHit(pComm->content.c_hit.hitStrSeq,
pComm->content.c_hit.hitTickTimer))
{
//insert this string into hit string list
insertUsrHit(pComm->content.c_hit.hitStrSeq,
pComm->content.c_hit.hitTickTimer,
pComm->content.c_hit.hitStrLen);
}
break;
default:
#if DEBUG_RAIN
TRACE("unknown command type:0x%x",pComm->type);
#endif
}
}
void CRainSSSock::insertUsrHit(ULONG hitStrSeq,ULONG hitTickTimer,int hitStrLen)
{
int i;
for(i=0;i<RAIN_Y;i++)
{
if(hitStrList[i].state==0)
{
hitStrList[i].state=1;
hitStrList[i].hitStrSeq=hitStrSeq;
hitStrList[i].hitTickTimer=hitTickTimer;
hitStrList[i].hitStrLen=hitStrLen;
break;
}
}
}
/////////////////////////////////////////////////////////////////////////////
// CRainMSSock
CRainMSSock::CRainMSSock()
{
isSockCreated=0;
init();
}
CRainMSSock::~CRainMSSock()
{
}
void CRainMSSock::init()
{
int i;
rightChar=0;
rightStr=0;
initUsrHit();
usrID=0;
for(i=0;i<DFT_CLIENT_NUM;i++)
{
slaveSocket[i].init();
slaveSocket[i].usrID=i+1;
}
if(isSockCreated)
{
Close();
}
setConnectState(CONNECTION_LOST);
}
void CRainMSSock::OnAccept(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
int i;
CAsyncSocket tmpSock;
if(nErrorCode)
{
#if DEBUG_RAIN
TRACE("OnAccept error code:%d\n",nErrorCode);
#endif
return;
}
if(PDocument->getRainState()!=RS_ACCEPTING)
{
#if DEBUG_RAIN
TRACE("refuse to accept now\n");
#endif
// refuse this connection
Accept(tmpSock,NULL,NULL);
tmpSock.Close();
return;
}
i=findFreeSlave();
if(i<0)
{
#if DEBUG_RAIN
TRACE("no socket to accept a connection\n");
#endif
// refuse this connection
Accept(tmpSock,NULL,NULL);
tmpSock.Close();
return;
}
Accept(slaveSocket[i],NULL,NULL);
slaveSocket[i].init();
slaveSocket[i].setConnectState(CONNECTION_SAFE);
slaveSocket[i].isSockCreated=1;
#if DEBUG_RAIN
TRACE("accept a connection\n");
#endif
}
int CRainMSSock::findFreeSlave()
{
int i,find;
find=-1;
for(i=0;i<DFT_CLIENT_NUM;i++)
{
if(slaveSocket[i].getConnectState()==CONNECTION_LOST)
{
find=i;
break;
}
}
return find;
}
void CRainMSSock::checkSlave()
{
int i;
for(i=0;i<DFT_CLIENT_NUM;i++)
{
if(slaveSocket[i].getConnectState()!=CONNECTION_LOST)
{
if(slaveSocket[i].getConnectState()==CONNECTION_DANGER)
{
slaveSocket[i].init();
#if DEBUG_RAIN
TRACE("slave connection timeout\n");
#endif
}
else
{
slaveSocket[i].setConnectState(CONNECTION_DANGER);
}
}
}
}
void CRainMSSock::req()
{
int i,j,ret;
struct command server_req;
int usrNumber,hitNumber;
// fill server info
server_req.type=COM_S_REQ;
server_req.content.s_req.usrInfo[0].usrIP=inet_addr(myIP);
server_req.content.s_req.usrInfo[0].usrID=usrID;
strcpy(server_req.content.s_req.usrInfo[0].usrName,PDocument->localUsrName);
// fill server hit here
hitNumber=0;
for(j=0;j<RAIN_Y;j++)
{
if(hitStrList[j].state)
{
server_req.content.s_req.usrInfo[0].hitStrSeq[hitNumber]=
hitStrList[j].hitStrSeq;
hitNumber++;
//server really hit
rightStr++;
rightChar+=hitStrList[j].hitStrLen;
PDocument->strHitAck(hitStrList[j].hitStrSeq,PDocument->localUsrName,0);
}
}
server_req.content.s_req.usrInfo[0].hitNumber=hitNumber;
// fill client info
usrNumber=1;
for(i=0;i<DFT_CLIENT_NUM;i++)
{
if(slaveSocket[i].getConnectState()!=CONNECTION_LOST)
{
server_req.content.s_req.usrInfo[usrNumber].usrIP=
inet_addr(slaveSocket[i].usrIP);
strcpy(server_req.content.s_req.usrInfo[usrNumber].usrName,
slaveSocket[i].usrName);
server_req.content.s_req.usrInfo[usrNumber].usrID=
slaveSocket[i].usrID;
//fill client hit here
hitNumber=0;
for(j=0;j<RAIN_Y;j++)
{
if(slaveSocket[i].hitStrList[j].state)
{
server_req.content.s_req.usrInfo[usrNumber].hitStrSeq[hitNumber]=
slaveSocket[i].hitStrList[j].hitStrSeq;
hitNumber++;
//client really hit
slaveSocket[i].rightStr++;
slaveSocket[i].rightChar+=slaveSocket[i].hitStrList[j].hitStrLen;
PDocument->strHitAck(slaveSocket[i].hitStrList[j].hitStrSeq,
slaveSocket[i].usrName,1);
}
}
server_req.content.s_req.usrInfo[usrNumber].hitNumber=hitNumber;
usrNumber++;
}
}
server_req.content.s_req.usrNumber=usrNumber;
for(i=0;i<DFT_CLIENT_NUM;i++)
{
if(slaveSocket[i].getConnectState()!=CONNECTION_LOST)
{
ret=slaveSocket[i].Send((UCHAR*)&server_req,sizeof(server_req),0);
}
}
initUsrHit();
PDocument->tick_timer=0;//think:put here right ?
#if DEBUG_RAIN
TRACE("server req\n");
#endif
}
void CRainMSSock::dispatchStr(char *str ,int len,int whereX,ULONG seq)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -