Alternative to missing method in last version of Boost asio library

假装没事ソ 提交于 2019-12-02 07:27:09

The docs state under Networking TS compatibility that you can use get_context().context(), which will get you a io_context instance (which replaced io_service somewhere around boost 1.64/1.65 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.

Both get_io_service() and get_io_context() were previously in place to facilitate porting, but they have in the mean time also been deprecated and obsoleted.

PS: Also see Get boost::asio::io_context from a boost::asio::ip::tcp::socket to exec a custom function which is eerily similar to your question but specifies a specific use-case.

The comments there have the decidedly better solution for that use-case:

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

Becomes

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

The subclasses: POP3conN and SMTPconN have a member:

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

Similarly, POP3conS and SMTPconS have a member:

boost::asio::ssl::stream<boost::asio::ip::tcp::socket>  socket_;

The first argument of all constructors is a pointer to io_service. Some like:

IPCON::IPCON(boost::asio::io_service* ioserv_, ...) { ... }

POP3conN::POP3conN(boost::asio::io_service* ioserv_, ....) {...}

First change: in the abstract class IPCON has been added a new member:

boost::asio::io_context* iocontPtr_;

wich is initialized in the constructor replacing the old reference to io_service:

IPCON::IPCON(boost::asio::io_context* iocont_, ...) { ... }

In the constructors of the subclasses has been added initialization to such member:

POP3conN::POP3conN(boost::asio::io_context* iocont, ....) : IPCON(iocont) { ... }

Second change: all occurences of

boost::asio::io_service

Can be replaced by

boost::asio::io_context

The problematic expressions

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

appears now like this:

void SMTPconN::run() { iocontPtr->run(); }
void SMTPconN::reset() { iocontPtr->reset(); }

It seems that the functionality of the old io_service has been replaced by the new io_context.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!