- //
- // CDataAdmin.cpp
- //
- #include "stdafx.h"
- #include "CDataAdmin.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- ///////////////////////////////////////////////////////////////////
- CDataAdmin::CDataAdmin()
- {
- m_bFlushing = false;
- // Allocate buffers
- m_pMutex = new CMutex(FALSE, NULL);
- for (int i = 0; i < PACK_TOTAL_COUNT; i++)
- PoolList.AddTail(new MPEG1_PACK);
- // Initialize thread event
- m_hBufEnough = ::CreateEvent(NULL, // pointer to security attributes
- TRUE, // flag for manual-reset event
- FALSE, // flag for initial state
- NULL); // pointer to event-object name
- }
- CDataAdmin::~CDataAdmin()
- {
- // Free data buffers
- PMPEG1_PACK pData;
- POSITION pos = DataList.GetHeadPosition();
- while (pos)
- {
- pData = (PMPEG1_PACK) DataList.GetNext(pos);
- delete pData;
- }
- pos = PoolList.GetHeadPosition();
- while (pos)
- {
- pData = (PMPEG1_PACK) PoolList.GetNext(pos);
- delete pData;
- }
- PoolList.RemoveAll();
- DataList.RemoveAll();
- // Free mutex memory
- if (m_pMutex != NULL)
- {
- delete m_pMutex;
- m_pMutex = NULL;
- }
- if (m_hBufEnough)
- {
- CloseHandle(m_hBufEnough);
- m_hBufEnough = NULL;
- }
- }
- int CDataAdmin::ResetList(void)
- {
- // Initialize the buffer pool
- CSingleLock lock(m_pMutex);
- lock.Lock(1000);
- if (lock.IsLocked())
- {
- while (!DataList.IsEmpty())
- {
- PoolList.AddTail(DataList.RemoveHead());
- }
- lock.Unlock();
- }
- return 1;
- }
- int CDataAdmin::GetListSize(void)
- {
- return (DataList.GetCount() + PoolList.GetCount());
- }
- // Point to the data list head
- PMPEG1_PACK CDataAdmin::PointToDataHead(void)
- {
- if (!DataList.IsEmpty())
- return DataList.GetHead();
- return NULL;
- }
- // Roll back the data buffer
- int CDataAdmin::RollBackDataHead(PMPEG1_PACK pData)
- {
- PMPEG1_PACK pPack = GetDataBuffer();
- if (pPack != NULL)
- ReleaseDataBuffer(pPack);
- return 1;
- }
- // Get a data buffer to read from...
- PMPEG1_PACK CDataAdmin::GetDataBuffer(void)
- {
- CSingleLock lock(m_pMutex);
- PMPEG1_PACK Result = NULL;
- lock.Lock(1000);
- if (lock.IsLocked())
- {
- if (!DataList.IsEmpty())
- Result = DataList.RemoveHead();
- lock.Unlock();
- }
- return Result;
- }
- // Replace the buffer to pool list to receive data...
- int CDataAdmin::ReleaseDataBuffer(PMPEG1_PACK pData)
- {
- CSingleLock lock(m_pMutex);
- int nResult = 0;
- lock.Lock(1000);
- if (lock.IsLocked())
- {
- if (pData != NULL)
- {
- PoolList.AddTail(pData);
- nResult = 1;
- }
- lock.Unlock();
- }
- return nResult;
- }
- // Get a pool buffer to receive data...
- PMPEG1_PACK CDataAdmin::GetWriteBuffer(void)
- {
- CSingleLock lock(m_pMutex);
- PMPEG1_PACK Result = NULL;
- lock.Lock(1000);
- if (lock.IsLocked())
- {
- if (!PoolList.IsEmpty())
- Result = PoolList.RemoveHead();
- lock.Unlock();
- }
- return Result;
- }
- // Place the buffer to data list after receiving data...
- int CDataAdmin::ReleaseWriteBuffer(PMPEG1_PACK pData)
- {
- CSingleLock lock(m_pMutex);
- int nResult = 0;
- lock.Lock(1000);
- if (lock.IsLocked())
- {
- if (pData != NULL)
- {
- DataList.AddTail(pData);
- nResult = 1;
- }
- lock.Unlock();
- }
- return nResult;
- }
- // If no more data available, waiting...
- DWORD CDataAdmin::WaitForNext(DWORD inTimeOut)
- {
- return ::WaitForSingleObject(m_hBufEnough, inTimeOut);
- }
- void CDataAdmin::SetFlushing(bool inFlush)
- {
- m_bFlushing = inFlush;
- }
- bool CDataAdmin::IsFlushing(void)
- {
- return m_bFlushing;
- }