Jump to content
  • Advertisement
Sign in to follow this  
whiterook6

Boost buffer and multiple buffers

This topic is 2998 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

Hi guys, I'm trying to marshall data into an asyncronous send call through Boost asio. I need to send two objects, one immediately after the other, so that it can be received on the other end in one call. I'm using TCP to keep things simple, though eventually I'll move to UDP because... well, you know.

Anyways, my solution at the moment is to make a char * buffer large enough for both, and do 2 memcpy()s. It's fragile and stupid. I recall reading somewhere that boost can buffer multiple buffers into one buffer. Can I use that?

What should I do?



For example, I have right now:
char *buffer=new char[sizeof(MessageHeader)+sizeof(ServerJoinOtherBody)];

memcpy(buffer,&header,sizeof(MessageHeader));

memcpy(buffer+sizeof(MessageHeader),b,sizeof(ServerJoinOtherBody));

boost::asio::async_send(socket, boost::buffer(buffer, sizeof(MessageHeader)+sizeof(ServerJoinOtherBody), asyncHandler);

Share this post


Link to post
Share on other sites
Advertisement
See Buffers and Scatter-Gather I/O.

Put regular buffers into one of containers, boost::array is simplest for known number of elements. Buffers are constructed regularly, so they can be plain array.

The underlying syscall will use scatter/gather, so there should be no inherent penalty for doing so, and might in theory be even slightly more efficient.

Share this post


Link to post
Share on other sites
There is no guarantee that the other end will receive the data in a single receive call anyway. It's TCP. It's a stream. Deal with it!

Share this post


Link to post
Share on other sites
If I use the scatter gather method described on that link (boost::array with boost::buffers) do I need to use an array with buffers to decode it? Or, in other words, will the TCP stream include extra data doing this?

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!