是否可以处理超时的阻塞读取功能?
Is it possible to handle a blocking read function with timeout?
我正在研究用于客户端和服务器之间异步通信的增强 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());
) 其余部分是打印在程序中不同位置的 cout。经过的时间以秒为单位
读取 WS 之前经过的时间:0
读取 WS 后经过的时间:0.3继续之前经过的时间 : 0.3{ "state": "listening" }
转到 ReadSocket读取 WS 之前经过的时间 :0.3
读取 WS 后经过的时间 : 2.1继续之前经过的时间 : 2.1{ "results": [ { "alternatives": [ { "confidence": 0.89, "transcript": "several tornadoes touch down as a line of severe some " } ], "final": true } ], "result_index": 0 }
转到 ReadSocket读取 WS 之前经过的时间 : 2.1
读取 WS 后经过的时间 : 2.1继续之前经过的时间 : 2.1{ "state": "listening" }
转到 ReadSocket
读取 WS 之前经过的时间 : 2.1推送事件:FILE_WRITE_DONE
读取 WS 后经过的时间 : 34
{ "error": "Session timed out." }
前进前经过的时间 : 34
程序以 -1 退出
问题:
2.1
秒后,程序再次进入 ReadSocket,ws_.read(ReadBuffer_);
阻止执行近 32 秒,直到它从套接字接收到内容,在这种情况下,它会收到"会话超时"。
当此块打开 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++读取功能改进,使读取多个文件
- 如何继续读取不同功能中的文件,而不是从头开始再次读取文件?
- 读取、写入和as_bytes功能
- C++从键盘读取整数的功能
- 如何使用 recv 或 C++ 中的读取功能从 tcp 套接字读取大请求
- 此读取功能有什么问题
- 功能"fread()"不读取特定文件类型?
- 从文件到字符串读取并传递到功能
- 功能.exe:0xc0000005:访问违规读取地点0x0000000000000000
- 使用运算符读取文件和使用读取功能>>文件有什么区别?
- 是否可以处理超时的阻塞读取功能?
- 串口读取功能的通信超时和线程不超时
- 解析一般CSV读取功能时,如何处理不同的数据类型?(不明确指定它们)
- 当我设置readdata并读取功能以上载文件时,libcurl会在fwrite()上崩溃
- 我应该如何通过读取c 功能中的向量
- 读取功能失败时的成功消息
- 通过读取功能而不是fread读取标准输出
- 从文件读取功能(和"create" C++)
- FFMPEG自定义读取功能读取所有数据
- OpenCV读取功能无法读取图像