📄 senddatapool.cpp
字号:
// SendDataPool.cpp: implementation of the CSendDataPool class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "LiveController.h"
#include "SendDataPool.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define DATA_CELL_SIZE 1024
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSendDataPool::CSendDataPool()
{
// input initialize
m_nDataCellSize = DATA_CELL_SIZE;
if ( m_nDataCellSize > 0 )
{
m_hSemaphore_Full = CreateSemaphore( NULL, 0, m_nDataCellSize, NULL );
m_hSemaphore_Empty = CreateSemaphore( NULL, m_nDataCellSize, m_nDataCellSize, NULL );
m_pDataCell = new LPSD_DATA_CELL[m_nDataCellSize];
for ( int i=0; i<m_nDataCellSize; i++ )
{
m_pDataCell[i] = NULL;
}
m_nCellGetIndex = 0;
m_nCellPutIndex = 0;
}
}
CSendDataPool::~CSendDataPool()
{
if ( m_nDataCellSize > 0 )
{
if ( m_hSemaphore_Full )
{
CloseHandle( m_hSemaphore_Full );
m_hSemaphore_Full = NULL;
}
if ( m_hSemaphore_Empty )
{
CloseHandle( m_hSemaphore_Empty );
m_hSemaphore_Empty = NULL;
}
}
if ( m_pDataCell )
{
for ( int i=0; i<m_nDataCellSize; i++ )
{
delete m_pDataCell[i];
m_pDataCell[i] = NULL;
}
delete [m_nDataCellSize]m_pDataCell;
}
}
BOOL CSendDataPool::RemoveNewData( DWORD dwWaitMillSecond )
{
if ( m_nDataCellSize <= 0 ) {
return FALSE;
}
DWORD dwWaitResult;
// Try to enter the semaphore gate.
dwWaitResult = WaitForSingleObject(
m_hSemaphore_Full, // handle to full semaphore
dwWaitMillSecond ); // time-out interval
if ( dwWaitResult == WAIT_OBJECT_0 )
{
// The semaphore object was signaled.
LPSD_DATA_CELL pDataCell = m_pDataCell[m_nCellGetIndex];
ASSERT(pDataCell);
m_pDataCell[m_nCellGetIndex] = NULL;
m_nCellGetIndex++;
if ( m_nCellGetIndex >= m_nDataCellSize )
m_nCellGetIndex = 0;
// Increment the count of the Empty_semaphore
ReleaseSemaphore( m_hSemaphore_Empty, 1, NULL );
delete pDataCell;
return TRUE;
}
return FALSE;
}
SD_DATA_CELL* CSendDataPool::GetNewDataRef( DWORD dwWaitMillSecond )
{
if ( m_nDataCellSize <= 0 ) {
return NULL;
}
DWORD dwWaitResult;
dwWaitResult = WaitForSingleObject(
m_hSemaphore_Full, // handle to full semaphore
dwWaitMillSecond ); // time-out interval
if ( dwWaitResult == WAIT_OBJECT_0 )
{
// The semaphore object was signaled.
ReleaseSemaphore( m_hSemaphore_Full, 1, NULL );
return m_pDataCell[m_nCellGetIndex];
}
return NULL;
}
BOOL CSendDataPool::PutNewData( int nMsg, int cParams, DWORD *pParams, DWORD nWaitTime )
{
if ( m_nDataCellSize <= 0 )
{
return FALSE;
}
DWORD dwWaitResult;
dwWaitResult = WaitForSingleObject( m_hSemaphore_Empty, nWaitTime );
if ( dwWaitResult == WAIT_OBJECT_0 )
{
SD_DATA_CELL *pNewData = new SD_DATA_CELL;
if ( pNewData == NULL )
{
ReleaseSemaphore( m_hSemaphore_Empty , 1, NULL );
return FALSE;
}
pNewData->nMsg = nMsg;
if ( cParams > MAX_PARAMCOUNT ) {
cParams = MAX_PARAMCOUNT;
}
pNewData->nParamCount = cParams;
for ( int i=0; i<cParams; i++ ) {
pNewData->dwParams[i] = pParams[i];
}
pNewData->nLastSendTime = 0;
pNewData->nTryedTimes = 0;
m_pDataCell[m_nCellPutIndex] = pNewData;
m_nCellPutIndex++;
if ( m_nCellPutIndex >= m_nDataCellSize )
m_nCellPutIndex = 0;
ReleaseSemaphore( m_hSemaphore_Full, 1, NULL );
return TRUE;
}
return FALSE;
}
void CSendDataPool::RemoveAllCell()
{
BOOL bRt = FALSE;
do {
bRt = RemoveNewData();
} while(bRt == FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -