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

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

#include <agrpc/server_rpc.hpp>

Inherits agrpc::detail::ServerRPCBidiStreamingBase< Responder, Traits, Executor >.

Classes

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

Public Types

using Ptr = agrpc::ServerRPCPtr< ServerRPC >
 ServerRPCPtr specialized on this type.
 

Public Member Functions

 ServerRPC ()=delete
 Deleted default constructor.
 

Static Public Attributes

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

Detailed Description

template<class TraitsT, class Executor>
class agrpc::ServerRPC< agrpc::ServerRPCType::GENERIC, TraitsT, Executor >

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

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

Example:

void server_rpc_generic(agrpc::GrpcContext& grpc_context, grpc::AsyncGenericService& service)
{
using RPC = asio::use_awaitable_t<>::as_default_on_t<agrpc::GenericServerRPC>;
agrpc::register_awaitable_rpc_handler<RPC>(
grpc_context, service,
[](RPC& rpc) -> asio::awaitable<void>
{
RPC::Request request_buffer;
if (!co_await rpc.read(request_buffer))
{
co_return;
}
example::v1::Request request;
if (const auto status =
grpc::GenericDeserialize<grpc::ProtoBufferReader, example::v1::Request>(&request_buffer, &request);
!status.ok())
{
co_await rpc.finish(status);
co_return;
}
example::v1::Response response;
response.set_integer(request.integer());
RPC::Response response_buffer;
bool own_buffer;
if (const auto status = grpc::GenericSerialize<grpc::ProtoBufferWriter, example::v1::Response>(
response, &response_buffer, &own_buffer);
!status.ok())
{
co_await rpc.finish(status);
co_return;
}
if (!co_await rpc.write(response_buffer))
{
co_return;
}
co_await rpc.finish(grpc::Status::OK);
},
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