"two-step" async_read with boost asio

Marius Herzog

I want to implement a protocol on top of the TCP/IP stack using boost asio. The length of a protocol - PDU is contained in its first 6 bytes. Now using the synchronous read methods provided by asio, I can read exactly the first 6 bytes, calculate the length n and then read exactly n bytes to get the whole PDU.

I would rather use the asynchronous methods, though, but studying the example in the asio documentation leaves me with a question. The author uses the socket member function async_read_some(), which reads an (seemingly for me) indeterminate amount of data from the socket. How would I apply my "two-step" procedure described in the first paragraph to receive the complete PDU? Or is there another advisable solution for my problem?


Mike Seymour

Use the non-member functions async_read to read a fixed amount.

For example, using a std::vector or similar for a buffer:

// read the header
buffer.resize(6);
async_read(socket, boost::asio::buffer(buffer),
    [=](const boost::system::error_code & error, size_t bytes){
        if (!error) {
            assert(bytes == 6);

            // read the payload
            buffer.resize(read_size(buffer));
            async_read(socket, boost::asio::buffer(buffer),
                [=](const boost::system::error_code & error, size_t bytes){
                     if (!error) {
                          // process the payload
                     }
                });
        }
    });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

"two-step" async_read with boost asio

From Dev

Boost asio async_read_until followed by async_read

From Dev

boost::asio::async_read 100% CPU usage on simple example

From Dev

boost::asio::async_read return end of file error on newline

From Dev

boost::asio::async_read return end of file error on newline

From Dev

asio : multiple pending async_read?

From Dev

Boost ASIO - What is async

From Dev

Boost asio trouble with async_read_until

From Dev

Boost::Asio Async write failed

From Dev

What is the right pattern to use with asio::async_read?

From Dev

boost::asio::read() blocks forever

From Dev

Boost asio bind read handler

From Dev

boost::asio read/write trouble

From Dev

Boost asio asynchronous read and then write

From Dev

EOF in boost::async_read with thread_pull and boost 1.54

From Dev

Concurrent read and async_read_some in boost asio

From Dev

Boost asio async_read_some returning small amount of data

From Dev

Boost ASIO, async_read_some callback not called

From Dev

Boost::asio::async_read_until never calls handler

From Dev

Boost asio TCP async server not async?

From Dev

Boost::Asio - read or read_until?

From Dev

Boost::Asio - read or read_until?

From Dev

boost asio async udp server - poor performance

From Dev

Generic way to timeout async operations in boost::asio

From Dev

Boost.Asio: Async operations timeout

From Dev

Boost asio async operation bad file descriptor

From Dev

boost asio async udp server - poor performance

From Dev

Boost.Asio: Async operations timeout

From Dev

Callback passed to boost::asio::async_read_some never invoked in usage where boost::asio::read_some returns data

Related Related

HotTag

Archive