【发布时间】:2019-02-27 15:03:10
【问题描述】:
在我的网络库中,如果我手动 run() 和 restart() io_context,我可以对网络进行异步写入。
我现在正在尝试通过添加线程池来扩大规模:
.hpp
struct pool : public std::enable_shared_from_this<pool> {
pool(const pool &) = delete;
auto operator=(const pool &) -> pool & = delete;
explicit pool(pool_parameters config, db_parameters params) noexcept;
asio::io_context m_io_context;
asio::thread_pool m_workers;
asio::executor_work_guard<asio::io_context::executor_type> m_work_guard;
/// \brief Container to hold connections.
std::vector<std::unique_ptr<dbc::connection>> m_connections;
};
.cpp
pool::pool(pool_parameters config, db_parameters params) noexcept
: m_config{std::move(config)},
m_params{std::move(params)},
m_work_guard{asio::make_work_guard(m_io_context)},
m_workers{m_config.thread_pool_size} {
m_connections.reserve(m_config.connection_pool_size);
asio::post(m_workers, [&]() { m_io_context.run(); });
}
管理连接:
.hpp
struct abstract_connection : connection {
explicit abstract_connection(const std::shared_ptr<pool> &pool) noexcept;
~abstract_connection() override;
packet m_buffer;
asio::local::stream_protocol::endpoint m_endpoint;
asio::generic::stream_protocol::socket m_socket;
asio::io_context::strand m_strand;
};
.cpp
abstract_connection::abstract_connection(const std::shared_ptr<pool> &pool) noexcept
: m_params{pool->m_params},
m_config{pool->m_config},
m_endpoint{pool->m_config.socket},
m_socket{pool->m_io_context},
m_strand{pool->m_io_context} {
m_socket.connect(m_endpoint);
m_socket.non_blocking(true);
}
abstract_connection::~abstract_connection() {
std::error_code ec;
m_socket.shutdown(asio::generic::stream_protocol::socket::shutdown_both, ec);
m_socket.close();
}
现在是令人困惑的公园。在具体连接对象的 ctor 上,我需要进行握手,同时对同一类的析构函数进行握手。这不会发生,因为套接字对象的行为似乎很奇怪:
如果我异步发送数据,则不会向套接字写入任何内容,有时会出现管道损坏错误:
asio::dispatch(m_strand, [&]() {
m_buffer = write::startup(m_params);
asio::async_write(m_socket, asio::buffer(m_buffer), [](std::error_code ec, std::size_t len) {});
});
如果我进行同步写入,我会在从套接字读取之前得到一个损坏的管道错误:
std::error_code ec;
auto startup = write::startup(m_params);
asio::write(m_socket, asio::buffer(startup), ec);
if (set_error(ec)) {
std::cerr << " XXX " << ec.message() << std::endl;
return;
}
m_buffer.reserve(327);
asio::read(m_socket, asio::buffer(m_buffer), ec);
std::cerr << ec.message() << std::endl;
std::cerr << m_buffer.size() << std::endl;
连接是通过一个 unix 套接字完成的,我有 socat 坐在两者之间,所以我可以看到数据来来去去,以及损坏的管道消息。尝试使用第三方工具连接到远程是可行的,所有相关数据都出现在 socat 中,所以我相信问题出在我的代码中。
如何调试套接字的情况?
【问题讨论】:
-
很难从这样的部分代码中判断出了什么问题。但是,您发布的代码中存在一个可疑的
->shared_from_this()缺失,因此问题很可能是生命周期管理不善。将日志代码添加到 ctor/dtor 通常有助于跟踪这些类型的错误。
标签: c++ c++11 boost-asio