|
Posted on 2009-09-18 22:33 S.l.e!ep.¢% 阅读(615) 评论(0) 编辑 收藏 引用 所属分类: IOCP
ICOP可以这么简单?
class
CIOCompletionPort
{
public
:
explicit
CIOCompletionPort(size_t maxConcurrency);
~
CIOCompletionPort();
void
AssociateDevice(HANDLE hDevice, ULONG_PTR completionKey);
void
PostStatus(ULONG_PTR completionKey, DWORD dwNumBytes
=
0
, OVERLAPPED
*
pOverlapped
=
0
);
void
GetStatus(ULONG_PTR
*
pCompletionKey, PDWORD pdwNumBytes, OVERLAPPED
**
ppOverlapped);
bool
GetStatus(ULONG_PTR
*
pCompletionKey, PDWORD pdwNumBytes, OVERLAPPED
**
ppOverlapped, DWORD dwMilliseconds);
private
: HANDLE m_iocp;
//
No copies do not implement
CIOCompletionPort(
const
CIOCompletionPort
&
rhs); CIOCompletionPort
&
operator
=
(
const
CIOCompletionPort
&
rhs); }
;
CIOCompletionPort::CIOCompletionPort( size_t maxConcurrency) : m_iocp(::CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL,
0
, maxConcurrency))
{
if
(m_iocp
==
0
)
{
throw
CWin32Exception(_T(
"
CIOCompletionPort::CIOCompletionPort() - CreateIoCompletionPort
"
), ::GetLastError()); }
}
CIOCompletionPort::
~
CIOCompletionPort()
{ ::CloseHandle(m_iocp); }
void
CIOCompletionPort::AssociateDevice( HANDLE hDevice, ULONG_PTR completionKey)
{
if
(m_iocp
!=
::CreateIoCompletionPort(hDevice, m_iocp, completionKey,
0
))
{
throw
CWin32Exception(_T(
"
CIOCompletionPort::AssociateDevice() - CreateIoCompletionPort
"
), ::GetLastError()); }
}
void
CIOCompletionPort::PostStatus( ULONG_PTR completionKey, DWORD dwNumBytes
/**/
/*
= 0
*/
, OVERLAPPED
*
pOverlapped
/**/
/*
= 0
*/
)
{
if
(
0
==
::PostQueuedCompletionStatus(m_iocp, dwNumBytes, completionKey, pOverlapped))
{
throw
CWin32Exception(_T(
"
CIOCompletionPort::PostStatus() - PostQueuedCompletionStatus
"
), ::GetLastError()); }
}
void
CIOCompletionPort::GetStatus( ULONG_PTR
*
pCompletionKey, PDWORD pdwNumBytes, OVERLAPPED
**
ppOverlapped)
{
if
(
0
==
::GetQueuedCompletionStatus(m_iocp, pdwNumBytes, pCompletionKey, ppOverlapped, INFINITE))
{
throw
CWin32Exception(_T(
"
CIOCompletionPort::GetStatus() - GetQueuedCompletionStatus
"
), ::GetLastError()); }
}
bool
CIOCompletionPort::GetStatus( ULONG_PTR
*
pCompletionKey, PDWORD pdwNumBytes, OVERLAPPED
**
ppOverlapped, DWORD dwMilliseconds)
{
bool
ok
=
true
;
if
(
0
==
::GetQueuedCompletionStatus(m_iocp, pdwNumBytes, pCompletionKey, ppOverlapped, dwMilliseconds))
{ DWORD lastError
=
::GetLastError();
if
(lastError
!=
WAIT_TIMEOUT)
{
throw
CWin32Exception(_T(
"
CIOCompletionPort::GetStatus() - GetQueuedCompletionStatus
"
), lastError); }
ok
=
false
; }
return
ok; }
SURE,IOCP 在WINDOWS下是一种机制,一种高效的IO访问机制,‘IO’不仅仅只限于网络,这样就变得简单多了!
|