asio-grpc v1.6.0
Asynchronous gRPC with Asio/unified executors
agrpc::detail::WriteLastFn Struct Reference

Function object to coalesce write and send trailing metadata of streaming RPCs. More...

#include <agrpc/rpc.hpp>

Public Member Functions

template<class Response , class CompletionToken = agrpc::DefaultCompletionToken>
auto operator() (grpc::ServerAsyncWriter< Response > &writer, const Response &response, grpc::WriteOptions options, CompletionToken &&token={}) const noexcept(detail::IS_NOTRHOW_GRPC_INITIATE_COMPLETION_TOKEN< CompletionToken >)
 Coalesce write and send trailing metadata of a server stream. More...
 
template<class Request , class CompletionToken = agrpc::DefaultCompletionToken>
auto operator() (grpc::ClientAsyncWriter< Request > &writer, const Request &request, grpc::WriteOptions options, CompletionToken &&token={}) const noexcept(detail::IS_NOTRHOW_GRPC_INITIATE_COMPLETION_TOKEN< CompletionToken >)
 Perform write and writes_done in a single step. More...
 
template<class Response , class Request , class CompletionToken = agrpc::DefaultCompletionToken>
auto operator() (grpc::ServerAsyncReaderWriter< Response, Request > &reader_writer, const Response &response, grpc::WriteOptions options, CompletionToken &&token={}) const noexcept(detail::IS_NOTRHOW_GRPC_INITIATE_COMPLETION_TOKEN< CompletionToken >)
 Coalesce write and send trailing metadata of a server stream. More...
 
template<class Request , class Response , class CompletionToken = agrpc::DefaultCompletionToken>
auto operator() (grpc::ClientAsyncReaderWriter< Request, Response > &writer, const Request &request, grpc::WriteOptions options, CompletionToken &&token={}) const noexcept(detail::IS_NOTRHOW_GRPC_INITIATE_COMPLETION_TOKEN< CompletionToken >)
 Perform write and writes_done in a single step. More...
 

Detailed Description

Function object to coalesce write and send trailing metadata of streaming RPCs.

The examples below are based on the following .proto file:

syntax = "proto3";
package example.v1;
service Example {
rpc ServerStreaming(Request) returns (stream Response) {}
rpc ClientStreaming(stream Request) returns (Response) {}
rpc BidirectionalStreaming(stream Request) returns (stream Response) {}
rpc Unary(Request) returns (Response) {}
}
message Request {
int32 integer = 1;
}
message Response {
int32 integer = 1;
}
Attention
The completion handler created from the completion token that is provided to the functions described below must have an associated executor that refers to a GrpcContext:
asio::io_context io_context;
asio::co_spawn(
io_context,
[&]() -> asio::awaitable<void>
{
grpc::ServerContext server_context;
grpc::ServerAsyncReader<example::v1::Response, example::v1::Request> reader{&server_context};
// error: asio::this_coro::executor does not refer to a GrpcContext
// co_await agrpc::request(&example::v1::Example::AsyncService::RequestClientStreaming, service,
// server_context, reader, asio::use_awaitable);
// correct:
co_await agrpc::request(&example::v1::Example::AsyncService::RequestClientStreaming, service,
server_context, reader, asio::bind_executor(grpc_context, asio::use_awaitable));
},
asio::detached);
constexpr detail::RequestFn request
Start a new RPC.
Definition: rpc.hpp:1493

Per-Operation Cancellation

None. Operations will be cancelled when the deadline of the RPC has been reached (see grpc::ClientContext::set_deadline) or the call has been cancelled (see grpc::ClientContext::TryCancel and grpc::ServerContext::TryCancel).

Member Function Documentation

◆ operator()() [1/4]

template<class Request , class Response , class CompletionToken = agrpc::DefaultCompletionToken>
auto agrpc::detail::WriteLastFn::operator() ( grpc::ClientAsyncReaderWriter< Request, Response > &  writer,
const Request &  request,
grpc::WriteOptions  options,
CompletionToken &&  token = {} 
) const
inlinenoexcept

