asio-grpc v3.1.0
Asynchronous gRPC with Asio/unified executors
Completion token

The last argument to all async functions in this library is a CompletionToken. It can be used to customize how to receive notification of the completion of the asynchronous operation. Some examples:

Callback

alarm.wait(deadline, [&](bool /*wait_ok*/) {});

Boost.Coroutine

asio::spawn(
io_context,
[&](const asio::yield_context& yield)
{
agrpc::Alarm alarm{grpc_context};
alarm.wait(deadline, yield); // suspend coroutine until alarm goes off
},
asio::detached);

use_sender

agrpc::use_sender causes functions in this library to return a Sender. They can for example be combined with unifex::task to asynchronously process RPCs using co_await. Note when using libunifex or stdexec exclusively then agrpc::use_sender is already the default completion token:

unifex::task<void> server_streaming_example(agrpc::GrpcContext& grpc_context, example::v1::Example::Stub& stub)
{
grpc::ClientContext client_context;
RPC::Request request;
RPC rpc{grpc_context};
co_await rpc.start(stub, request, agrpc::use_sender);
RPC::Response response;
co_await rpc.read(response, agrpc::use_sender);
co_await rpc.finish(agrpc::use_sender);
}
Primary ClientRPC template.
Definition: forward.hpp:57
Execution context based on grpc::CompletionQueue
Definition: grpc_context.hpp:50
constexpr agrpc::UseSender use_sender
Instance and factory for sender completion tokens.
Definition: use_sender.hpp:55

Custom allocator

Asio-grpc attempts to get the completion handler's associated allocator by calling asio::get_associated_allocator and uses to allocate intermediate storage, typically for the completion handler itself. Prior to invocation of the completion handler all storage is deallocated.

The associated allocator can be customized using asio::bind_allocator (since Boost.Asio 1.79):

co_await alarm.wait(deadline, asio::bind_allocator(my_allocator, asio::use_awaitable));