PostQueuedCompletionStatus功能函数向一个已经初始完的I/O端口发送数据包,触发GetQueuedCompletionStatus功能调用函数来取得数据包
BOOL PostQueuedCompletionStatus(
HANDLE CompletionPort, // handle to an I/O completion port
DWORDdwNumberOfBytesTransferred, // value to return via GetQueuedCompletionStatus' lpNumberOfBytesTranferred
DWORD dwCompletionKey, // value to return via GetQueuedCompletionStatus' lpCompletionKey
LPOVERLAPPED lpOverlapped // value to return via GetQueuedCompletionStatus'lpOverlapped
);
以上PostQueuedCompletionStatus的三个参数的值正好由GetQueuedCompletionStatus函数接收
BOOL GetQueuedCompletionStatus(
HANDLE CompletionPort, // handle to completion port
LPDWORD lpNumberOfBytes, // bytes transferred
PULONG_PTR lpCompletionKey, // file completion key
LPOVERLAPPED *lpOverlapped, // buffer
DWORD dwMilliseconds // optional timeout value
);
线程中,不断的执行GetQueuedCompletionStatus来从消息队列中读取数据
例如某一线程执行函数RepetitionRun:
//Run函数
bool CQueueServiceThread::RepetitionRun()
{
//效验参数
GT_ASSERT(m_hCompletionPort!=NULL);
//变量定义
DWORD dwThancferred=0;
OVERLAPPED * pOverLapped=NULL;
CQueueService * pQueueService=NULL;
//等待完成端口
if (GetQueuedCompletionStatus(m_hCompletionPort,&dwThancferred,(PULONG_PTR)&pQueueService,&pOverLapped,INFINITE))
{
//判断退出
if (pQueueService==NULL) return false;
//获取数据
tagDataHead DataHead;
bool bSuccess=pQueueService->GetData(DataHead,m_cbBuffer,sizeof(m_cbBuffer));
GT_ASSERT(bSuccess==true);
//处理数据
if (bSuccess==true) pQueueService->OnQueueServiceThread(m_cbThreadIndex, DataHead,m_cbBuffer,DataHead.wDataSize);
return true;
}
return false;
}
CreateIoCompletionPort则是创建消息队列
HANDLE CreateIoCompletionPort (
HANDLE FileHandle, // handle to file
HANDLE ExistingCompletionPort, // handle to I/O completion port
ULONG_PTR CompletionKey, // completion key
DWORD NumberOfConcurrentThreads // number of threads to execute concurrently
);