【问题标题】:boost SSL async_shutdown completion handler not called未调用 boost SSL async_shutdown 完成处理程序
【发布时间】:2013-08-22 06:25:17
【问题描述】:

我正在使用带有 OpenSSL 1.0.1e 的 boost 1.54.0。

不时关闭 SSL 连接对象时,我看到 async_shutdown() 的完成处理程序没有被调用。

调试后我发现,当有 outstaing async_write() 时会发生这种情况。

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

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

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

编辑如果我一直在使用 async_ API,我可以调用shutdown() 还是必须调用async_shutdown()

【问题讨论】:

    标签: c++ boost boost-asio


    【解决方案1】:

    这就是我处理关机的方式。在此之前,我遇到了关闭问题。这段代码干净利落地关闭了一切:

    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();
       }
    }
    

    【讨论】:

    • 问题出在哪里,您的代码中究竟是什么解决了它?是 pSocket->next_layer().cancel() 吗?
    • 这是前段时间,但我相信当我调用取消方法时问题就消失了。我也遇到了多线程问题,这就是我单线程代码的原因。
    猜你喜欢
    • 2021-03-18
    • 2014-08-16
    • 1970-01-01
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多