#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];
}