提升 asio 绑定读取处理程序

Boost asio bind read handler

本文关键字:处理 程序 读取 绑定 asio 提升      更新时间:2023-10-16

我正在尝试绑定读取处理程序,而提升 asio 让我发疯。

我有以下课程

class namenode_registration: 
        public  boost::enable_shared_from_this<namenode_registration>,
        private boost::noncopyable {
private:
     [...]
public:
    namenode_registration(....);
    [...]
    void handle_request_sent(const boost::system::error_code& e);
    void handle_read_reply_type(boost::asio::mutable_buffers_1& buffer,
        const boost::system::error_code& e, std::size_t bytes_transferred);
};

通过以下实现:

void namenode_registration::handle_request_sent(
        const boost::system::error_code& e) {
    if (!e) {
        boost::asio::async_read(socket_,
                boost::asio::buffer(buffer_data_, namenode::reply::type_size),
                boost::bind(&namenode_registration::handle_read_reply_type,
                        shared_from_this(), boost::asio::placeholders::error,
                        boost::asio::placeholders::bytes_transferred));
    }
}

而且我真的不明白为什么它不编译:

In file included from /usr/include/boost/bind.hpp:22:0,
                from /home/.../datanode/namenode_registration.cpp:9:
/usr/include/boost/bind/bind.hpp:
    In instantiation of ‘boost::_bi::result_traits<boost::_bi::unspecified,
    void (datanode::namenode_registration::*)(boost::asio::mutable_buffers_1&,
    const boost::system::error_code&, long unsigned int)>’:
/usr/include/boost/bind/bind_template.hpp:15:48:
    instantiated from ‘boost::_bi::bind_t<boost::_bi::unspecified, void (datanode::namenode_registration::*
    (boost::asio::mutable_buffers_1&, const boost::system::error_code&, long unsigned int),
    boost::_bi::list3<boost::_bi::value<boost::shared_ptr<datanode::namenode_registration> >,
    boost::arg<1> (*)(), boost::arg<2> (*)()> >’
/home/.../datanode/namenode_registration.cpp:50:51:
    instantiated from here
/usr/include/boost/bind/bind.hpp:69:37:
    erreur: ‘void (datanode::namenode_registration::*)(boost::asio::mutable_buffers_1&,
    const boost::system::error_code&, long unsigned int)’ is not a class, struct, or union type

您尚未为此函数绑定足够的参数:

void handle_read_reply_type(boost::asio::mutable_buffers_1& buffer,
    const boost::system::error_code& e, std::size_t bytes_transferred);

您缺少缓冲区参数:

            boost::bind(&namenode_registration::handle_read_reply_type,
                    shared_from_this(), boost::asio::placeholders::error,
                    boost::asio::placeholders::bytes_transferred));