开发者

Problem reading from boost basic_streambuf with an istream

I'm having some problems reading from a streambuf which is being filled through an asyc_read(). When stepping through my code in VS i can see that the correct data is in the buffer but when i go to read it with:

std::istream is = std::istream(&buffer_);

unsigned short type;
unsigned short size;

is >> type;
is >> size;

The type and size variables remain at their initialized values. No errors or anything are thrown. I'm really stumped as to why this is the case i've seen similar code which reads data off into variables in the exact same way

EDIT: so here's my async_read code which then calls the above code:

boost::asio::async_read(socket_,
    buffer_,
    boost::asio::transfer_at_least(4),
    boost::bind(&Session::handleReadBody, this,
    boost::asio::placeholders::error,
    boost::开发者_如何学编程asio::placeholders::bytes_transferred));


If type remains in its initialized value, it's apparent that is >> type; failed: check the status of the stream (if(is) {...}) to be sure.

And, seeing as you have a transfer_at_least(4), I suspect that you're transferring binary data, not whitespace-separated strings of ASCII characters. If that is the case, use read():

int16_t type, size;
data.read(reinterpret_cast<char*>(&type), sizeof type);
data.read(reinterpret_cast<char*>(&size), sizeof size);

but pay attention to byte order.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