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

📄 servicesocket.cpp

📁 此源码使用VC编写的一个聊天程序
💻 CPP
字号:
// ChatSocket.cpp : implementation file
//

#include "stdafx.h"
#include "Client.h"
#include "ServiceSocket.h"

#include "Message.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#include "UserDlg.h"

#define SAFEDELETE(x) if(x!=NULL){delete x;x=NULL;}


/////////////////////////////////////////////////////////////////////////////
// CServiceSocket
class CUserDlg;

CServiceSocket::CServiceSocket(CUserDlg* pDlg)
{
	m_pDlg = pDlg;
}

CServiceSocket::~CServiceSocket()
{
	SAFEDELETE(m_pArchiveIn);
	SAFEDELETE(m_pArchiveOut);
	SAFEDELETE(m_pFile);

}


// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CServiceSocket, CSocket)
	//{{AFX_MSG_MAP(CServiceSocket)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif	// 0

/////////////////////////////////////////////////////////////////////////////
// CServiceSocket member functions

void CServiceSocket::OnReceive(int nErrorCode) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	CSocket::OnReceive(nErrorCode);
	m_pDlg->ProcessReadMessage();
}

void CServiceSocket::Init()
{
	m_pFile = new CSocketFile(this);
	m_pArchiveIn = new CArchive(m_pFile,CArchive::load);
	m_pArchiveOut = new CArchive(m_pFile,CArchive::store);
}

void CServiceSocket::ReceiveMsg(CMessage *pMsg)
{
	pMsg->Serialize(*m_pArchiveIn);
}

void CServiceSocket::SendMsg(CMessage *pMsg)
{
	if (m_pArchiveOut != NULL){
		pMsg->Serialize(*m_pArchiveOut);
		m_pArchiveOut->Flush();
	}
}

BOOL CServiceSocket::OnMessagePending() 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CSocket::OnMessagePending();
}

void CServiceSocket::OnClose(int nErrorCode) 
{
	// TODO: Add your specialized code here and/or call the base class
	CMessage *m_pCloseMsg;
	m_pCloseMsg = new CMessage();
	m_pCloseMsg->From = Name;
	m_pCloseMsg->To = "All";
	m_pCloseMsg->Type = 2;
	m_pCloseMsg->ShortMessage = "Logout!";
	
	SendMsg(m_pCloseMsg);

	delete m_pCloseMsg;

	CSocket::OnClose(nErrorCode);
}

⌨️ 快捷键说明

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