CCirculaQueue

#pragma once

class CCirculaQueue
{
public:
	CCirculaQueue(int maxsize = 256 );
	~CCirculaQueue(void);
	int push_back(void *pBuf);
	void *pop_front();
	void resize(int newsize);
	int size();
	int empty();


protected:
	int m_head;
	int m_tail;
	void **m_pBuf;
	int m_maxsize;
	CRITICAL_SECTION m_CirticalSection;
};


//fdfe
//dfdedfd
//

#include "stdafx.h"
#include "CirculaQueue.h"


CCirculaQueue::CCirculaQueue(int maxsize)
{
	m_head = 0;
	m_tail = 0;
	m_pBuf = 0;
	m_maxsize = maxsize;
	InitializeCriticalSection(&m_CirticalSection);
	resize(m_maxsize);
	
}


CCirculaQueue::~CCirculaQueue(void)
{
	EnterCriticalSection(&m_CirticalSection);
	char *pBuf;
	pBuf = (char *)m_pBuf;
	delete []  pBuf;
	LeaveCriticalSection(&m_CirticalSection);


	DeleteCriticalSection(&m_CirticalSection);
}


void CCirculaQueue::resize(int newsize)
{
	char *pBuf ;
	EnterCriticalSection(&m_CirticalSection);
	if(m_pBuf)
	{
		pBuf = (char *)m_pBuf;
		delete []  pBuf;
	}
	pBuf = new char[sizeof(void *) * newsize];
	m_pBuf = (void **)pBuf;
	m_maxsize = newsize;
	LeaveCriticalSection(&m_CirticalSection);
}


int CCirculaQueue::push_back(void *pBuf)
{
	EnterCriticalSection(&m_CirticalSection);
	//is full ?
	if((m_tail + 1) % m_maxsize == m_head )
		return 0;
	//OK push data
	m_pBuf[m_tail] = pBuf;
	m_tail = (m_tail + 1) % m_maxsize;
	LeaveCriticalSection(&m_CirticalSection);
	return 1;
}


void *CCirculaQueue::pop_front()
{
	void *pRet;
	EnterCriticalSection(&m_CirticalSection);
	// is empty?
	if(m_tail == m_head)
		return 0;
	// OK pop data
	pRet = m_pBuf[m_head];
	m_head = (m_head + 1) % m_maxsize;
	LeaveCriticalSection(&m_CirticalSection);
	return pRet;
}


int CCirculaQueue::size()
{
	EnterCriticalSection(&m_CirticalSection);
	int nRet = (m_tail - m_head + m_maxsize) % m_maxsize ;
	LeaveCriticalSection(&m_CirticalSection);


	return nRet; 
}


int CCirculaQueue::empty()
{
	int nRet;
	EnterCriticalSection(&m_CirticalSection);
	if(m_tail == m_head)
		nRet = 1;
	else
		nRet = 0;
	LeaveCriticalSection(&m_CirticalSection);
	return nRet;
}




使用

#define SOCKET_BUFFER_SIZE 128
声明
WSABUF *m_TempBuffPool[SOCKET_BUFFER_SIZE];
	CCirculaQueue m_BufferPool;//接收缓冲池
        CCirculaQueue m_SendBufferList;//发送缓冲池
初始化
         WSABUF *pWSABuf;
for(int i=0;i<SOCKET_BUFFER_SIZE;i++)
{
	pWSABuf = new WSABUF;
	m_TempBuffPool[i] = pWSABuf;
pWSABuf->len = MAX_PACKAGE_SIZE;
pWSABuf->buf = new char[MAX_PACKAGE_SIZE];
m_BufferPool.push_back(pWSABuf);

</pre><pre code_snippet_id="688647" snippet_file_name="blog_20150609_10_5007296" name="code" class="cpp">       接收缓冲池中数据不足,则等待数据到达
SABUF *pWSABuf = NULL;
/*while(m_BufferPool.size()<4) 
	</span>Sleep(1) ;*/
pWSABuf = (WSABUF *)m_BufferPool.pop_front();//接收缓冲区弹出数据

 
             
              if(m_BufferPool.size()<4)
{
Sleep(10);
continue;
}

nRet = recvfrom(m_RecvSocket,pWSABuf->buf,MAX_PACKAGE_SIZE,0,NULL,NULL);
//AfxMessageBox(_T("接受到流!"));
	if(SOCKET_ERROR == nRet)
{
int LastError = WSAGetLastError();
if(LastError != WSAETIMEDOUT)
{
swprintf_s(m_ErrMsgBuf,100,_T("%s line %d:接收失败!\n"), //,错误号:%d; 
WIDEN(__FILE__),__LINE__,LastError);
}
continue;
}
WSABuf->len = nRet;
m_SendBufferList.push_back(pWSABuf);//存入发送缓冲区
</pre><pre code_snippet_id="688647" snippet_file_name="blog_20150609_15_5270209" name="code" class="cpp">最后释放

       for(int i=0;i<SOCKET_BUFFER_SIZE;i++)
{
delete[] m_TempBuffPool[i]->buf;
delete m_TempBuffPool[i];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值