c++ - 替代上一版 Boost asio 库中缺少的方法

标签 c++ boost boost-asio

几年前,我使用 Boost asio 库编写了一个电子邮件客户端。

有一个抽象类 ICON 有四个子类。

POP3conN 到平面 POP3 通信

POP3conS 以保护 POP3 通信

SMTPconN 到平面 SMTP 通信

SMTPconS 以保护 SMTP 通信

ICON有一个成员

boost::asio::ip::tcp::socket socket_

和两个虚拟过程,在每个子类中定义:

void SMTPconN::run() { socket_.get_io_service().run(); }
void SMTPconN::reset() { socket_.get_io_service().reset(); }

该应用程序在 boost_1_63_0 上运行良好。但是当我尝试更新到 boost_1_70_0 时,编译器 (MS V Studio 2015) 在两个定义中都出现了问题:

class "boost::asio::ssl::stream<boost::asio::ip::tcp::socket>" has no member "get_io_service".

因为我想对大量代码和复杂的逻辑做最小的改动:是否有一些解决方法可以解决这个遗漏的方法?

最佳答案

Networking TS compatibility 下的文档状态您可以使用 get_context().context(),这将为您提供一个 io_context 实例(它取代了 boost 1.64/1.65 附近的某个地方的 io_service IIRC)。

Networking TS compatibility

Boost.Asio now provides the interfaces and functionality specified by the "C++ Extensions for Networking" Technical Specification. In addition to access via the usual Boost.Asio header files, this functionality may be accessed through special headers that correspond to the header files defined in the TS. These are listed in the table below:

[...]

Use get_executor().context() to obtain the associated io_context.

get_io_service()get_io_context() 以前都用于 boost 移植,但同时它们也已被弃用和废弃。

附言:另见 Get boost::asio::io_context from a boost::asio::ip::tcp::socket to exec a custom function这与您的问题非常相似,但指定了一个特定的用例。

那里的评论针对该用例提供了明显更好的解决方案:

socket.get_io_service().post([](){ /* my custom code */ } );

成为

post(socket.executor(), [](){ /* my custom code */ } );

关于c++ - 替代上一版 Boost asio 库中缺少的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56225374/

相关文章:

c++ - 在静态编译和链接时保持共享库插件系统的结构

c++ - 您如何确定由 'std::map' 创建的用于 'boost::pool_allocator' 的节点的大小(以跨平台方式)?

c++ - 从 std::thread 调用 boost::asio::io_service::run

c++ - 我如何从 Boost.ASIO 获得 SSL*?

c++ - 即使使用 strand,async_writes 的顺序也不正确

c++ - __DATE__ 宏的不同格式

使用预处理器指令生成 C++ 类

c++ - GetModuleBaseName 不接受 char* 参数

c++ - 内置mpi的自定义boost python模块?

c++ - boost::asio 和套接字所有权