我有一个网络库,该库使用asio进行异步读写。我发现自己写的是很多愚蠢的样板代码:
asio::dispatch(m_strand, [&, cb]() {
m_buffer = write::startup(m_params);
write(m_buffer, [&, cb](std::error_code ec, std::size_t len) {
if (ec) {
sql_state state;
cb(ec, state);
return;
}
read([&](std::error_code ec, packet barray) {
if (ec) {
sql_state state;
cb(ec, state);
return;
}
auto packets = parse::split_into_packets(barray);
uint8_t auth_type{1};
state = fsm::authentication_request(packets.front(), auth_type);
if (state) {
cb(ec, std::move(state));
return;
}
});
});
});
有没有一种方法可以优雅地抽象出错误处理?