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

Server-side function object to finish RPCs with an error. More...

#include <agrpc/rpc.hpp>

Public Member Functions

template<class Response , class Request , class CompletionToken = agrpc::DefaultCompletionToken>
auto operator() (grpc::ServerAsyncReader< Response, Request > &reader, const grpc::Status &status, CompletionToken &&token={}) const noexcept(detail::IS_NOTRHOW_GRPC_INITIATE_COMPLETION_TOKEN< CompletionToken >)
 Finish a client stream with an error. More...
 
template<class Response , class CompletionToken = agrpc::DefaultCompletionToken>
auto operator() (grpc::ServerAsyncResponseWriter< Response > &writer, const grpc::Status &status, CompletionToken &&token={}) const noexcept(detail::IS_NOTRHOW_GRPC_INITIATE_COMPLETION_TOKEN< CompletionToken >)
 Finish a unary RPC with an error. More...
 

Detailed Description

Server-side function object to finish RPCs with an error.

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/2]

template<class Response , class Request , class CompletionToken = agrpc::DefaultCompletionToken>
auto agrpc::detail::FinishWithErrorFn::operator() ( grpc::ServerAsyncReader< Response, Request > &  reader,
const grpc::Status &  status,
CompletionToken &&  token = {} 
) const
inlinenoexcept

Finish a client stream with an error.

It should not be called concurrently with other streaming APIs on the same stream.

Side effect:

  • Sends initial metadata if not alreay sent.
  • Uses the ServerContext associated with the call to send possible initial and trailing metadata.

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

Note
Status must have a non-OK code.

Example:

bool finish_with_error_ok = co_await agrpc::finish_with_error(reader, grpc::Status::CANCELLED, asio::use_awaitable);
constexpr detail::FinishWithErrorFn finish_with_error
Finish a RPC with an error.
Definition: rpc.hpp:1556
Parameters
tokenA completion token like asio::yield_context or the one created by agrpc::use_sender. The completion signature is void(bool). The bool should always be true.

◆ operator()() [2/2]

template<class Response , class CompletionToken = agrpc::DefaultCompletionToken>
auto agrpc::detail::FinishWithErrorFn::operator() ( grpc::ServerAsyncResponseWriter< Response > &  writer,
const grpc::Status &  status,
CompletionToken &&  token = {} 
) const
inlinenoexcept

Finish a unary RPC with an error.

Indicate that the stream is to be finished with a non-OK status, and request notification for when the server has finished sending the appropriate signals to the client to end the call.

Should not be used concurrently with other operations.

Side effect:

  • Sends initial metadata if not already sent (using the ServerContext associated with this call).

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

Note
Status must have a non-OK code.

Example:

bool finish_with_error_ok = co_await agrpc::finish_with_error(writer, grpc::Status::CANCELLED, 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). The bool should always be true.