【问题标题】:boost::asio read handler type requirements not met未满足 boost::asio 读取处理程序类型要求
【发布时间】:2019-04-18 11:52:33
【问题描述】:

我正在尝试将读取标头处理程序实现为成员函数来处理传入数据。但是在编译过程中我被告知没有满足类型要求。

我检查了 boost 文档,函数签名似乎没问题。我看不出有什么不同。但是 boost 不接受处理程序是有效的。

.cpp 文件:

void tcpclient::read_data() {
    char buffer_[1];
    boost::asio::async_read(_socket, boost::asio::buffer(buffer_, HEADER_LEN),
                            std::bind(&tcpclient::handle_read_header, this,
                                      boost::asio::placeholders::error,
                                      boost::asio::placeholders::bytes_transferred));
}


void tcpclient::handle_read_header(const boost::system::error_code &error, std::size_t bytes_transferred) {
    if (!error) {
        logger::log_info("Read " + std::to_string(bytes_transferred) + " bytes.");
    } else {
        logger::log_error("Failed to read header");
        _socket.close();
    }
}

.h 文件:

void handle_read_header(const boost::system::error_code &error, std::size_t bytes_transferred);

    void read_data();

    boost::asio::ip::tcp::socket _socket;

我希望代码能够很好地接受处理程序签名,但是却提示我:

/usr/include/boost/asio/impl/read.hpp: In instantiation of ‘typename boost::asio::async_result<typename std::decay<WriteHandler>::type, void(boost::system::error_code, long unsigned int)>::return_type boost::asio::async_read(AsyncReadStream&, const MutableBufferSequence&, ReadHandler&&, typename std::enable_if<boost::asio::is_mutable_buffer_sequence<MutableBufferSequence>::value>::type*) [with AsyncReadStream = boost::asio::basic_stream_socket<boost::asio::ip::tcp>; MutableBufferSequence = boost::asio::mutable_buffers_1; ReadHandler = std::_Bind<void (tcpclient::*(tcpclient*, boost::arg<1> (*)(), boost::arg<2> (*)()))(const boost::system::error_code&, long unsigned int)>; typename boost::asio::async_result<typename std::decay<WriteHandler>::type, void(boost::system::error_code, long unsigned int)>::return_type = void; typename std::enable_if<boost::asio::is_mutable_buffer_sequence<MutableBufferSequence>::value>::type = void]’:
/home/void/Documents/Development/SocketTest/SocketTest/networking/tcpclient.cpp:55:84:   required from here
/usr/include/boost/asio/impl/read.hpp:446: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:430:3: error: no match for call to ‘(std::_Bind<void (tcpclient::*(tcpclient*, boost::arg<1> (*)(), boost::arg<2> (*)()))(const boost::system::error_code&, long unsigned int)>) (const boost::system::error_code&, const long unsigned int&)’
   BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

【问题讨论】:

    标签: c++ asynchronous boost handler asio


    【解决方案1】:

    使用boost::bindboost::asio::placeholders::..

    boost::bind(&tcpclient::handle_read_header, this,
                                      boost::asio::placeholders::error,
                                      boost::asio::placeholders::bytes_transferred));
    

    std::placeholders::_1/_2std::bind

    std::bind(&tcpclient::handle_read_header, this,
                                      std::placeholders::_1,
                                      std::placeholders::_2));
    

    【讨论】:

    • 感谢您的回复。我更愿意使用第一个建议的示例,因为它更具描述性。但是,当我尝试使用boost::bind 而不是std::bind 时,我被告知No member 'bind' in namespace boost。我错过了包含吗?
    • 标头boost/bind.hpp 缺失,需要添加。
    • 是否还有机会将读取缓冲区传递给处理程序?这样我就可以在处理程序本身中处理接收到的数据。
    • 是的,您可以使用 lambda 作为处理程序。然后你需要在 lambda 的捕获列表中传递 buffer。如果你愿意,我可以添加示例链接。
    • Here - example 我添加了 2 个版本:一个使用 lambda,另一个使用扩展 handle_read_header 成员 - 作为第三个参数,它需要缓冲区。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    • 2014-02-08
    相关资源
    最近更新 更多