#include "StreamHandler.h" #include "StreamBuffer.h" #include #include #include //#define HANDLER_LOG #ifdef HANDLER_LOG #include std::ofstream HandlerLog; #endif ///////////////////////////////////////////////////////////////////////////////////////// // DWORD __stdcall NotificationProc( void * ); ///////////////////////////////////////////////////////////////////////////////////////// // CStreamHandler::CStreamHandler() : m_pHandles( new HANDLES ) , m_pStreamBuffers( new STREAMBUFFERS ) , m_dwNotifyThreadID( 0 ) , m_hNotifyThread( 0 ) { #ifdef HANDLER_LOG HandlerLog.open( "HandlerLog.txt" ); #endif } ///////////////////////////////////////////////////////////////////////////////////////// // CStreamHandler::~CStreamHandler() { Destroy(); delete m_pHandles; delete m_pStreamBuffers; #ifdef HANDLER_LOG HandlerLog.close(); #endif } ///////////////////////////////////////////////////////////////////////////////////////// // void CStreamHandler::Create() { m_hNotifyThread = CreateThread( NULL, 0, NotificationProc, this, 0, &m_dwNotifyThreadID ); } ///////////////////////////////////////////////////////////////////////////////////////// // void CStreamHandler::Destroy() { if( m_hNotifyThread != 0 ) { PostThreadMessage( m_dwNotifyThreadID, WM_QUIT, 0, 0 ); WaitForSingleObject( m_hNotifyThread, INFINITE ); CloseHandle( m_hNotifyThread ); m_dwNotifyThreadID = 0; m_hNotifyThread = 0; } m_pHandles->clear(); m_pStreamBuffers->clear(); } ///////////////////////////////////////////////////////////////////////////////////////// // void CStreamHandler::Add( CStreamBuffer & streamBuffer ) { // m_pHandles->push_back( streamBuffer.GetEventNotify() ); m_pStreamBuffers->push_back( &streamBuffer ); UpdateStreamHandles(); } ///////////////////////////////////////////////////////////////////////////////////////// // void CStreamHandler::Remove( CStreamBuffer & streamBuffer ) { STREAMBUFFERS::iterator it = find( m_pStreamBuffers->begin(), m_pStreamBuffers->end(), &streamBuffer ); if( it != m_pStreamBuffers->end() ) m_pStreamBuffers->erase( it ); /* HANDLES::iterator it2 = find( m_pHandles->begin(), m_pHandles->end(), streamBuffer.GetEventNotify() ); if( it2 != m_pHandles->end() ) m_pHandles->erase( it2 );*/ UpdateStreamHandles(); } void CStreamHandler::UpdateStreamHandles() { m_pHandles->clear(); STREAMBUFFERS::iterator it = m_pStreamBuffers->begin(); STREAMBUFFERS::iterator it_end = m_pStreamBuffers->end(); //MsgWaitForMultipleObject°¡ Ç®¸®µµ·Ï ¾Æ¹« ¸Þ½ÃÁö³ª º¸³¿. PostThreadMessage( m_dwNotifyThreadID, WM_PAINT, 0, 0 ); for( ; it != it_end; it++ ) { CStreamBuffer * p = *it; m_pHandles->push_back( p->GetEventNotify() ); } #ifdef HANDLER_LOG char szBuf[1024]; HandlerLog << "---UpdateStreamHandles Begin---\n"; for( int i = 0; i < m_pHandles->size(); i++ ) { HANDLE h = m_pHandles->at( i ); sprintf( szBuf, "Handle #%d : 0x%X", i, h ); HandlerLog << szBuf << endl; } HandlerLog << "---UpdateStreamHandles End---\n"; #endif } ///////////////////////////////////////////////////////////////////////////////////////// // DWORD __stdcall NotificationProc( void * lpParameter ) { typedef CStreamHandler::HANDLES HANDLES; CStreamHandler * pHandler = (CStreamHandler*)lpParameter; try { while( true ) { unsigned nHandles = pHandler->m_pHandles->size(); if( nHandles <= 0 ) continue; vector Handles = *pHandler->m_pHandles; // HANDLE * pHandles = &pHandler->m_pHandles->at( 0 ); #ifdef HANDLER_LOG HandlerLog << "--NotificationProc--\n"; char szBuf[256]; for( int i = 0; i < nHandles; i++ ) { sprintf( szBuf, "Handle #%d : 0x%X", i, Handles[i] ); HandlerLog << szBuf << endl; } HandlerLog << "--------------------\n"; #endif DWORD dwResult = MsgWaitForMultipleObjects( nHandles, &Handles[0], FALSE, INFINITE, QS_ALLEVENTS ); int index = dwResult - WAIT_OBJECT_0; if( index >= 0 && index < nHandles ) { CStreamBuffer * pStreamBuffer = pHandler->m_pStreamBuffers->at( index ); pStreamBuffer->HandleNotification(); #ifdef HANDLER_LOG if( Handles[index] != pStreamBuffer->GetEventNotify() ) { MessageBox( NULL, "¿¡·¯!!!!!!!!!!!!", "»ç¿îµå ÇÚµé ¾È¸ÂÀ½.", MB_OK ); exit( 0 ); } #endif } else { MSG msg; while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) { if( msg.message == WM_QUIT ) return 0; } } } } catch( exception & e ) { MessageBox( NULL, e.what(), "Error!!", MB_OK ); } return 0; } ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// // CStreamUpdater::CStreamUpdater() : m_pHandles( new HANDLES ) , m_pStreamBuffers( new STREAMBUFFERS ) { } ///////////////////////////////////////////////////////////////////////////////////////// // CStreamUpdater::~CStreamUpdater() { Destroy(); delete m_pHandles; delete m_pStreamBuffers; } ///////////////////////////////////////////////////////////////////////////////////////// // void CStreamUpdater::Create() { } ///////////////////////////////////////////////////////////////////////////////////////// // void CStreamUpdater::Destroy() { m_pHandles->clear(); m_pStreamBuffers->clear(); } ///////////////////////////////////////////////////////////////////////////////////////// // void CStreamUpdater::Add( CStreamBuffer & streamBuffer ) { m_pHandles->push_back( streamBuffer.GetEventNotify() ); m_pStreamBuffers->push_back( &streamBuffer ); } ///////////////////////////////////////////////////////////////////////////////////////// // void CStreamUpdater::Remove( CStreamBuffer & streamBuffer ) { STREAMBUFFERS::iterator it = find( m_pStreamBuffers->begin(), m_pStreamBuffers->end(), &streamBuffer ); if( it != m_pStreamBuffers->end() ) m_pStreamBuffers->erase( it ); HANDLES::iterator it2 = find( m_pHandles->begin(), m_pHandles->end(), streamBuffer.GetEventNotify() ); if( it2 != m_pHandles->end() ) m_pHandles->erase( it2 ); } ///////////////////////////////////////////////////////////////////////////////////////// // void CStreamUpdater::Update() { static bool b = true; try { if( b ) { unsigned nHandles = m_pHandles->size(); if( nHandles <= 0 ) return; HANDLE * pHandles = &m_pHandles->at( 0 ); if( pHandles ) { DWORD dwResult = MsgWaitForMultipleObjects( nHandles, pHandles, FALSE, 0, QS_ALLEVENTS ); int index = dwResult - WAIT_OBJECT_0; if( index >= 0 && index < nHandles ) { CStreamBuffer * pStreamBuffer = m_pStreamBuffers->at( index ); if( pStreamBuffer ) pStreamBuffer->HandleNotification(); } } } } catch( exception & e ) { MessageBox( NULL, e.what(), "Error!!", MB_OK ); //exit( 0 ); b = false; } } /////////////////////////////////////////////////////////////////////////////////////////