📄 lsnsock.cpp
字号:
/*
Copyright (c) 2005 william.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2
or any later version published by the Free Software Foundation;
with no Invariant Sections, no Front-Cover Texts, and no Back-Cover
Texts. A copy of the license is included in the section entitled "GNU
Free Documentation License".
*/
#include "stdafx.h"
void CMsg::SerializeSnd(CArchive& ar) const
{
ASSERT(ar.IsStoring());
int t = state;
ar << t;
ar << PosX;
ar << PosY;
ar << str;
}
void CMsg::SerializeRev(CArchive& ar)
{
ASSERT(ar.IsLoading());
int t;
ar >> t;
state = (_state)t;
ar >> PosX;
ar >> PosY;
ar >> str;
}
///////////////////////////////////////////////
// CCntSock
/////////////////////////////////////////////////
void CCntSock::Init()
{
File = new CSocketFile(this);
ArchiveIn = new CArchive(File,CArchive::load);
ArchiveOut = new CArchive(File,CArchive::store);
}
bool CCntSock::SendMsg(const CMsg& msg) const
{
if(ArchiveOut == 0) return false;
try{
msg.SerializeSnd(*ArchiveOut);
ArchiveOut->Flush();
}catch(CException* e){
e->Delete();
//it's fail,all's fail.
DisConnectFnx(IDS_EX_SND);
return false;
}
return true;
}
void CCntSock::Over()
{
if(ArchiveOut)delete ArchiveOut,ArchiveOut=0;
if(ArchiveIn)delete ArchiveIn,ArchiveIn=0;
if(File)delete File,File=0;
IsOver = true;
}
void CCntSock::OnReceive(int nErrorCode)
{
CSocket::OnReceive(nErrorCode);
//if(ArchiveIn->IsBufferEmpty())
do
{
TRY
{
CMsg msg;
if(ArchiveIn == 0) break;
msg.SerializeRev(*ArchiveIn);
if(!ReceiveMsgFnx(&msg)) break;
}
CATCH_ALL(e)
{
e->Delete();
//it's fail,all's fail.
DisConnectFnx(IDS_EX_RECV);
break;
}
END_CATCH_ALL;
}
while (!ArchiveIn->IsBufferEmpty());
}
SetDiscSign CCntSock::BeginConnect(LPCTSTR lpszAddress, UINT port)
{
if (!Create()){
DisConnectFnx(IDS_EX_CRT);
return;
}
if(Connect(lpszAddress,5520+port)){
Init();
}else{
DisConnectFnx(IDS_NOSERV);
}
}
///////////////////////////////////////////////
// CSvrSock 成员函数
/////////////////////////////////////////////////
SetDiscSign CSvrSock::BeginListen(UINT port)
{
if (!Create(5520+port))
DisConnectFnx(IDS_EX_CRT);
else if(!Listen())
DisConnectFnx(IDS_EX_LSN);
}
void CSvrSock::OnAccept(int nErrorCode)
{
CCntSock* sock_s = 0;
acptFnx(&sock_s);
ASSERT(sock_s!=0);
if (Accept(*sock_s)){
sock_s->Init();
}else{
DisConnectFnx(IDS_EX_ACC);
}
CSocket::OnAccept(nErrorCode);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -