头文件:
/**********************************************************************
* Copyright (C) 2008 - - All Rights Reserved
*
* 文件名称: IOCP.h
* 摘 要: IOCP包裹类
*
* 作 者: yanglinbo,
* 修 改: 查看文件最下方.
*
***********************************************************************/
#ifndef __IOCP_H__
#define __IOCP_H__
#include <assert.h>
#include <WinSock2.h>
//////////////////////////////////////////////////////////////////////////
/// IOCP包装类,完成IOCP创建,关联等动作
//////////////////////////////////////////////////////////////////////////
class CIOCP
{
public:
CIOCP(int nMaxConcurrency = -1);
virtual ~CIOCP(void);
CIOCP& operator = (const CIOCP& rht);
CIOCP& operator = (const HANDLE& rht);
operator HANDLE() const;
/// 创建IOCP
/// @param [in] nMaxConcurrency 最大线程数
BOOL Create(int nMaxConcurrency);
/// 销毁IOCP
BOOL Destroy();
/// 完全端口是否有效
BOOL IsValid() const;
/// 关联指定SOCKET及参数到IOCP
/// @param [in] socket
/// @param [in] CompKey
BOOL AssociateSocket(SOCKET socket, ULONG_PTR CompKey);
/// 发送通知到IOCP
/// @param [in] CompKey
/// @param [in] dwNumBytes
/// @param [in] po
BOOL PostStatus(ULONG_PTR CompKey, DWORD dwNumBytes = 0, OVERLAPPED* po = NULL);
/// 取得IOCP状态
/// @param [in] pCompKey
/// @param [in] pdwNumBytes
/// @param [in] ppo
/// @param [in] dwMilliseconds
BOOL GetStatus(ULONG_PTR* pCompKey, PDWORD pdwNumBytes, OVERLAPPED** ppo, DWORD dwMilliseconds = INFINITE);
private:
HANDLE m_hIOCP; ///< IOCP句柄
};
#endif
cpp文件:
/**//**********************************************************************
* Copyright (C) 2008 - - All Rights Reserved
*
* 文件名称: IOCP.cpp
* 摘 要: IOCP包裹类
*
* 作 者: yanglinbo,
* 修 改: 查看文件最下方.
*
***********************************************************************/
#include "StdAfx.h"
#include "IOCP.h"
CIOCP::CIOCP( int nMaxConcurrency /**//*= -1*/ )
: m_hIOCP(INVALID_HANDLE_VALUE)
{
if (nMaxConcurrency != -1)
{
Create(nMaxConcurrency);
}
}
CIOCP::~CIOCP( void )
{
Destroy();
}
CIOCP& CIOCP::operator=( const CIOCP& rht )
{
m_hIOCP = rht.m_hIOCP;
return *this;
}
CIOCP& CIOCP::operator=( const HANDLE& rht )
{
m_hIOCP = rht;
return *this;
}
CIOCP::operator HANDLE() const
{
return (HANDLE)(this == NULL ? NULL : m_hIOCP);
}
BOOL CIOCP::Create( int nMaxConcurrency )
{
m_hIOCP = ::CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, nMaxConcurrency);
ASSERT(IsValid());
return IsValid();
}
BOOL CIOCP::Destroy()
{
if (IsValid() == FALSE) return TRUE;
BOOL bClosed = ::CloseHandle(m_hIOCP);
m_hIOCP = INVALID_HANDLE_VALUE;
return bClosed;
}
BOOL CIOCP::AssociateSocket( SOCKET socket, ULONG_PTR CompKey )
{
ASSERT(IsValid());
if ( IsValid() == FALSE ) return FALSE;
return (::CreateIoCompletionPort((HANDLE)socket, m_hIOCP, CompKey, 0) == m_hIOCP);
}
BOOL CIOCP::PostStatus( ULONG_PTR CompKey, DWORD dwNumBytes /**//*= 0*/, OVERLAPPED* po /**//*= NULL*/ )
{
ASSERT(IsValid());
if ( IsValid() == FALSE ) return FALSE;
return ::PostQueuedCompletionStatus(m_hIOCP, dwNumBytes, CompKey, po);
}
BOOL CIOCP::GetStatus( ULONG_PTR* pCompKey, PDWORD pdwNumBytes, OVERLAPPED** ppo, DWORD dwMilliseconds /**//*= INFINITE*/ )
{
ASSERT(IsValid());
if ( IsValid() == FALSE ) return FALSE;
return ::GetQueuedCompletionStatus(m_hIOCP, pdwNumBytes, pCompKey, ppo, dwMilliseconds);
}
BOOL CIOCP::IsValid() const
{
return (m_hIOCP != NULL) && (m_hIOCP != INVALID_HANDLE_VALUE);
}