c++ - 是否可以处理超时的阻塞读取功能?

标签 c++ boost websocket boost-asio boost-beast

我正在为 asynchronous communication between the client and server 开发 boost websockets| .

现在我在程序中使用 boost::timer::auto_cpu_timer 打印耗时 .它以秒为单位显示耗时。

我的程序片段如下:

此函数将数据发送到 websocket:

void WebSocketSession::WriteSocket(beast::error_code ec) {
    if (ec)
         return fail(ec, "ssl_handshake");
    cout << "Authorised" <<endl;
    
    //Start the timer
    BoostTimer.start();
    
    // Send the Data message
    ws_.async_write(net::buffer(DATAMSG),
            bind(&WebSocketSession::ReadSocket, shared_from_this(), placeholders::_1,placeholders::_2));
}

此函数读取 websocket 响应

void WebSocketSession::ReadSocket(beast::error_code ec, size_t bytes_transferred) {
    boost::ignore_unused(bytes_transferred);
    if (ec)
         return fail(ec, "WriteSocket Failed");

    cout << "Time Elapsed before reading WS : " << TimeElapsed() << endl;

    try {
        ws_.read(ReadBuffer_);
    } catch (exception *e) {
        cerr << "Error: " << e->what() << endl;
    }

    cout << "Time Elapsed after reading WS : " << TimeElapsed() << endl;
    
    // Display the buffer into stringstream
    cout << beast::buffers(ReadBuffer_.data());
    
    // Clear the websocket buffer
    ReadBuffer_.consume(ReadBuffer_.size());

    cout << "Time Elapsed before moving ahead : " << TimeElapsed() << endl;

    // Decision tree

    // IsGstFileWriteDone() gives "true" when the file is written... that file is not related to this context. An event is pushed saying FILE_WRITE_DONE
        if (mIbmWatsonobj->IsGstFileWriteDone()){
            cout<< "Going to CloseSocket" << endl;
            WebSocketSession::CloseSocket(ec, 0);
        }else{
            cout<< "Going to ReadSocket" << endl;
            WebSocketSession::ReadSocket(ec, 0);
        }
}

此函数关闭网络套接字

void WebSocketSession::CloseSocket(beast::error_code ec, size_t bytes_transferred) {
    boost::ignore_unused(bytes_transferred);
    if (ec)
        return fail(ec, "ReadSocket Failed");

    cout << "CLOSING" <<endl;
    
    // Close the WebSocket connection
    ws_.close(websocket::close_code::normal);
}

这是我的程序输出的样子:从 websocket 收到的响应以灰色显示(cout << beast::buffers(ReadBuffer_.data()); 的输出)其余是在程序的不同位置打印的 couts。 耗时以秒为单位

IBM Authorised
Time Elapsed before reading WS : 0
Time Elapsed after reading WS : 0.3

{  
    "state": "listening"  
}

Time Elapsed before moving ahead : 0.3
Going to ReadSocket
Time Elapsed before reading WS : 0.3
Time Elapsed after reading WS : 2.1

{
   "results": [
      {
         "alternatives": [
            {
               "confidence": 0.89, 
               "transcript": "several tornadoes touch down as a line of severe some "
            }
         ], 
         "final": true
      }
   ], 
   "result_index": 0
}

Time Elapsed before moving ahead : 2.1
Going to ReadSocket
Time Elapsed before reading WS : 2.1
Time Elapsed after reading WS : 2.1

{
   "state": "listening"
}

Time Elapsed before moving ahead : 2.1
Going to ReadSocket
Time Elapsed before reading WS : 2.1

Event pushed : FILE_WRITE_DONE

Time Elapsed after reading WS : 34

{
   "error": "Session timed out."
}

Time Elapsed before moving ahead : 34

//PROGRAM EXITS WITH -1

问题:

2.1 之后秒后程序再次转到 ReadSocket,其中 ws_.read(ReadBuffer_);阻止执行将近 32 秒,直到它从套接字收到一些东西,在这种情况下它收到“ session 超时”。

当这个 block 打开 5 秒时,我如何移动到 CloseSocket。也就是说,如果在任何时候我有 ws_.read阻止我的代码超过 5 秒我想放置我的行为,比如 CloseSocket。

最佳答案

或者您可以只使用 1.70.0 中的新 Boost.Beast,它支持 websocket 操作的内置超时:https://www.boost.org/doc/libs/1_70_0/libs/beast/doc/html/beast/using_websocket/timeouts.html

关于c++ - 是否可以处理超时的阻塞读取功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55649130/

相关文章:

php - 如何让 Ratchet WebSocket 保持事件状态?

c++ - WritePrivateProfileString - 不可预测

c++ - 在 C++ 中用字符串连接 const char * 的最干净的方法是什么?

c++ - native c++/调试中的断言

C++:直接使用函数返回 shared_ptr 的值是个坏主意吗?

c++ - 如何在 Visual Studio 2012 中用 C++11 替换所有 BOOST_FOREACH?

c++ - 根据第一个选项选择选项组

angularjs - Firebase 网络事件

c++ - 从右值对象返回左值引用的成员函数

python-3.x - 目前可用于在 python3 上使用 flask 的 websockets 的最佳选择