173 lines
3.6 KiB
C++
173 lines
3.6 KiB
C++
#include "stdafx.h"
|
|
#include <WBANetwork/Common.h>
|
|
#include <WBANetwork/WBANetwork.h>
|
|
#include <WBANetwork/util/EventQueue.h>
|
|
#include <WBANetwork/net/Socket.h>
|
|
#include <WBANetwork/util/AsyncResult.h>
|
|
using namespace WBANetwork;
|
|
|
|
EventQueue::EventQueue()
|
|
: m_handleQueue( INVALID_HANDLE_VALUE )
|
|
{
|
|
}
|
|
|
|
EventQueue::~EventQueue()
|
|
{
|
|
Close();
|
|
}
|
|
|
|
bool EventQueue::Create( int threadCount )
|
|
{
|
|
if( m_handleQueue != INVALID_HANDLE_VALUE )
|
|
{
|
|
//_ASSERTE(!"EventQueue::Create");
|
|
return false;
|
|
}
|
|
|
|
m_handleQueue = ::CreateIoCompletionPort( INVALID_HANDLE_VALUE, 0, 0, threadCount );
|
|
|
|
if( NULL == m_handleQueue )
|
|
{
|
|
CallbackErrorHandler( ::GetLastError(),
|
|
_T("[WBANetwork::EventQueue::Create] CreateIoCompletionPort failed") );
|
|
//_ASSERTE(!"EventQueue::Create");
|
|
|
|
m_handleQueue = INVALID_HANDLE_VALUE;
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void EventQueue::Close()
|
|
{
|
|
if( INVALID_HANDLE_VALUE == m_handleQueue )
|
|
{
|
|
return;
|
|
}
|
|
|
|
::CloseHandle( m_handleQueue );
|
|
m_handleQueue = INVALID_HANDLE_VALUE;
|
|
}
|
|
|
|
bool EventQueue::AddEvent( EventHandler* handler )
|
|
{
|
|
if( NULL == handler )
|
|
{
|
|
//_ASSERTE(!"EventQueue::AddEvent");
|
|
return false;
|
|
}
|
|
|
|
HANDLE ret = ::CreateIoCompletionPort( handler->GetHandle()->GetNativeHandle(),
|
|
m_handleQueue,
|
|
( ULONG_PTR )handler,
|
|
0 );
|
|
if( NULL == ret )
|
|
{
|
|
DWORD err = ::GetLastError();
|
|
|
|
if( ERROR_INVALID_PARAMETER != err )
|
|
{
|
|
CallbackErrorHandler( err,
|
|
_T("[WBANetwork::EventQueue::AddEvent] CreateIoCompletionPort failed") );
|
|
//_ASSERTE(!"EventQueue::AddEvent");
|
|
}
|
|
}
|
|
|
|
return ( NULL != ret );
|
|
}
|
|
|
|
void EventQueue::RemoveEvent( EventHandler* handler )
|
|
{
|
|
if( NULL == handler )
|
|
{
|
|
//_ASSERTE(!"EventQueue::RemoveEvent");
|
|
return;
|
|
}
|
|
|
|
handler->GetHandle()->Close();
|
|
}
|
|
|
|
EventHandler* EventQueue::GetCompletionEvent( AsyncResult** result, int ms )
|
|
{
|
|
if( NULL == result ||
|
|
INVALID_HANDLE_VALUE == m_handleQueue )
|
|
{
|
|
//_ASSERTE(!"EventQueue::GetCompletionEvent");
|
|
return 0;
|
|
}
|
|
|
|
DWORD transBytes = 0;
|
|
//ULONG key = 0;
|
|
ULONG_PTR key = 0;
|
|
//typedef unsigned long ULONG;
|
|
//PULONG_PTR abc;
|
|
//typedef _W64 unsigned long ULONG_PTR, *PULONG_PTR;
|
|
|
|
BOOL ret = ::GetQueuedCompletionStatus( m_handleQueue,
|
|
&transBytes,
|
|
&key,
|
|
( LPOVERLAPPED* )result,
|
|
ms );
|
|
|
|
//EventHandler* handler = ( EventHandler* )(u_int64)key;
|
|
EventHandler* handler = ( EventHandler* )key;
|
|
//DWORD error = ::WSAGetLastError();
|
|
|
|
if(ret && key != NULL && *result != NULL) //¼º°ø
|
|
{
|
|
( *result )->transBytes = transBytes;
|
|
( *result )->handler = handler;
|
|
}
|
|
else
|
|
{
|
|
DWORD error = ::GetLastError();
|
|
|
|
if(NULL == *result)
|
|
{
|
|
CallbackErrorHandler( error, _T("[WBANetwork::EventQueue::GetCompletionEvent] GetQueuedCompletionStatus TimeOut") );
|
|
return 0;
|
|
}
|
|
else
|
|
{
|
|
CallbackErrorHandler( error, _T("[WBANetwork::EventQueue::GetCompletionEvent] GetQueuedCompletionStatus fail") );
|
|
}
|
|
|
|
if(*result != NULL)
|
|
{
|
|
( *result )->transBytes = transBytes;
|
|
( *result )->handler = handler;
|
|
( *result )->error = error;
|
|
}
|
|
}
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
void EventQueue::PostCompletion( EventHandler* handler, AsyncResult* result )
|
|
{
|
|
BOOL bResult = 0;
|
|
|
|
if( NULL != result )
|
|
{
|
|
bResult = ::PostQueuedCompletionStatus( m_handleQueue,
|
|
result->transBytes,
|
|
( ULONG_PTR )handler,
|
|
result );
|
|
}
|
|
else
|
|
{
|
|
bResult = ::PostQueuedCompletionStatus( m_handleQueue,
|
|
0,
|
|
( ULONG_PTR )handler,
|
|
0 );
|
|
}
|
|
|
|
if( !bResult )
|
|
{
|
|
CallbackErrorHandler( ::GetLastError(),
|
|
_T("[WBANetwork::EventQueue::PostCompletion] PostQueuedCompletionStatus Failed") );
|
|
|
|
}
|
|
} |