Perform write and writes_done in a single step.

gRPC does not take ownership or a reference to response, so it is safe to to deallocate once write_last returns.

Example:

bool write_last_ok = co_await agrpc::write_last(*reader_writer, request, grpc::WriteOptions{}, asio::use_awaitable);
constexpr detail::WriteLastFn write_last
Coalesce write and send trailing metadata of a streaming RPC.
Definition: rpc.hpp:1538
Parameters
tokenA completion token like asio::yield_context or the one created by agrpc::use_sender. The completion signature is void(bool). true means that the data/metadata/status/etc is going to go to the wire. If it is false, it is not going to the wire because the call is already dead (i.e., canceled, deadline expired, other side dropped the channel, etc).

◆ operator()() [2/4]

template<class Request , class CompletionToken = agrpc::DefaultCompletionToken>
auto agrpc::detail::WriteLastFn::operator() ( grpc::ClientAsyncWriter< Request > &  writer,
const Request &  request,
grpc::WriteOptions  options,
CompletionToken &&  token = {} 
) const
inlinenoexcept

Perform write and writes_done in a single step.

gRPC does not take ownership or a reference to response, so it is safe to to deallocate once write_last returns.

Example:

bool write_last_ok = co_await agrpc::write_last(*writer, request, grpc::WriteOptions{}, asio::use_awaitable);
Parameters
tokenA completion token like asio::yield_context or the one created by agrpc::use_sender. The completion signature is void(bool). true means that the data/metadata/status/etc is going to go to the wire. If it is false, it is not going to the wire because the call is already dead (i.e., canceled, deadline expired, other side dropped the channel, etc).

◆ operator()() [3/4]

template<class Response , class Request , class CompletionToken = agrpc::DefaultCompletionToken>
auto agrpc::detail::WriteLastFn::operator() ( grpc::ServerAsyncReaderWriter< Response, Request > &  reader_writer,
const Response &  response,
grpc::WriteOptions  options,
CompletionToken &&  token = {} 
) const
inlinenoexcept

Coalesce write and send trailing metadata of a server stream.

write_last buffers the response. The writing of response is held until finish is called, where response and trailing metadata are coalesced and write is initiated. Note that write_last can only buffer response up to the flow control window size. If response size is larger than the window size, it will be sent on wire without buffering.

gRPC does not take ownership or a reference to response, so it is safe to to deallocate once write_last returns.

Example:

example::v1::Response response;
bool write_last_ok = co_await agrpc::write_last(reader_writer, response, grpc::WriteOptions{}, asio::use_awaitable);
Parameters
tokenA completion token like asio::yield_context or the one created by agrpc::use_sender. The completion signature is void(bool). true means that the data/metadata/status/etc is going to go to the wire. If it is false, it is not going to the wire because the call is already dead (i.e., canceled, deadline expired, other side dropped the channel, etc).

◆ operator()() [4/4]

template<class Response , class CompletionToken = agrpc::DefaultCompletionToken>
auto agrpc::detail::WriteLastFn::operator() ( grpc::ServerAsyncWriter< Response > &  writer,
const Response &  response,
grpc::WriteOptions  options,
CompletionToken &&  token = {} 
) const
inlinenoexcept

Coalesce write and send trailing metadata of a server stream.

write_last buffers the response. The writing of response is held until finish is called, where response and trailing metadata are coalesced and write is initiated. Note that write_last can only buffer response up to the flow control window size. If response size is larger than the window size, it will be sent on wire without buffering.

gRPC does not take ownership or a reference to response, so it is safe to to deallocate once write_last returns.

Example:

bool write_last_ok = co_await agrpc::write_last(writer, response, grpc::WriteOptions{}, asio::use_awaitable);
Parameters
tokenA completion token like asio::yield_context or the one created by agrpc::use_sender. The completion signature is void(bool). true means that the data/metadata/status/etc is going to go to the wire. If it is false, it is not going to the wire because the call is already dead (i.e., canceled, deadline expired, other side dropped the channel, etc).