The IO Completion Port C Class (IOCP.h)

[Previous] [Next]

The simple CIOCP C++ class, shown in Listing B-5, just wraps the Windows I/O completion port kernel object functions. All the methods are inline, so there is no performance penalty when you use the class. Wrapping the functions allows you to manipulate an I/O completion port object with a more logical interface.

Listing B-5. The IOCP.h header file

 

IOCP.h

/****************************************************************************** Module: IOCP.h Notices: Copyright (c) 2000 Jeffrey Richter Purpose: This class wraps an I/O Completion Port. See Appendix B. ******************************************************************************/ #pragma once // Include this header file once per compilation unit /////////////////////////////////////////////////////////////////////////////// #include "..\CmnHdr.h" // See Appendix A. /////////////////////////////////////////////////////////////////////////////// class CIOCP { public: CIOCP(int nMaxConcurrency = -1) { m_hIOCP = NULL; if (nMaxConcurrency != -1) (void) Create(nMaxConcurrency); } ~CIOCP() { if (m_hIOCP != NULL) chVERIFY(CloseHandle(m_hIOCP)); } BOOL Create(int nMaxConcurrency = 0) { m_hIOCP = CreateIoCompletionPort( INVALID_HANDLE_VALUE, NULL, 0, nMaxConcurrency); chASSERT(m_hIOCP != NULL); return(m_hIOCP != NULL); } BOOL AssociateDevice(HANDLE hDevice, ULONG_PTR CompKey) { BOOL fOk = (CreateIoCompletionPort(hDevice, m_hIOCP, CompKey, 0) == m_hIOCP); chASSERT(fOk); return(fOk); } BOOL AssociateSocket(SOCKET hSocket, ULONG_PTR CompKey) { return(AssociateDevice((HANDLE) hSocket, CompKey)); } BOOL PostStatus(ULONG_PTR CompKey, DWORD dwNumBytes = 0, OVERLAPPED* po = NULL) { BOOL fOk = PostQueuedCompletionStatus(m_hIOCP, dwNumBytes, CompKey, po); chASSERT(fOk); return(fOk); } BOOL GetStatus(ULONG_PTR* pCompKey, PDWORD pdwNumBytes, OVERLAPPED** ppo, DWORD dwMilliseconds = INFINITE) { return(GetQueuedCompletionStatus(m_hIOCP, pdwNumBytes, pCompKey, ppo, dwMilliseconds)); } private: HANDLE m_hIOCP; }; ///////////////////////////////// End of File /////////////////////////////////



Programming Server-Side Applications for Microsoft Windows 2000
Programming Server-Side Applications for Microsoft Windows 2000 (Microsoft Programming)
ISBN: 0735607532
EAN: 2147483647
Year: 2000
Pages: 126

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net