Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically sized boost::asio::buffer

I'm reading from a boost::asio::ip::udp::socket like this:

using boost::asio::ip::udp;

// ...

char recv_buf[128];
udp::endpoint sender_endpoint;
size_t len = socket.receive_from(boost::asio::buffer(recv_buf), sender_endpoint);

Now, this works perfectly fine, but the maximum amount of characters that I am able to recieve is now 127. However I am facing a problem because I need to accept some data input of which the length can greatly vary (and is not of well-defined length with prefixed headers, for example). A solution to this would be a dynamically expanding buffer, like a vector. Is it possible to create a dynamically expanding boost::asio::buffer to accept (theoretical) infite amounts of input and store it in a container?

like image 245
orlp Avatar asked May 08 '11 22:05

orlp


2 Answers

Boost 1.66.0 added dynamic_buffer which can adapt a reference to std::string or std::vector<CharType>:

  • http://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/reference/dynamic_buffer.html
like image 59
sehe Avatar answered Oct 03 '22 17:10

sehe


If you use smaller buffers, you can easily chain them together via the *BufferSequences concepts. For example, you can pass in a MutableBufferSequence to accept data from a read(2) call or pass a ConstBufferSequence for a list of buffers that you are going to write(2) out. That said, I tend to recommend using a single buffer in each direction because it tends to simplify code (though that's not always possible).

like image 24
Sean Avatar answered Oct 03 '22 16:10

Sean