Drop data using boost :: asio

I use boost :: asio in asynchronous mode and I would like to skip / throw / delete the message that was sent to me via TCP. I want to do this because I have already read the message header and I know that this does not interest me. The message may be large, so I would prefer not to allocate space for it, and even better not to transfer it to user space in general.

I see boost :: asio :: null_buffers, but it is not applicable here (see https://svn.boost.org/trac/boost/ticket/3627 ).

+6
source share
2 answers

As far as I know, the BSD socket interface does not give you this function. You always need to read the buffer. Now, what you can do to not allocate a huge buffer is to read into a smaller buffer in a loop. Something like that:

void skip_impl(tcp::socket& s, int n, boost::function<void(error_code const&)> h , char* buf, error_code const& ec, std::size_t bytes_transferred) { assert(bytes_transferred <= n); n -= bytes_transferred; if (ec || n == 0) { delete[] buf; h(ec); return; } s.async_read_some(boost::asio::buffer(temp, std::min(4096, n)) , boost::bind(&skip_impl, boost::ref(s), n, h, temp, _1, _2)); } void async_skip_bytes(tcp::socket& s, int n, boost::function<void(error_code const&)> h) { char* temp = new char[4096]; s.async_read_some(boost::asio::buffer(temp, std::min(4096, n)) , boost::bind(&skip_impl, boost::ref(s), n, h, temp, _1, _2)); } 

This was not passed through the compiler, so there might be silly typos, but this should illustrate the point.

+6
source

Boost Asio is a library that I haven't used yet. Therefore, I do not know if only anything can be connected with the Sink interface. But if so, then null_sink forcing works well enough for your situation ...?

http://www.boost.org/doc/libs/1_46_1/libs/iostreams/doc/classes/null.html

(He would just throw away the data, so that you are still in user space. I would be surprised if there was a way to do otherwise, but it would be great if you could.)

0
source

Source: https://habr.com/ru/post/895707/


All Articles