asio-grpc v3.1.0
Asynchronous gRPC with Asio/unified executors
agrpc::ServerRPC< RequestUnary, TraitsT, Executor > Class Template Reference

I/O object for server-side, unary rpcs. More...

#include <agrpc/server_rpc.hpp>

+ Inheritance diagram for agrpc::ServerRPC< RequestUnary, TraitsT, Executor >:
+ Collaboration diagram for agrpc::ServerRPC< RequestUnary, TraitsT, Executor >:

Classes

struct  rebind_executor
 Rebind the ServerRPC to another executor. More...
 

Public Types

using Request = RequestT
 The response message type.
 
using Response = ResponseT
 The request message type.
 
using Traits = TraitsT
 The traits type.
 
using Ptr = agrpc::ServerRPCPtr< ServerRPC >
 ServerRPCPtr specialized on this type.
 
using executor_type = Executor
 The executor type.
 

Public Member Functions

 ServerRPC ()=delete
 Deleted default constructor.
 
template<class CompletionToken = detail::DefaultCompletionTokenT<Executor>>
auto finish (const ResponseT &response, const grpc::Status &status, CompletionToken &&token=detail::DefaultCompletionTokenT< Executor >{})
 Finish the rpc. More...
 
template<class CompletionToken = detail::DefaultCompletionTokenT<Executor>>
auto finish_with_error (const grpc::Status &status, CompletionToken &&token=detail::DefaultCompletionTokenT< Executor >{})
 Finish the rpc with an error. More...
 
auto send_initial_metadata (CompletionToken &&token=detail::DefaultCompletionTokenT< Executor >{})
 Send initial metadata. More...
 
bool is_done () const noexcept
 Is this rpc done? More...
 
auto wait_for_done (CompletionToken &&token=detail::DefaultCompletionTokenT< Executor >{})
 Wait for done. More...
 
const executor_typeget_executor () const noexcept
 Get the executor. More...
 
const executor_typeget_scheduler () const noexcept
 Get the scheduler. More...
 
auto & context ()
 Get the underlying ServerContext
 
const auto & context () const
 Get the underlying ServerContext (const overload)
 
void cancel () noexcept
 Cancel this RPC. More...
 

Static Public Member Functions

static constexpr std::string_view service_name () noexcept
 Name of the gRPC service. More...
 
static constexpr std::string_view method_name () noexcept
 Name of the gRPC method. More...
 

Static Public Attributes

static constexpr agrpc::ServerRPCType TYPE = agrpc::ServerRPCType::UNARY
 The rpc type.
 

Detailed Description

template<class ServiceT, class RequestT, class ResponseT, detail::ServerUnaryRequest< ServiceT, RequestT, ResponseT > RequestUnary, class TraitsT, class Executor>
class agrpc::ServerRPC< RequestUnary, TraitsT, Executor >

I/O object for server-side, unary rpcs.

Use one of the agrpc::register_ functions to set up request handling.

Example:

void server_rpc_unary(agrpc::GrpcContext& grpc_context,
example::v1::Example::AsyncService& service)
{
using RPC = asio::use_awaitable_t<>::as_default_on_t<
agrpc::register_awaitable_rpc_handler<RPC>(
grpc_context, service,
[](RPC& rpc, RPC::Request& request) -> asio::awaitable<void>
{
RPC::Response response;
response.set_integer(request.integer());
co_await rpc.finish(response, grpc::Status::OK);
// Alternatively finish with an error:
co_await rpc.finish_with_error(grpc::Status::CANCELLED);
},
asio::detached);
}
Execution context based on grpc::CompletionQueue
Definition: grpc_context.hpp:50
Primary ServerRPC template.
Definition: forward.hpp:76

Based on .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;
}
Template Parameters
RequestUnaryA pointer to the generated gRPC method.
TraitsA type used to customize this rpc. See agrpc::DefaultServerRPCTraits.
ExecutorThe executor type, must be capable of referring to a agrpc::GrpcContext.

Per-Operation Cancellation

(except wait_for_done) Terminal and partial. Cancellation is performed by invoking grpc::ServerContext::TryCancel. After successful cancellation no further operations should be started on the rpc. Operations are also cancelled when the deadline of the rpc has been reached.

Since
2.7.0

Member Function Documentation

◆ service_name()

template<class ServiceT , class RequestT , class ResponseT , detail::ServerUnaryRequest< ServiceT, RequestT, ResponseT > RequestUnary, class TraitsT , class Executor >
static constexpr std::string_view agrpc::ServerRPC< RequestUnary, TraitsT, Executor >::service_name ( )
inlinestaticconstexprnoexcept

Name of the gRPC service.

Equal to the generated Service::service_full_name().

E.g. for the .proto schema

package example.v1;
service Example { ... }

the return value would be "example.v1.Example".

◆ method_name()

