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

Server-side function object to coalesce write and finish 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, const grpc::Status &status, CompletionToken &&token={}) const noexcept(detail::IS_NOTRHOW_GRPC_INITIATE_COMPLETION_TOKEN< CompletionToken >)
 Coalesce write and finish of a server stream. More...
 
template<class Response , class Request , class CompletionToken = agrpc::DefaultCompletionToken>
auto operator() (grpc::ServerAsyncReaderWriter< Response, Request > &reader_writer, const Response &response, grpc::WriteOptions options, const grpc::Status &status, CompletionToken &&token={}) const noexcept(detail::IS_NOTRHOW_GRPC_INITIATE_COMPLETION_TOKEN< CompletionToken >)
 Coalesce write and finish of a bidirectional stream. More...
 

Detailed Description

Server-side function object to coalesce write and finish 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:1394

Member Function Documentation

◆ operator()() [1/2]

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

Coalesce write and finish of a bidirectional stream.

Write response and coalesce it with trailing metadata which contains status, using WriteOptions options.

write_and_finish is equivalent of performing write_last and finish in a single step.

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

Implicit input parameter:

  • The ServerContext associated with the call is used for sending trailing (and initial) metadata to the client.
Note
Status must have an OK code.

Example:

bool write_and_finish_ok = co_await agrpc::write_and_finish(reader_writer, response, grpc::WriteOptions{},
grpc::Status::OK, asio::use_awaitable);
constexpr detail::WriteAndFinishFn write_and_finish
Coalesce write and finish of a streaming RPC.
Definition: rpc.hpp:1448
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/2]

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

Coalesce write and finish of a server stream.

Write response and coalesce it with trailing metadata which contains status, using WriteOptions options.

write_and_finish is equivalent of performing write_last and finish in a single step.

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

Implicit input parameter:

  • The ServerContext associated with the call is used for sending trailing (and initial) metadata to the client.
Note
Status must have an OK code.

Example:

bool write_and_finish_ok =
co_await agrpc::write_and_finish(writer, response, grpc::WriteOptions{}, grpc::Status::OK, 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).