提升未调用的SSL async_shutdown完成处理程序

时间:2013-08-22 06:25:18

标签: c++ boost boost-asio

我在OpenSSL 1.0.1e上使用了boost 1.54.0。

不时关闭SSL连接对象时,我发现async_shutdown()的完成处理程序未被调用。

经过调试后我发现,当出现async_write()时会发生这种情况。

SSL async_shutdown()应该发送SSL警报(Closing),因此我们在这里有2次写入。 我知道禁止多个async_write()。

我该如何应对这种情况? 我应该在调用SSL async_shutdown()之前等待async_write()完成吗?

编辑:根据this我可能需要在底层TCP套接字上取消()以取消所有未完成的异步操作。这是对的吗?

编辑如果我一直在使用async_ API,是否可以致电shutdown()或者我必须致电async_shutdown()

1 个答案:

答案 0 :(得分:2)

这是我处理关闭的方式。在此之前,我遇到了关闭问题。这段代码干净地关闭了所有东西:

void SSLSocket::Stop()
{
   // This method calls the shutdown method on the socket in order to stop reads or writes that might be going on.  If this is not done, then an exception will be thrown
   // when it comes time to delete this object.
   //
   boost::system::error_code EC;
   try
   {
      // This method can be called from the handler as well.  So once the ShuttingDown flag is set, don't go throught the same code again.
      //
      LockCode->Acquire(); // Single thread the code.
      if (ShuttingDown)
         return;
      ShuttingDown = true;
      pSocket->next_layer().cancel();
      pSocket->shutdown(EC);
      if (EC)
      {
         stringstream ss;
         ss << "SSLSocket::Stop: socket shutdown error - " << EC.message() << ".\n";
         // Log.LogString(ss.str(), LogError); // Usually get this - probably not an error.
      }
      else
      {
         pSocket->next_layer().close();
      }
      delete pSocket;
      pSocket = 0;
      ReqAlive = false;
      SetEvent(hEvent);
      IOService->stop();
      LobbySocketOpen = false;
      WorkerThreads.join_all();
      LockCode->Release();
      delete LockCode;
      LockCode = 0;
   }
   catch (std::exception& e)
   {
      stringstream ss;
      ss << "SSLSocket::Stop: threw an error - " << e.what() << ".\n";
      Log.LogString(ss.str(), LogError);
      Stop();
   }
}