gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:39:00 31 4
gpt4 key购买 nike

我正在为 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com