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

📄 queue.cpp

📁 EVC下的串口测试工具
💻 CPP
字号:
#include "StdAfx.h"
#include "Queue.h"

CQueue::CQueue(int cnt)
{
	if(cnt > 1)
	{
		m_size = cnt;
		m_data = new QNode[m_size];
	}
	else
	{
		m_data = NULL;
		m_size = 0;
	}

	buffCounter = 0;
	m_count = 0;
	m_head = 0;
	m_tale = 0;

	hNotEmpty[0] = CreateEvent(NULL, TRUE, FALSE, NULL);
	hNotEmpty[1] = CreateEvent(NULL, TRUE, FALSE, NULL);
	hNotFull[0] = CreateEvent(NULL, TRUE, TRUE, NULL);
	hNotFull[1] = CreateEvent(NULL, TRUE, FALSE, NULL);
	if( hNotEmpty[0] == INVALID_HANDLE_VALUE ||
		hNotEmpty[1] == INVALID_HANDLE_VALUE ||
		hNotFull[0] == INVALID_HANDLE_VALUE ||
		hNotFull[1] == INVALID_HANDLE_VALUE )
		AfxMessageBox(TEXT("ERROR : Create Queue Event Handle Fail.\r\n"));
}

CQueue::~CQueue(void)
{
	Clear();
	if(m_data != NULL)
		delete[] m_data;
	if( hNotEmpty[0] != INVALID_HANDLE_VALUE)
		CloseHandle(hNotEmpty[0]);
	if(hNotFull[0] == INVALID_HANDLE_VALUE)
		CloseHandle(hNotFull[0]);
	if( hNotEmpty[1] != INVALID_HANDLE_VALUE)
		CloseHandle(hNotEmpty[1]);
	if(hNotFull[1] == INVALID_HANDLE_VALUE)
		CloseHandle(hNotFull[1]);
}

int CQueue::GetCount(void)
{
	return m_count;
}

BOOL CQueue::Push(QNode *data)
{

	if(IsFull())
		return FALSE;

	m_data[m_head].data = data->data;
	m_data[m_head].size = data->size;
	m_head = (m_head+1)%m_size;
	m_count++;

	SetEvent(hNotEmpty[1]);
	if(IsFull())
		ResetEvent(hNotFull[1]);
	return TRUE;
}

BOOL CQueue::Pop(QNode *data)
{
	if(IsEmpty())
		return FALSE;
	
	data->data = m_data[m_tale].data;
	data->size = m_data[m_tale].size;
	m_tale = (m_tale+1)%m_size;
	buffCounter = 0;
	m_count--;

	SetEvent(hNotFull[1]);
	if(IsEmpty())
		ResetEvent(hNotEmpty[1]);
	return TRUE;
}

DWORD CQueue::QueueReader(BYTE *buff, unsigned int size, DWORD timeout, char endchar)
{
	unsigned int index;
	for(index=0; index<size; index++)
	{
		if( (WaitForData(timeout) == WAIT_TIMEOUT ) ||
			(WaitForData(timeout) == WAIT_OBJECT_0 ) )
			break;

		if(m_data[m_tale].size == buffCounter)
		{
			LocalFree(m_data[m_tale].data);
			m_tale = (m_tale+1)%m_size;
			m_count--;

			SetEvent(hNotFull[1]);
			if(IsEmpty())
				ResetEvent(hNotEmpty[1]);

			buffCounter = 0;
			index--;
		}
		else
		{
			buff[index] = m_data[m_tale].data[buffCounter++];
			if(buff[index] == endchar)
			{
				index++;
				break;
			}
		}
	}
	return index;
}

void CQueue::SetReady()
{
	ResetEvent(hNotEmpty[0]);
	ResetEvent(hNotFull[0]);
}

void CQueue::Clear()
{
	SetEvent(hNotEmpty[0]);
	SetEvent(hNotFull[0]);
	while(IsEmpty() == FALSE)
	{
		QNode release;
		Pop(&release);
		LocalFree(release.data);
	}
	m_count = 0;
	m_head = 0;
	m_tale = 0;

	ResetEvent(hNotEmpty[1]);
	SetEvent(hNotFull[1]);
}

BOOL CQueue::IsEmpty()
{
	if(m_data == NULL)
		return FALSE;

	return (m_head == m_tale);
}

BOOL CQueue::IsFull()
{
	return ( ((m_head+1)%m_size) == m_tale);
}

DWORD CQueue::WaitForData(DWORD time)
{
	return WaitForMultipleObjects(2, hNotEmpty, FALSE, time);
//	return WaitForSingleObject(hNotEmpty, time);
}

DWORD CQueue::WaitForSpace(DWORD time)
{
	return WaitForMultipleObjects(2, hNotFull, FALSE, time);
//	return WaitForSingleObject(hNotFull, time);
}

⌨️ 快捷键说明

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