本文主要是介绍关于 boost::asio::strand 初始化 socket、stream、resolver、deadline_timer 对象,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在 boost::asio 之中默认情况下,大家使用 io_context 来为这些对象初始化传递的执行者,但我需要这里说明。
对于 boost::asio 构造类似 socket 对象必须构造传递 io_context 是个伪命题,boost::asio 对象并非只允许传递 boost::asio::io_context【重点】。
它的构造是接收一个 executor 的,这个 executor 不是非必须是 io_context,这个大家必须要了解,但凡仔细看过文档应该都明白才对,但可惜的国内很多人并不愿意多仔细的看看文档,看着那些所谓大牛的 aiso 经验,贴出来的 demo 代码就算是过了。
这样不好。
在 boost::asio 之中,不同的 asio 对象封装所需要构造传递的执行器略微有些限制,但可以确定的是都不允许:
boost::asio::io_context::strand 传递进去执行,对于传递 strand 的正确做法为:
声明:
typedef boost::asio::io_context::executor_type executor_type;
typedef boost::asio::strand<executor_type> strand;
构造:
方法一:
boost::asio::io_context ioc;
strand(ioc.get_executor());
方法二:
boost::asio::io_context ioc;
boost::asio::make_strand(ioc)
把这个构造出来的 strand 传递给 socket、stream 对象的构造函数就可以了。
当然,上面提到了 asio 的构造初始化并非只是 io_context,只要符合 executor 就可以。
比如:
boost::asio::any_io_executor、boost::asio:::any_completion_executor 就没问题,取决于每个不同 asio 对象构造执行器的限制类型。
比如:
如果我们希望 socket 都运行在一个 strand 上面,按照那些所谓大牛,文档都看不仔细的情况下,大概率是让人们构造一个共享的 boost::asio::io_context::strand,然后在 socket 的异步行为回调函数上都 warp 一次。
但我不建议大家这么做,正确的做法是上述形式,构造 strand 的执行器并在构造的时候传递给 socket 即可,这也包括其它的 asio 对象,如 resolver、DNS解析对象。
本人在此处实现了一个封装,用来模拟串化 io_context。
/* https://www.boost.org/doc/libs/1_71_0/libs/beast/example/websocket/client/async/websocket_client_async.cpp */
/* https://fossies.org/linux/boost/libs/asio/test/strand.cpp */
namespace poost {namespace asio {class io_context final {public:typedef boost::asio::io_context::executor_type executor_type;typedef boost::asio::strand<executor_type> strand;typedef boost::asio::io_context::work work;public:io_context() noexcept : io_context(ppp::make_shared_object<boost::asio::io_context>()) {}io_context(const std::shared_ptr<boost::asio::io_context>& context) noexcept : context_(context) /* context_->get_executor() */, strand_(boost::asio::make_strand(*context)) {}public:strand& get_strand() const noexcept { return ppp::constantof(strand_); }boost::asio::io_context& get_context() const noexcept { return *context_; }std::shared_ptr<boost::asio::io_context> ptr_context() const noexcept { return context_;}executor_type get_executor() const noexcept { boost::asio::io_context& context = get_context();return context.get_executor(); }std::shared_ptr<ppp::Byte> ptr_buffers() const noexcept {return buffers_;}void ptr_buffers(const std::shared_ptr<ppp::Byte>& buffers) const noexcept {ppp::constantof(buffers_) = buffers;}public:template <typename LegacyCompletionHandler> void post(LegacyCompletionHandler&& handler) const noexcept { strand& strand = get_strand();boost::asio::post(strand, handler); }template <typename LegacyCompletionHandler> void dispatch(LegacyCompletionHandler&& handler) const noexcept { strand& strand = get_strand();boost::asio::dispatch(strand, handler); }public:void restart() noexcept { boost::asio::io_context& context = get_context();context.restart(); } void run() noexcept; void run(boost::system::error_code& ec) noexcept { boost::asio::io_context& context = get_context();context.run(ec); }void stop() noexcept { boost::asio::io_context& context = get_context();context.stop(); }bool stopped() noexcept { boost::asio::io_context& context = get_context();return context.stopped(); }private:mutable std::shared_ptr<boost::asio::io_context> context_;mutable strand strand_;mutable std::shared_ptr<ppp::Byte> buffers_;};inline void io_context::run() noexcept {boost::system::error_code ec; run(ec); boost::asio::detail::throw_error(ec);}}
}
如何在 socket 上面使用它?
std::shared_ptr<poost::asio::io_context> context;
boost::asio::ip::udp::socket socket_(context->get_strand());
是不是很直观跟简单,那么运行效果会是什么样子呢?
看看线程的调用堆栈是会重叠一层 strand 滴:(所以根本不需要额外的 strand 手动处理)
这篇关于关于 boost::asio::strand 初始化 socket、stream、resolver、deadline_timer 对象的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!