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

📄 mirmsgqueue.cpp

📁 这是传奇代码私服之二
💻 CPP
字号:
// MirMsgQueue.cpp: implementation of the CMirMsgQueue class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MirX.h"
#include "MirMsgQueue.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CMirMsgQueue::CMirMsgQueue()
{
	InitializeCriticalSection(&m_CS);
	m_MsgList.RemoveAll();
}

CMirMsgQueue::~CMirMsgQueue()
{
	while(!m_MsgList.IsEmpty())
	{
		char *tmpBuf=(char*)m_MsgList.GetHead();
		delete []tmpBuf;
		m_MsgList.RemoveHead();
	}
	DeleteCriticalSection(&m_CS);
}


///////////////////////////////////////////////////////////////////////
//
// 函数名       : CMirMsgQueue::PutMsg
// 功能描述     : 向消息列队中添加消息
// 参数         : char *pMsg
// 返回值       : bool (返回true表示成功;否则失败)
//
///////////////////////////////////////////////////////////////////////
bool CMirMsgQueue::PutMsg(char *pMsg)
{
	EnterCriticalSection(&m_CS);
	bool retval=false;
	int ilen=strlen(pMsg);
	char *tmpBuf=new char[ilen];
	if(tmpBuf!=NULL)
	{
		strcpy(tmpBuf,pMsg);
		m_MsgList.AddTail(tmpBuf);
		retval=true;
	}
	LeaveCriticalSection(&m_CS);
	return retval;
}


///////////////////////////////////////////////////////////////////////
//
// 函数名       : CMirMsgQueue::GetMsg
// 功能描述     : 从消息列队中取出一个消息
// 参数         : char *pMsg
// 返回值       : bool (返回true表示成功;否则失败)
//
///////////////////////////////////////////////////////////////////////
bool CMirMsgQueue::GetMsg(char *pMsg)
{
	EnterCriticalSection(&m_CS);
	char *tmpBuf;
	bool retval=false;
	if(pMsg!=NULL)
	{
		if(!m_MsgList.IsEmpty())
		{
			tmpBuf=(char*)m_MsgList.GetHead();
			strcpy(pMsg,tmpBuf);
			FILE*fp=fopen("d:\\mirx-recv.txt","a");
			fprintf(fp,"%s\n",tmpBuf);
			fclose(fp);
			m_MsgList.RemoveHead();
			//delete []tmpBuf;
			retval=true;
		}
	}
	LeaveCriticalSection(&m_CS);
	return retval;
}


///////////////////////////////////////////////////////////////////////
//
// 函数名       : CMirMsgQueue::PutMsg
// 功能描述     : 向消息列队中添加消息
// 参数         : MMSG *pMsg
// 返回值       : bool (返回true表示成功;否则失败)
//
///////////////////////////////////////////////////////////////////////
bool CMirMsgQueue::PutMsg(MMSG *pMsg)
{
	EnterCriticalSection(&m_CS);	// 进入临界段
	bool retval=false;		// 默认返回false
	int ilen=sizeof(MMSG);	
	char *tmpBuf=new char[ilen+1];
	if(tmpBuf!=NULL)
	{
		strncpy(&tmpBuf[1],(char*)pMsg,ilen);
		m_MsgList.AddTail(tmpBuf);

		//做一个标志,防止被GetMsg(char *pMsg)错误取出
		tmpBuf[0]=0;

		retval=true;
	}
	LeaveCriticalSection(&m_CS);	//离开临界段
	return retval;
}


///////////////////////////////////////////////////////////////////////
//
// 函数名       : CMirMsgQueue::GetMsg
// 功能描述     : 从消息列队中取出一个消息
// 参数         : MMSG *pMsg
// 返回值       : bool (返回true表示成功;否则失败)
//
///////////////////////////////////////////////////////////////////////
bool CMirMsgQueue::GetMsg(MMSG *pMsg)
{
	EnterCriticalSection(&m_CS);
	char *tmpBuf;
	bool retval=false;
	if(pMsg!=NULL)
	{
		if(!m_MsgList.IsEmpty())
		{
			tmpBuf=(char*)&m_MsgList.GetHead();
			//if(tmpBuf[0]==0)
			{
				strncpy((char*)pMsg,&tmpBuf[1],sizeof(MMSG));
				m_MsgList.RemoveHead();
				//delete []tmpBuf;
				retval=true;
			}
		}
	}
	LeaveCriticalSection(&m_CS);
	return retval;
}

⌨️ 快捷键说明

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