📄 clientsocket.cpp
字号:
// ClientSocket.cpp : implementation file
//
#include "stdafx.h"
#include "chess.h"
#include "ClientSocket.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "linebuffer.h"
#include "chessdlg.h"
//#include "ringbuffer.h"
/////////////////////////////////////////////////////////////////////////////
// CClientSocket
CClientSocket::CClientSocket(CChessDlg* chessDlg)
{
m_pChessDlg=chessDlg;
m_Buffer.Init(2);
}
CClientSocket::~CClientSocket()
{
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CClientSocket, CSocket)
//{{AFX_MSG_MAP(CClientSocket)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
/////////////////////////////////////////////////////////////////////////////
// CClientSocket member functions
BOOL CClientSocket::Send(DWORD type, void *pBuf, int nLen)
{
if(type==CHESS)
{
CLineBuffer temp;
temp+=type;
temp+=nLen;
temp.AddData(pBuf,nLen);
int i=TRY_TIMES;
while(Send(temp,temp.GetCount())==SOCKET_ERROR&&i) i--;
if(i==0)
AfxMessageBox(IDS_SOCKETBLOCK);
return i;
}
if(type==MESSAGE)
{
CLineBuffer temp;
temp+=type;
temp+=nLen;
temp.AddData(pBuf,nLen);
int i=TRY_TIMES;
while(Send(temp,temp.GetCount())==SOCKET_ERROR&&i) i--;
if(i==0)
AfxMessageBox(IDS_SOCKETBLOCK);
return i;
}
return FALSE;
}
int CClientSocket::Send(const void* lpBuf, int nBufLen, int nFlags)
{
// TODO: Add your specialized code here and/or call the base class
return CSocket::Send(lpBuf, nBufLen, nFlags);
}
int CClientSocket::Receive(void* lpBuf, int nBufLen, int nFlags)
{
// TODO: Add your specialized code here and/or call the base class
return CSocket::Receive(lpBuf, nBufLen, nFlags);
}
void CClientSocket::OnAccept(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
CSocket::OnAccept(nErrorCode);
}
void CClientSocket::OnReceive(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
BYTE tempbuf[1024];
while(1)
{
int length=Receive(tempbuf,1024);
if(m_Buffer.CanWrite(length))
{
m_Buffer.Write(tempbuf,length);
}
else
{
ProcessBuf();
m_Buffer.Write(tempbuf,length);
}
if(length<1024)
break;
}
ProcessBuf();
CSocket::OnReceive(nErrorCode);
}
void CClientSocket::ProcessBuf()
{
int type;
int length;
while(1)
{
if(m_Buffer.IsEmpty())
return;
if(!m_Buffer.CanRead(8))
return;
BYTE* lengthtemp=new BYTE[8];
m_Buffer.GetData((BYTE*)&type,4);//yizhoucheng change read() to getdata()
m_Buffer.GetData((BYTE*)lengthtemp,8);
memcpy(&length,lengthtemp+4,4);
delete[] lengthtemp;
if(length>MAX_MAYBE_LENGTH||length<=0)
{
OnError();
return;
}
if(!m_Buffer.CanRead(length+8))
{
// m_Buffer.Skip(-8);
return;
}
m_Buffer.Skip(8);
switch(type)
{
case MESSAGE:
{
if(length<1)
OnError();
else
{
BYTE* temp=new BYTE[length+1];
m_Buffer.Read(temp,length);
*(temp+length) = 0;
m_pChessDlg->m_recMessage.SetWindowText(LPCTSTR(temp));
::MessageBeep(MB_ICONQUESTION);
delete[] temp;
}
}
// CaseST_TEST(length);
break;
case CHESS:
{
if(length!=4)
OnError();
else
{
int temp;
m_Buffer.Read((unsigned char*)&temp,length);
if(m_pChessDlg->m_bServer)
{
m_pChessDlg->m_blackPosList.AddTail((void*)temp);
}
else
{
m_pChessDlg->m_whitePosList.AddTail((void*)temp);
}
m_pChessDlg->m_bReady=TRUE;
MessageBeep(MB_ICONQUESTION);
}
}
break;
/* case ST_DOWN:
// CaseST_DOWN(length);
break;
case ST_RESERVE:
// CaseST_RESERVE(length);
break;
case ST_ASYNC:
// CaseST_ASYNC(length);
break;
case ST_ASYNC_REPLY:
// CaseST_ASYNC_REPLY(length);
break;
*/
default:
OnError();
}
}
}
void CClientSocket::OnError()
{
}
void CClientSocket::OnClose(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
if(m_pChessDlg->m_bServer)
m_pChessDlg->m_recMessage.SetWindowText("对方通信中断!请等待或者重新开始.");
else
m_pChessDlg->m_recMessage.SetWindowText("服务器已经关闭,请重新开始.");
Close();
CSocket::OnClose(nErrorCode);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -