【发布时间】:2012-01-21 02:16:00
【问题描述】:
我认为我的问题在这里很常见,但我只是看不出我在这里做错了什么。
我正在做一些 boost::asio 的工作并尝试编写一个模板化的异步读取函数。
这是一个函数。
template <typename Handler>
void AsyncRead(std::vector<boost::uint8_t>& _inMsg, Handler _handler)
{
#if debug
std::cout<< "IConnection::AsyncRead" << std::endl;
#endif
using namespace protocols;
typedef boost::tuple<Handler> tHandler;
typedef boost::function< void(const boost::system::error_code &, std::vector<boost::uint8_t> &, tHandler ) > HeaderReaderFunc;
//void (AConnection::*f)(const boost::system::error_code&, std::vector<boost::uint8_t>&, boost::tuple<Handler>) = &AConnection::HandleReadHeader<Handler>;
tHandler t(boost::make_tuple(_handler));
HeaderReaderFunc x(boost::bind(&AConnection::HandleReadHeader<Handler>, this, boost::asio::placeholders::error, boost::ref(_inMsg), t));
inboundHeader.resize(sizeof(SocketIO::MsgData));
boost::asio::async_read(socket, boost::asio::buffer(inboundHeader), x);
}
在这个函数的最后一个语句中事情开始变糟了。
boost::asio::async_read(socket, boost::asio::buffer(inboundHeader), x);
当我尝试将变量“x”作为参数传递给 async_read 函数时。
这些错误的长度和缺乏可解读的含义都是传奇。
只是一个错误输出的小例子:
boost_1_38_0/boost/asio/detail/bind_handler.hpp: In member function ‘void boost::asio::detail::binder2<Handler, Arg1, Arg2>::operator()() [with Handler = boost::function<void ()(const boost::system::error_code&, std::vector<unsigned char, std::allocator<unsigned char> >&, boost::tuples::tuple<boost::function<void ()(const boost::system::error_code&)>, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>)>, Arg1 = boost::system::error_code, Arg2 = unsigned int]’:
如果我在这里不使用 boost 函数,而是使用注释掉的行,它是对成员函数的引用,事情似乎可以正常工作,但我不知道为什么会这样。
AConnection::HandleReadHeader 函数模板的函数签名是:
template <typename Handler>
void HandleReadHeader(const boost::system::error_code& _e,
std::vector<boost::uint8_t> & _inMsg,
boost::tuple<Handler> _handler)
处理程序的类型:
boost::function<void (const boost::system::error_code & ) >
函数 AsyncRead 和 HandleReadHeader 是我自己的类的成员 AConnection(可能不重要)。
我在创建 boost::function 的语法方面遗漏了一些东西 签名包含 boost::tuple 的对象,或 boost::asio::async_read 函数的第三个参数类型与我的不匹配 变量“x”。
任何帮助将不胜感激。 谢谢。
编辑:
以下代码确实有效,但使用成员函数引用而不是 boost::functon。
template <typename Handler>
void AsyncRead(std::vector<boost::uint8_t>& _inMsg, Handler _handler)
{
#if debug
std::cout<< "Connection::AsyncRead" << std::endl;
#endif
using namespace protocols;
// Issue a read operation to read exactly the number of bytes in a header.
void (Connection::*f)(
const boost::system::error_code&,
std::vector<boost::uint8_t>&, boost::tuple<Handler>)
= &Connection::HandleReadHeader<Handler>;
inboundHeader.resize(sizeof(simple::message_header));
boost::asio::async_read(socket, boost::asio::buffer(inboundHeader),
boost::bind(f,
this, boost::asio::placeholders::error, boost::ref(_inMsg),
boost::make_tuple(_handler)));
}
【问题讨论】:
标签: c++ linux boost-asio boost-function