是否有可能知道通过API调用在IOCP上排队了多少未处理的已完成操作?

Law*_*ard 5 .net c# sockets iocp

我正在使用C#套接字(使用IOCP进行回调).我想要一种方法来确定我的处理逻辑落后的天气.是否有API调用可以为我提供未被回调处理的已完成操作的大小?

我已经考虑过使用类似心跳操作的东西,我会发布到队列中,并确定我是否已经过了回调的时间,但我希望在可能的情况下更直接的路由(另外我没有轻松访问)到.Net内部控制的IOCP句柄).

Len*_*ate 4

不是通过记录的 API,但你可以尝试这个......

/*
GetIocpQueueCount

Description:
Returns the number of queued IOCP work and I/O completion items.

Remarks:
Microsoft declined to implement the NtQueryIoCompletion routine
for user mode. This function gets past that omission by calling
the NTDLL.DLL function dynamically.

Returns:
Number of items in the queue at the instant this function was
called, or -1 if an error occurred. Errors can be retrieved
by calling GetLastError.
*/
long GetIocpQueueCount()
{
   long lQueueDepth = -1;

   typedef DWORD (WINAPI *LPFNNTQUERYIOCOMPLETION)(HANDLE, int, PVOID, ULONG, PULONG);

   static LPFNNTQUERYIOCOMPLETION pfnNtQueryIoCompletion = NULL;

   if (MFTASKQUEUENOTCREATED != m_dwStatus)
   {
      DWORD rc = NO_ERROR;

      /* need to load dynamically */

      if (NULL == pfnNtQueryIoCompletion)
      {
         /* Now dynamically obtain the undocumented NtQueryIoCompletion
          * entry point from NTDLL.DLL
          */

         HMODULE hmodDll = ::GetModuleHandleW(L"ntdll.dll");

         // NTDLL is always loaded, just get its handle

         if (NULL != hmodDll)
         {
            pfnNtQueryIoCompletion = (LPFNNTQUERYIOCOMPLETION)::GetProcAddress(
               hmodDll,
               "NtQueryIoCompletion"); // NB: ANSI
         } 
      }

      if (NULL != pfnNtQueryIoCompletion)
      {
         rc = (pfnNtQueryIoCompletion)(
            m_hIOCP,
            0,
            (PVOID)&lQueueDepth,
            sizeof(lQueueDepth),
            NULL);
      }
      else
      {
         rc = ERROR_NOT_FOUND;
      }
      ::SetLastError(rc);
   }
   return lQueueDepth;
}
Run Code Online (Sandbox Code Playgroud)