是否可以使用超时处理阻塞读取功能?

问题描述 投票:2回答:1

我正在为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);
        }
}

此功能关闭Web套接字

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授权 在阅读WS之前经过的时间:0 读完WS后经过的时间:0.3

{  
    "state": "listening"  
}

在前进之前经过的时间:0.3 去ReadSocket 在阅读WS之前经过的时间:0.3 阅读WS:2.1后经过的时间

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

在前进之前经过的时间:2.1 去ReadSocket 在阅读WS之前经过的时间:2.1 阅读WS:2.1后经过的时间

{
   "state": "listening"
}

在前进之前经过的时间:2.1 去ReadSocket 在阅读WS之前经过的时间:2.1

推送事件:FILE_WRITE_DONE

阅读WS:34后经过的时间

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

在前进之前经过的时间:34

// PROGRAM EXITS WITH -1

题:

2.1秒之后,程序再次进入ReadSocket,其中ws_.read(ReadBuffer_);阻止了几乎32秒的执行,直到它从套接字接收到某些东西,在这种情况下它接收“会话超时”。

如果此块开启5秒,我怎样才能移动到CloseSocket。也就是说,如果在任何时候我让ws_.read阻止我的代码超过5秒我想表达我的行为,比如说CloseSocket。

c++ boost websocket boost-asio boost-beast
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.