This is often referred to as reactor-style operations.
These can be obtained by providing boost::asio::null_buffers to the asynchronous operations. Reactor-style operations provide a way to be informed when a read or write operation can be performed, and are useful for integrating with third party libraries, using shared memory pools, etc. The Boost.Asio documentation provides some information and the following example code:
ip::tcp::socket socket(my_io_service);
...
socket.non_blocking(true);
...
socket.async_read_some(null_buffers(), read_handler);
...
void read_handler(boost::system::error_code ec)
{
if (!ec)
{
std::vector<char> buf(socket.available());
socket.read_some(buffer(buf));
}
}
Boost.Asio also provides an official nonblocking example, illustrating how to integrate with libraries that want to perform the read and write operations directly on a socket.
Providing a zero-length buffer to operations will often result in a no-op, as the operation's completion condition will have been met without attempting to perform any I/O. Here is a complete example demonstrating the difference between the two:
#include <array>
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
// This example is not interested in the handlers, so provide a noop function
// that will be passed to bind to meet the handler concept requirements.
void noop() {}
void print_status(
const boost::system::error_code& error,
std::size_t bytes_transferred,
boost::asio::ip::tcp::socket& socket)
{
std::cout << "error: " << error.message() << "; "
"transferred: " << bytes_transferred << "; "
"available: " << socket.available() << std::endl;
}
int main()
{
using boost::asio::ip::tcp;
// Create all I/O objects.
boost::asio::io_service io_service;
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 0));
tcp::socket socket1(io_service);
tcp::socket socket2(io_service);
// Connect the sockets.
acceptor.async_accept(socket1, boost::bind(&noop));
socket2.async_connect(acceptor.local_endpoint(), boost::bind(&noop));
io_service.run();
io_service.reset();
std::array<char, 512> buffer;
// Reading into a zero-length buffer is a no-op and will be
// considered immediately completed.
socket1.async_receive(boost::asio::buffer(buffer, 0),
boost::bind(&print_status,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred,
boost::ref(socket1))
);
// Guarantee the handler runs.
assert(1 == io_service.poll());
io_service.reset();
// Start a reactor-style read operation by providing a null_buffer.
socket1.async_receive(boost::asio::null_buffers(),
boost::bind(&print_status,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred,
boost::ref(socket1))
);
// Guarantee that the handler did not run.
assert(0 == io_service.poll());
// Write to the socket so that data becomes available.
boost::asio::write(socket2, boost::asio::buffer("hello"));
assert(1 == io_service.poll());
}
Output:
error: Success; transferred: 0; available: 0
error: Success; transferred: 0; available: 6