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

📄 cmcqueue.cpp

📁 Soul的源代码,类似于劲舞团之类的游戏
💻 CPP
字号:
#include <winsock2.h>
#include <windows.h>
#include "CMCQueue.h"

using namespace MatrixCore::Network;

/*-------------------------------------------------------------------------------------------------------
//	Name		 :: CMCQueue(), ~CMCQueue()
//	Create Date	 :: 2004/01/9
//	Description	 :: 积己磊 家戈磊
//	param		 ::	
//	Return Value ::
//	Bug	Report	 ::
//-----------------------------------------------------------------------------------------------------*/
CMCQueue::CMCQueue(int QueueSize,BOOL bQueueDelete)
{
	m_MaxQueueSize = QueueSize;

	m_bQueueDelete = bQueueDelete;
	m_pBuf = (_Data *)new _Data[QueueSize];
	if(m_pBuf == NULL)
	{
		//	皋葛府甫 棱绰单 角菩 窍看促.
	}

	//	皋葛府 檬扁拳.
	memset(m_pBuf,0,sizeof(_Data) *QueueSize);
	for(int i = 0 ; i < QueueSize ; i++)
	{
		m_pBuf[i].iIndex = i+1;
		m_pBuf[i].iSize = 0;
		m_pBuf[i].pData = NULL;
		m_pBuf[i].s = INVALID_SOCKET;
	}

	iTailPos = 0;
	iHeadPos = 0;
	m_nPackSize = 0;

	
	//农府萍拿 冀记 檬扁拳.
	InitializeCriticalSection(&m_PushCS);
	InitializeCriticalSection(&m_PopCS);
}


CMCQueue::~CMCQueue()
{
	DeleteCriticalSection(&m_PushCS);
	DeleteCriticalSection(&m_PopCS);

	for(int i = 0 ; i < m_MaxQueueSize ; i++)
	{
		if(m_pBuf[i].pData != NULL)
		{

			delete m_pBuf[i].pData;
			m_pBuf[i].pData = NULL;
		}
	}

	if(m_pBuf != NULL)
	{
		delete m_pBuf;
	}

}


//-------------------------------------------------------------------------------------------------------
//	Name		 :: BOOL Push(char cFrom, char * pData, DWORD dwMsgSize, int iIndex)
//	Create Date	 :: 2004/01/9
//	Description	 :: 皋技瘤甫 沥利钮俊 笼绢 持绰促.(券康钮)
//	param		 ::	char cFrom		:	绢叼肺何磐 吭绰啊?		//酒流 救静绊 乐蝶.
//					char * pData	:	单捞鸥 器牢磐.
//					int dwMsgSize	:	皋技瘤 荤捞令.
//					int iIndex		:	皋葛府 牢郸胶.
//	Return Value ::	己傍捞搁 TRUE , 角菩搁 FALSE
//	Bug	Report	 ::
//-------------------------------------------------------------------------------------------------------
BOOL CMCQueue::Push(char cFrom, char * pData, int dwMsgSize, int iIndex)
{
	int err;
	__try {
		EnterCriticalSection( &m_PushCS );
		// 皋矫瘤 钮啊 促 谩促搁 俊矾 
		if (m_nPackSize >= m_MaxQueueSize) return FALSE;

		m_pBuf[iTailPos].pData = (char*) malloc (dwMsgSize + 1);
		if (m_pBuf[iTailPos].pData == NULL) 
		{
			err = ::GetLastError();
 			return FALSE;
		}
		memset(m_pBuf[iTailPos].pData,0, dwMsgSize + 1);
		memcpy(m_pBuf[iTailPos].pData, pData, dwMsgSize);

		m_pBuf[iTailPos].iSize = dwMsgSize;
		// 罐篮 菩哦荐.
		// 滴俺 鞍捞 遏 吧府绰 何盒 吝夸
		InterlockedIncrement(&m_nPackSize);

		//单捞鸥啊 甸绢啊乐绰 器牢磐甫 颗变促.
		iTailPos++;
		if (iTailPos >= m_MaxQueueSize) iTailPos = 0;
	}

	__finally
	{
		LeaveCriticalSection( &m_PushCS );
	}
	return TRUE;
}


//-------------------------------------------------------------------------------------------------------
//	Name		 :: BOOL Pop(char * pFrom, char * pData, DWORD * pMsgSize, int * pIndex)
//	Create Date	 :: 2004/01/9
//	Description	 :: 皋技瘤甫 沥利钮俊辑 哗郴绢 柯促.(券康钮)
//	param		 ::	char * cFrom		:	绢叼肺何磐 吭绰啊?		// 酒流 救静绊 乐促.
//					char * pData		:	单捞鸥 器牢磐.
//					int * dwMsgSize		:	皋技瘤 荤捞令.
//					int * iIndex		:	皋葛府 牢郸胶.
//	Return Value ::	己傍捞搁 TRUE , 角菩搁 FALSE
//	Bug	Report	 ::
//-------------------------------------------------------------------------------------------------------
BOOL CMCQueue::Pop(char * pFrom, char * pData, int * pMsgSize, int * pIndex)
{
	if(InterlockedCompareExchange(&m_nPackSize,0,0) == 0)
	{
		//单捞鸥啊 甸绢 柯霸 绝促.
		return FALSE;
	}

	//// 皋矫瘤 钮啊 促 谩促搁 俊矾 
	//if (m_nPackSize >= m_MaxQueueSize) return FALSE;

	memcpy(pData,((_Data *)m_pBuf + iHeadPos)->pData, ((_Data *)m_pBuf + iHeadPos)->iSize);
	*pMsgSize = ((_Data *)m_pBuf + iHeadPos)->iSize;
	//pIndex = ((_Data *)m_pBuf + iHeadPos)->iIndex;
	
	


	if(m_bQueueDelete == TRUE)
	{
		// 滴俺 鞍捞 遏 吧府绰 何盒 吝夸
		::InterlockedDecrement(&m_nPackSize);

		delete ((_Data *)m_pBuf + iHeadPos)->pData;
		((_Data *)m_pBuf + iHeadPos)->pData = NULL;
        iHeadPos++;
	}

	if (iHeadPos >= m_MaxQueueSize) iHeadPos = 0;

	return TRUE;
}

void CMCQueue::DeletePop()
{
	if(m_bQueueDelete != TRUE)
	{
		// 滴俺 鞍捞 遏 吧府绰 何盒 吝夸
		::InterlockedDecrement(&m_nPackSize);
		
		delete ((_Data *)m_pBuf + iHeadPos)->pData;
		((_Data *)m_pBuf + iHeadPos)->pData = NULL;
        iHeadPos++;
	}
	if (iHeadPos >= m_MaxQueueSize) iHeadPos = 0;

}

⌨️ 快捷键说明

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