Jump to content
  • Advertisement
Sign in to follow this  
beebs1

Boost asio streambuf?

This topic is 2502 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hiya,

I'm currently using asio to read from a socket into a boost::array, and then parsing the fields out. It works but it's a little ugly, and it's prone to breaking.

I've read about using asio::streambuf instead, which seems to be exactly what i need - receive the data, use an istream to pull out the fields and go.

The documentation isn't completely clear to me though - am I expected to read into a single streambuf for each socket, and copy each 'message' out into another array to pass around as needed? Or is it OK to read into a new streambuf for each message, and pass the streambuf itself around inside my application?

Many thanks for any advice.

Cheers, James

Share this post


Link to post
Share on other sites
Advertisement
You can create new streambufs for each call to read. The main thing to watch out for is that you may be reading more than you need for just your packet -- there may be data left in the streambuf that's the start of the next packet (at least if you're using TCP -- with UDP, you get exactly one datagram per streambuf)

Share this post


Link to post
Share on other sites
I'm having a little trouble with this.

Each message comprises it's size, followed by the data. In the async_read() handler, I'm parsing out the message size and posting another read if the last message's data isn't all there, using transfer_at_least().

When the handler is called again with the rest of the data, I then have no way of knowing the size of the incomplete message, as the size field was removed from the stream by the previous handler.

Is there a way of reading bytes from a streambuf without removing them, so I can leave the size field intact in the buffer?

Any help appreciated smile.png

Share this post


Link to post
Share on other sites
Note that transfer_at_least may transfer more than you request.

The best way to do this is to just use ASIO to snarf data from the network into a big cyclic buffer (perhaps virtual?) and then parse the length and packets out of that. All ASIO needs to do is try to fill the buffer with data. In the callback for "I got more data," you can check how many complete packets there are in the buffer at the time of the callback, dequeue any packets that are complete and dispatched, and then try to re-fill the buffer with more data.

There is a buffer class in ASIO that may be better than the streambuf class.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!