简体   繁体   中英

boost ASIO async_read_until doesn't compile with ASIO ssl client example

Starting from this example , I wanted to replace async_read() with async_read_until() .

So I looked up this example , and saw how async_read_until() is called. And I saw that it's called as follows:

      boost::asio::async_read_until(socket_, response_, "\r\n",
      boost::bind(&client::handle_read_status_line, this,
        boost::asio::placeholders::error));

So the only difference I see compared to the SSL client example is that response_ is a boost::asio::streambuf . So I created that object in my class body the same way, and then in the SSL client example, I replace the callback function:

  boost::asio::async_read(socket_,
      boost::asio::buffer(reply_, bytes_transferred),
      boost::bind(&SSLClient::handle_read, this,
        boost::asio::placeholders::error,
        boost::asio::placeholders::bytes_transferred));

with the other function, and removed the bytes_transferred part. And it became:

    boost::asio::async_read_until(socket_, response_, "\r\n",
        boost::bind(&SSLClient::handle_read, this,
        boost::asio::placeholders::error));

But... this doesn't compile. It gives me these errors:

In file included from /usr/include/boost/bind.hpp:22:0,
                 from SSLClient.h:5,
                 from main.cpp:14:
/usr/include/boost/bind/bind.hpp: In instantiation of 'struct boost::_bi::result_traits<boost::_bi::unspecified, void (SSLClient::*)(const boost::system::error_code&, long unsigned int)>':
/usr/include/boost/bind/bind_template.hpp:15:48:   required from 'class boost::_bi::bind_t<boost::_bi::unspecified, void (SSLClient::*)(const boost::system::error_code&, long unsigned int), boost::_bi::list2<boost::_bi::value<SSLClient*>, boost::arg<1> (*)()> >'
SSLClient.h:99:51:   required from here
/usr/include/boost/bind/bind.hpp:69:37: error: 'void (SSLClient::*)(const boost::system::error_code&, long unsigned int)' is not a class, struct, or union type
     typedef typename F::result_type type;
                                     ^
In file included from main.cpp:14:0:
SSLClient.h:87:70: warning: unused parameter 'bytes_transferred' [-Wunused-parameter]
     void handle_write(const boost::system::error_code& error, size_t bytes_transferred)
                                                                      ^
In file included from /usr/include/boost/asio/impl/io_service.hpp:18:0,
                 from /usr/include/boost/asio/io_service.hpp:767,
                 from /usr/include/boost/asio/basic_io_object.hpp:19,
                 from /usr/include/boost/asio/basic_socket.hpp:20,
                 from /usr/include/boost/asio/basic_datagram_socket.hpp:20,
                 from /usr/include/boost/asio.hpp:21,
                 from SSLClient.h:6,
                 from main.cpp:14:
/usr/include/boost/asio/impl/read_until.hpp: In instantiation of 'typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type boost::asio::async_read_until(AsyncReadStream&, boost::asio::basic_streambuf<Allocator>&, const string&, ReadHandler&&) [with AsyncReadStream = boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> >; Allocator = std::allocator<char>; ReadHandler = boost::_bi::bind_t<boost::_bi::unspecified, void (SSLClient::*)(const boost::system::error_code&, long unsigned int), boost::_bi::list2<boost::_bi::value<SSLClient*>, boost::arg<1> (*)()> >; typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type = void; std::string = std::basic_string<char>]':
SSLClient.h:99:52:   required from here
/usr/include/boost/asio/impl/read_until.hpp:707:3: error: static assertion failed: ReadHandler type requirements not met
   BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
   ^
/usr/include/boost/asio/impl/write.hpp:615:3: error: no match for call to '(boost::_bi::bind_t<boost::_bi::unspecified, void (SSLClient::*)(const boost::system::error_code&, long unsigned int), boost::_bi::list2<boost::_bi::value<SSLClient*>, boost::arg<1> (*)()> >) (const boost::system::error_code&, const long unsigned int&)'
   BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
   ^
Makefile:472: recipe for target 'main.o' failed
make: *** [main.o] Error 1
13:14:05: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project GNOMEClientCmd (kit: Qt 5.4.1)
When executing step "Make"
13:14:05: Elapsed time: 00:04.

All I had to do to get all these errors, is just switch from async_read to async_read_until as explained. If I go back to async_read , it compiles fine. What am I doing wrong?

If you require any additional information, please ask. Thanks.

Q. What Am I doing wrong

You're ignoring the documentation.

The documentation will tell you that async_read-until takes a streambuf¹, for one, and ReadHandler requires the bytes_transferred placeholder still.


¹ just noticed you described that in the prose; It works a lot better to show the relevant code. Prefer SSCCE on SO

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