從this example開始,我想用async_read_until()
替換async_read()
。boost ASIO async_read_until不能用ASIO ssl客戶端編譯示例
所以我擡頭看了this example,看到async_read_until()
被調用。我看到它被稱爲如下:
boost::asio::async_read_until(socket_, response_, "\r\n",
boost::bind(&client::handle_read_status_line, this,
boost::asio::placeholders::error));
因此,我認爲相比於SSL客戶端的例子,唯一的區別是,response_
是boost::asio::streambuf
。所以,我創建了一個對象在我的類主體以同樣的方式,然後在SSL客戶端的例子,我代替回調函數:
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));
與其他功能,並刪除了bytes_transferred
部分。它變成:
boost::asio::async_read_until(socket_, response_, "\r\n",
boost::bind(&SSLClient::handle_read, this,
boost::asio::placeholders::error));
但是...這不會編譯。它給了我這些錯誤:
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.
我不得不這樣做,以獲得所有這些錯誤,也同樣說明切換從async_read
到async_read_until
。如果我回到async_read
,它編譯得很好。我究竟做錯了什麼?
如果您需要任何其他信息,請詢問。謝謝。
其實你讓我注意到我忘記了在回調函數中刪除'bytes_transferred'佔位符。我刪除它並編譯。老實說,這是回調函數中最糟糕的事情。他們的調試是一場噩夢,非常不明確。沒有清楚的跡象表明問題是什麼,只能猜測和想象......我希望有更好的方法來調試回調函數。謝謝。 –
好吧。說實話,我不配你咆哮。如果你有一個有能力的編譯器,只需使用lambdas。這裏甚至沒有Asio棧(它確實有回調概念斷言),它只是綁定barfing。你不需要綁定一個lambda。 _幫助你的編譯器幫助你_ – sehe
對不起,朋友,這不是一個咆哮。我非常感謝你的幫助。我只是在抱怨它是多麼煩人,因爲這是一個微不足道的問題,我不能注意到它,因爲來自編譯器的巨大報告是與實際問題相距甚遠的錯誤。這與你無關:-)感謝lambdas的建議,我會考慮它的。 –