简体   繁体   English

C ++关闭Boost ASIO SSL套接字的最佳方法是什么?

[英]C++ What is the best way to shutdown a Boost ASIO SSL socket?

I have a client application that uses Boost ASIO SSL sockets. 我有一个使用Boost ASIO SSL套接字的客户端应用程序。 I am running into connection errors that leave the socket in an unusable state until I restart the app. 我遇到连接错误,导致套接字无法使用,直到重新启动应用程序为止。 To begin, I make a connection request with this code: 首先,我用以下代码发出连接请求:

void apiClient::connect(boost::asio::ip::tcp::resolver::iterator endpoint_list)
{
    boost::asio::ip::tcp::resolver::iterator endpoint_iter = endpoint_list;
    boost::asio::ip::tcp::endpoint endpoint = *endpoint_iter;

    if (!ssl_socket_->lowest_layer().is_open()) {
        ssl_socket_->lowest_layer().async_connect(endpoint,
            boost::bind(&apiClient::handle_connect, this,
            boost::asio::placeholders::error, ++endpoint_iter));
    }
}

Periodically I encounter an error returned to the connection handler, the error in ec.message is [A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond] Error code [10060] 定期我遇到返回到连接处理程序的错误,ec.message中的错误是[连接尝试失败,因为被连接方在一段时间后未正确响应,或者建立的连接失败,因为连接的主机未能响应]错误码[10060]

Here is my connection handler: 这是我的连接处理程序:

void apiClient::handle_connect(const boost::system::error_code& ec, 
    boost::asio::ip::tcp::resolver::iterator endpoint_list)
{
    bool found_error(false);

    if (!ec) {
        ssl_socket_->async_handshake(boost::asio::ssl::stream_base::client,
            boost::bind(&apiClient::send_request, this, 
            boost::asio::placeholders::error));
    }
    else if (endpoint_list != boost::asio::ip::tcp::resolver::iterator()) {
        std::wstring sMsg = boost::str(boost::wformat(L"API server connection failure to host [%s]. Trying next endpoint") % s2ws(endpoint_list->host_name()));
        LogMsg(sMsg);
        found_error = true;
        epList = endpoint_list;
    }
    else {
        std::wstring sMsg = boost::str(boost::wformat(L"API server connection failure.  Error msg [%s] Error code [%d]") % s2ws(ec.message()) % ec.value());
        LogMsg(sMsg);
        found_error = true;
        epList = start_endpoint_list;
    }

    if (found_error) {
        requeue_request(SHUTDOWN_CONN);
    }
}

In my requeue_request function I check the number of errors I have encountered for the request and if it exceeds a threshold, I discard the request and move on. 在我的requeue_request函数中,我检查该请求遇到的错误数,如果超过阈值,则放弃该请求并继续。 If I have not exceeded the threshold, I shutdown the socket connection before attempting to reconnect and send the message again. 如果未超过阈值,则在尝试重新连接并再次发送消息之前,请关闭套接字连接。 My understanding is that the emplace method recreates the socket instance. 我的理解是emplace方法会重新创建套接字实例。

void apiClient::reconnect(SHUTDOWN_CONN_ACTION action)
{
    if (action == SHUTDOWN_CONN) {
        shutdownSocket();
    }

    ssl_socket_.emplace(*io_context_, *ctx_);

    connect_timer.expires_from_now(boost::posix_time::milliseconds(3000));
    connect_timer.async_wait(boost::bind(&apiClient::handleConnectTimer, this, boost::asio::placeholders::error));
}

void CNXGHotKeyAPIClient::shutdownSocket()
{
    boost::system::error_code ec;

    ssl_socket_->next_layer().cancel(ec);
    ssl_socket_->shutdown(ec);
    ssl_socket_->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
    ssl_socket_->lowest_layer().close(ec);
}

This design is not working for me. 此设计不适用于我。 When I encounter any connection error, my application retries the connection 3 times, it fails with the same error all three times and the socket is left in a state where I cannot use it until I restart my application. 当我遇到任何连接错误时,我的应用程序将重试该连接3次,该连接因所有相同的错误而全部失败3次,并且套接字处于无法使用它的状态,直到重新启动应用程序。 The server I'm connecting to is honoring hundreds of connection requests from python apps that do not use Boost so I don't think the server is at fault. 我正在连接的服务器正在接受来自不使用Boost的python应用程序的数百个连接请求,因此我认为服务器没有故障。

I have tried to find examples that use async_shutdown but I cannot find anything that helps me. 我试图找到使用async_shutdown的示例,但是找不到任何可以帮助我的东西。 The example at this link What is the proper way to securely disconnect an asio SSL socket? 此链接上的示例安全断开asio SSL套接字的正确方法是什么?

uses the following code which I am unable to make work. 使用下面的代码,我无法工作。 example code: 示例代码:

boost::system::error_code ec;
ssl_socket.cancel(ec);
ssl_socket.async_shutdown([](...) { ssl_socket.close(); };

ssl_socket.cancel() doesn't even exist in my version of Boost (v1.66) the closest thing I can find is ssl_socket.next_layer().cancel(ec) ssl_socket.cancel()甚至在我的Boost(v1.66)版本中都不存在,我能找到的最接近的东西是ssl_socket.next_layer()。cancel(ec)

My question is; 我的问题是; how do you shutdown a socket after it has encountered an error so that it is not left in an unusable state? 遇到错误后如何关闭套接字,以使其不处于不可用状态? When my application encounters connection errors, many times I cannot reconnect until I restart the app. 当我的应用程序遇到连接错误时,很多时候我无法重新连接,直到重新启动应用程序。 The app is still running and servicing user actions so I know it has not crashed, but the socket is in a state that is unusable. 该应用程序仍在运行并为用户操作提供服务,因此我知道它没有崩溃,但是套接字处于无法使用的状态。 Thanks for any help 谢谢你的帮助

  1. You can't shutdown an SSL socket. 您无法关闭SSL套接字。 You have to close it. 您必须关闭它。
  2. You can't reconnect a socket after it has been connected, even if the connect attempt failed. 连接套接字后,即使连接尝试失败,也无法重新连接套接字。 You have to close it and create a new one. 您必须关闭它并创建一个新的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM