【发布时间】:2014-12-04 23:13:50
【问题描述】:
我正在尝试使用带有 boost::asio::async_read_until 的 c++11 lambda,如下所示:
void TCPSession::readData()
{
auto self(shared_from_this());
boost::asio::async_read_until(socket_, buffer_, "\r\n",
[this, self](const boost::system::error_code& ec, std::size_t length)
{
log()->debug("received %lu bytes && ec: %d", length, ec); //use boost::log for compile time checking
std::ostringstream ss;
ss << &buffer_;
log()->debug("Received data: %s",ss.str());
if (!ec){
log()->debug("We never get here!");
[... process data ...]
readData();
}
}
);
}
我观察到的是长度=0。 ec 被适当地设置为 boost EOF 代码。
当我将成员(即 buffer_)转换为字符串时,我也会看到数据。
任何有关初始化长度的建议都会有所帮助。
示例输出:
2014-10-09 14:47:56.968142 [tcp-session] debug: received 0 bytes && ec: asio.misc:2
2014-10-09 14:47:56.974593 [tcp-session] debug: Received data: test7.2.3.5 0.18722307655 2014-10-09 14:47:55.343591
test7.2.3.5 -0.0691607464759 2014-10-09 14:47:55.343837
test7.2.3.5 0.151471040421 2014-10-09 14:47:55.344017
test7.2.3.5 0.172789025585 2014-10-09 14:47:55.344211
test7.2.3.5 0.0409326066787 2014-10-09 14:47:55.344406
【问题讨论】:
-
/你如何/如何观察你所说的话。可能是调试器让您/自己感到困惑
-
“未填充”是什么意思?您从
printf语句中看到了什么输出?此外,您似乎根本没有打印ec。你能把它也包括在你的输出中吗?
标签: c++ c++11 boost lambda boost-asio