websocketpp asio听错误

时间:2017-08-06 19:40:13

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

我有一个多线程的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错误是什么意思?我希望有人之前见过这个,以便我可以开始排除故障。谢谢!

编辑: 我正在调整stock 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()

1 个答案:

答案 0 :(得分:0)

每当有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中,虽然它仍然抛出异常,因为它没有终止程序,它最终会关闭处理程序。

另外,也许在调用m_server.pause_reading(it->first)之前尝试close()来冻结该处理程序的活动。

经过第二次观察,我认为你在m_server.listen(...)听的地方会抛出你得到的例外。尝试用try / catch包围它并输入自定义日志消息。