我正在使用C#套接字(将IOCP用于回调)。我想要一种确定天气的方法,我的处理逻辑落后了。是否有API调用可以给我提供尚未由回调处理的已完成操作的大小?

我曾考虑过使用类似心跳操作的方法,将其发布到队列中,并确定我是否落后于其回调时间,但如果可能的话,我宁愿使用更直接的路由(另外,我无法轻松访问) NET内部控制的IOCP句柄)。

最佳答案

不是通过文档化的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;
}

关于c# - 是否可以通过API调用知道IOCP上有多少未处理的完成操作排队?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17815680/

10-11 13:35