msglist.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:6k
- #include "global.h"
- #include "msglist.h"
- CRITICAL_SECTION g_SyncFlag;
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- CMsgList::CMsgList() {
- m_ppElems = NULL;
- m_nSize = 0;
- m_nHead = 0;
- m_nTail = 0;
- m_nTotal = 0;
- m_nMax = MAXLIST;
- m_bSync = false;
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- CMsgList::~CMsgList() {
- DestroyList();
- if ( m_bSync )
- DeleteCriticalSection( &g_SyncFlag );
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- void CMsgList::DestroyList() {
- // if( m_ppElems ) // cause unkown memory error
- // delete[] m_ppElems;
- // m_ppElems = NULL;
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- bool CMsgList::CreatMsgList( unsigned int size, bool sync ) {
- if ( size < 0 || size > MAXLIST )
- return false;
- m_ppElems = (CMsgElem **)(new LPVOID[size]); // alloc memory
- if ( !m_ppElems )
- return false;
- m_nSize = size;
-
- if ( sync ) { // init sync flag
- m_bSync = true;
- InitializeCriticalSection( &g_SyncFlag );
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- void CMsgList::Lock() {
- if ( m_bSync )
- EnterCriticalSection( &g_SyncFlag );
- }
-
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- void CMsgList::UnLock() {
- if ( m_bSync )
- LeaveCriticalSection( &g_SyncFlag );
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- CMsgElem *CMsgList::Pop() {
- if ( IsEmpty() || NotInit() )
- return NULL;
- CMsgElem *pelem = m_ppElems[m_nTail];
- m_ppElems[m_nTail] = NULL;
- INCSCOPE( m_nTail, 0, m_nSize+1 ); // move pointer forward
- m_nTotal--; // reduce total number
-
- return pelem;
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- CMsgElem *CMsgList::Peek() {
- if ( IsEmpty() || NotInit() )
- return NULL;
- return m_ppElems[m_nTail];
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- bool CMsgList::Push( const CMsgElem *pelem ) {
- if ( NotInit() )
- return false;
- if ( IsFull() && !EnlargeSize() )
- return false;
- m_ppElems[m_nHead] = new CMsgElem();
- if ( !m_ppElems[m_nHead] )
- return false;
- m_ppElems[m_nHead]->m_nMsg = pelem->m_nMsg;
- m_ppElems[m_nHead]->m_dwMsgType = pelem->m_dwMsgType;
- m_ppElems[m_nHead]->m_nParamSize = pelem->m_nParamSize;
- m_ppElems[m_nHead]->m_psBuffer = new char[pelem->m_nParamSize];
- if ( !(m_ppElems[m_nHead]->m_psBuffer) ) {
- delete m_ppElems[m_nHead];
- return false;
- }
- memcpy( m_ppElems[m_nHead]->m_psBuffer, pelem->m_psBuffer,
- pelem->m_nParamSize );
- INCSCOPE( m_nHead, 0, m_nSize+1 ); // move pointer forward
- m_nTotal++; // increase total number
- return true;
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- bool CMsgList::EnlargeSize() {
- if ( m_nSize >= m_nMax || NotInit() )
- return false;
- int newsize = INCSTEP + m_nSize;
- if ( newsize > m_nMax)
- newsize = m_nMax;
- CMsgElem **temp = (CMsgElem **)(new LPVOID[m_nSize]);
- if ( !temp )
- return false;
- memcpy ( temp, m_ppElems, m_nSize*sizeof(LPVOID) );
- // delete[] m_ppElems;
- m_ppElems = (CMsgElem **)(new LPVOID[newsize]);
- if ( !m_ppElems )
- return false;
- int size = m_nSize - m_nTail;
- memcpy( m_ppElems, temp+m_nTail, size*sizeof(LPVOID) );
- memcpy( m_ppElems+size, temp, m_nTail*sizeof(LPVOID) );
- // delete[] temp;
- m_nTail = 0;
- m_nHead = m_nSize;
- m_nSize = newsize;
- return true;
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- CMsgElem::CMsgElem() {
- m_nMsg = 0;
- m_dwMsgType = 0;
- m_nParamSize = 0;
- m_psBuffer = NULL;
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- CMsgElem::~CMsgElem() {
- if ( !m_psBuffer )
- delete[] m_psBuffer;
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- bool CMsgElem::CreateMsgElem( int msg, LPCSTR param, int size, DWORD type ) {
- if ( param && size > 0 ) {
- m_psBuffer = new char[size];
- if ( !m_psBuffer )
- return false;
-
- memcpy( m_psBuffer, param, size );
- m_nParamSize = size;
- }
- else {
- if ( !m_psBuffer ) {
- delete[] m_psBuffer;
- m_psBuffer = NULL;
- }
- m_nParamSize = 0;
- }
- m_nMsg = msg;
- m_dwMsgType = type;
- return true;
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- bool CMsgElem::CreateMsgElemFromBuf( LPCSTR buf, int &size, DWORD type ) {
- int *piParam = (int *)buf;
- size = piParam[1] + sizeof(int)*2;
- return CreateMsgElem( piParam[0], (LPCSTR)(piParam+2), piParam[1], type );
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------