简体   繁体   中英

boost asio tcp socket 1.70 not backward compatible

I am using boost-beast for my http server. I started writing the server based on the example provided on boost beast documentation (probably version 1.66). Now after boost 1.70 I got compilation issues. Everything was working perfectly even with 1.69. The problem was inside my session class.

template <typename RouterT>
class session{
    boost::asio::strand<boost::asio::io_context::executor_type> _strand;
    boost::asio::ip::tcp::socket _socket
    // ...
    explicit session(RouterT& router, boost::asio::ip::tcp::socket socket, std::shared_ptr<std::string const> const& doc_root)
        : _router(router), 
          _socket(std::move(socket)), 
          _strand(_socket.get_executor()), 
          _doc_root(doc_root), 
          _lambda(*this){

          }
};

I was getting the following error when upgraded to 1.71

error: no matching function for call to ‘boost::asio::strand<boost::asio::io_context::executor_type>::strand(boost::asio::basic_socket<boost::asio::ip::tcp, boost::asio::executor>::executor_type)’

However the definition of the strand has not changed much from 1.69 to 1.70. So that should not be the problem behind this error.

The boost::asio::ip::tcp::socket is typedef of basic_stream_socket<tcp> with the default executor . So that executor is not convertible to the boost::asio::io_context::executor_type that the strand requires.

So instead of using plain boost::asio::ip::tcp::socket I am now using

typedef boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::io_context::executor_type> socket_type;

What I am not sure is whether this modification work with the older version of boost < 1.70 because I don't have old boost installation. So is that change okay?

Is this the correct solution? Or I was supposed to take the tcp::socket 's executor to build the strand?

Boost.Asio introduced a breaking change. The Executor is now a template parameter for every I/O object. The default type is executor , which is the polymorphic wrapper. You can convert from a typed executor to the polymorphic wrapper, but not the other way (the cause of your compile error). Your fix is correct, but it may require some #ifdef if you want it to compile on older versions of Boost.Asio.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM