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

📄 msgbuff.cpp

📁 实现了无线传感器网络中的一个路由协议算法。
💻 CPP
字号:
// MsgBuff.cpp: implementation of the CMsgBuff class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SimSensor.h"
#include "MsgBuff.h"

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

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

CMsgBuff::CMsgBuff()
{	
   pHead = NULL;
   pTail = NULL;
   InitializeCriticalSection(&Locked);
}

CMsgBuff::~CMsgBuff()
{
	EnterCriticalSection(&Locked);
   
	while ( !IsEmpty() ) {
		CMsg *pMsg = GetMsg();
		DELETE_OBJECT(pMsg);
	}

	LeaveCriticalSection(&Locked);
}

void CMsgBuff::AddMsg(CMsg *pMsg)
{
 	CMsgStrain *pMsgStrain = new CMsgStrain;
	pMsgStrain->Message = pMsg;

    EnterCriticalSection(&Locked);

	if ( pTail!=NULL ) {
		pTail->pNext = pMsgStrain;
		pTail = pTail->pNext;
	}else {
		pHead = pMsgStrain;
		pTail = pMsgStrain;
	}

	LeaveCriticalSection(&Locked);
}

CMsg * CMsgBuff::GetMsg()
{
    EnterCriticalSection(&Locked);

	if ( pHead == NULL) {
	    LeaveCriticalSection(&Locked);		
		return NULL;
	}

	CMsgStrain *pMsgStrain = pHead;
	pHead = pHead->pNext; 
	if ( pHead==NULL ) pTail=NULL;
    CMsg * pMsg = pMsgStrain->Message;

    LeaveCriticalSection(&Locked);

	DELETE_OBJECT(pMsgStrain);
   
	return pMsg;
}

void CMsgBuff::InsertMsg(CMsg *pMsg)
{
 	CMsgStrain *pMsgStrain = new CMsgStrain;
	pMsgStrain->Message = pMsg;

	EnterCriticalSection(&Locked);
    
	if (pHead!=NULL) {
		pMsgStrain->pNext = pHead;
		pHead = pMsgStrain;
	}else {
        pHead = pMsgStrain;
     	pTail = pMsgStrain;	
	}

	LeaveCriticalSection(&Locked);
}

BOOL CMsgBuff::IsEmpty()
{
	BOOL Rtn;
   	EnterCriticalSection(&Locked);
	if ( pHead == NULL) Rtn = TRUE;
	else   Rtn = FALSE;
	LeaveCriticalSection(&Locked);
	return Rtn;
}

⌨️ 快捷键说明

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