template<class ServiceT , class RequestT , class ResponseT , detail::ServerUnaryRequest< ServiceT, RequestT, ResponseT > RequestUnary, class TraitsT , class Executor >
static constexpr std::string_view agrpc::ServerRPC< RequestUnary, TraitsT, Executor >::method_name ( )
inlinestaticconstexprnoexcept

Name of the gRPC method.

E.g. for agrpc::ServerRPC<&example::Example::AsyncService::RequestMyMethod> the return value would be "MyMethod".

◆ finish()

template<class ServiceT , class RequestT , class ResponseT , detail::ServerUnaryRequest< ServiceT, RequestT, ResponseT > RequestUnary, class TraitsT , class Executor >
template<class CompletionToken = detail::DefaultCompletionTokenT<Executor>>
auto agrpc::ServerRPC< RequestUnary, TraitsT, Executor >::finish ( const ResponseT &  response,
const grpc::Status &  status,
CompletionToken &&  token = detail::DefaultCompletionTokenT<Executor>{} 
)
inline

Finish the rpc.

Indicate that the RPC is to be finished and request notification when the server has sent the appropriate signals to the client to end the call. Should not be used concurrently with other operations.

Side effect:

  • Also sends initial metadata if not already sent (using the ServerContext associated with the call).
Note
If status has a non-OK code, then message will not be sent, and the client will receive only the status with possible trailing metadata.

GRPC does not take ownership or a reference to message and status, so it is safe to deallocate once finish returns, unless a deferred completion token like agrpc::use_sender or asio::deferred is used.

Parameters
tokenA completion token like asio::yield_context or 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).

◆ finish_with_error()

template<class ServiceT , class RequestT , class ResponseT , detail::ServerUnaryRequest< ServiceT, RequestT, ResponseT > RequestUnary, class TraitsT , class Executor >
template<class CompletionToken = detail::DefaultCompletionTokenT<Executor>>
auto agrpc::ServerRPC< RequestUnary, TraitsT, Executor >::finish_with_error ( const grpc::Status &  status,
CompletionToken &&  token = detail::DefaultCompletionTokenT<Executor>{} 
)
inline

Finish the 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.

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

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, unless a deferred completion token like agrpc::use_sender or asio::deferred is used.

Note
Status must have a non-OK code.
Parameters
tokenA completion token like asio::yield_context or 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).

◆ send_initial_metadata()

auto agrpc::detail::ServerRPCBase< grpc::ServerAsyncResponseWriter< ResponseT > , TraitsT , Executor >::send_initial_metadata ( CompletionToken &&  token = detail::DefaultCompletionTokenT<Executor>{})
inlineinherited

Send initial metadata.

Request notification of the sending of initial metadata to the client.

This call is optional, but if it is used, it cannot be used concurrently with or after the finish/finish_with_error method.

Parameters
tokenA completion token like asio::yield_context or 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).

◆ is_done()

bool agrpc::detail::ServerRPCNotifyWhenDoneMixin< IsNotifyWhenDone, grpc::ServerAsyncResponseWriter< ResponseT > , Executor >::is_done ( ) const
inlinenoexceptinherited

Is this rpc done?

Only available if Traits contain NOTIFY_WHEN_DONE = true.

Returns true if NotifyWhenDone has fired which indicates the finish has been called or that the rpc is dead (i.e., canceled, deadline expired, other side dropped the channel, etc).

Thread-safe

◆ wait_for_done()

auto agrpc::detail::ServerRPCNotifyWhenDoneMixin< IsNotifyWhenDone, grpc::ServerAsyncResponseWriter< ResponseT > , Executor >::wait_for_done ( CompletionToken &&  token = detail::DefaultCompletionTokenT<Executor>{})
inlineinherited

Wait for done.

Only available if Traits contain NOTIFY_WHEN_DONE = true.

Request notification of the completion of this rpc, either due to calling finish or because the rpc is dead (i.e., canceled, deadline expired, other side dropped the channel, etc). rpc.context().IsCancelled() may only be called after this operation completes.

Cancelling this operation does not invoke grpc::ServerContext::TryCancel.

Parameters
tokenA completion token like asio::yield_context or agrpc::use_sender. The completion signature is void().

◆ get_executor()

template<class Executor >
const executor_type & agrpc::detail::RPCExecutorBase< Executor >::get_executor ( ) const
inlinenoexceptinherited

Get the executor.

Thread-safe

◆ get_scheduler()

template<class Executor >
const executor_type & agrpc::detail::RPCExecutorBase< Executor >::get_scheduler ( ) const
inlinenoexceptinherited

Get the scheduler.

Thread-safe

Since
2.9.0

◆ cancel()

template<class Responder >
void agrpc::detail::ServerRPCContextBase< Responder >::cancel ( )
inlinenoexceptinherited

Cancel this RPC.

Effectively calls context().TryCancel().

Thread-safe