websocketpp asio listen error

websocketpp asio listen error

本文关键字:error listen asio websocketpp      更新时间:2023-10-16

我有一个多线程websocketpp服务器。当我退出程序并重新启动时,没有连接客户端,它可以正常工作。

但是,当客户端连接并且我退出/重新启动时,程序会引发此错误

[2017-08-06 15:36:05] [info] asio listen error: system:98 ()
terminate called after throwing an instance of 'websocketpp::exception'
what():  Underlying Transport Error
Aborted

我相信我有一个正确的断开连接序列,并且在启动退出序列时出现以下消息(我自己的调试信息(

[2017-08-06 15:35:55] [control] Control frame received with opcode 8
on_close
[2017-08-06 15:35:55] [disconnect] Disconnect close local:[1000] remote:[1000]
Quitting :3
Waiting for thread

asio 错误是什么意思?我希望以前有人看过这个,以便我可以开始故障排除。谢谢!

编辑: 我正在调整股票broadcast_server示例,其中

typedef std::map<connection_hdl, connection_data, std::owner_less<connection_hdl> > con_list;
con_list m_connections;

用于关闭连接的代码。

lock_guard<mutex> guard(m_connection_lock);
std::cout << "Closing Server" << std::endl;
con_list::iterator it;
for (it = m_connections.begin(); it != m_connections.end(); ++it)
{
m_server.close(it->first, websocketpp::close::status::normal, "", ec);
if (ec)
{
std::cout << "> Error initiating client close: " << ec.message() << std::endl;
}
m_connections.erase(it->first);
}

同样在broadcast_server类的析构函数中,我有一个m_server.stop()

每当有websocketpp::exception时,我首先检查我显式使用端点的任何地方,在您的情况下m_server

例如,它可能在您呼叫m_server.send(...)的地方。由于您是多线程,因此很可能其中一个线程正在尝试利用connection_hdl,而它已被另一个线程关闭。

在这种情况下,它通常是一个websocketpp::exception invalid state.我不确定Underlying Transport Error.

您可以使用断点来发现罪魁祸首(或者将一堆cout序列放入不同的方法中,并在抛出异常之前查看哪个序列被破坏(,或使用 try/catch:

try {
m_server.send(hdl, ...);
// or
m_server.close(hdl, ...);
// or really anything you're trying to do using `m_server`.
} catch (const websocketpp::exception &e) {//by safety, I just go with `const std::exception` so that it grabs any potential exceptions out there.
std::cout << "Exception in method foo() because: " << e.what() /* log the cause of the exception */ << std::endl;
}

否则,我注意到当您尝试关闭connection_hdl时,它有时会引发异常,即使似乎没有其他线程访问它。但是,如果您将其放在 try/catch 中,尽管它仍然会引发异常,但由于它不会终止程序,它最终会关闭处理程序。

此外,在调用close()以冻结来自该处理程序的活动之前,请尝试m_server.pause_reading(it->first)


再看一眼后,我认为你得到的例外是你用m_server.listen(...)听的地方抛出的。尝试用 try/catch 包围它并放置自定义日志记录消息。

相关文章: