asio-grpc v3.1.0
Asynchronous gRPC with Asio/unified executors
agrpc::ClientRPC< agrpc::ClientRPCType::GENERIC_UNARY, Executor > Class Template Reference

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

#include <agrpc/client_rpc.hpp>

+ Inheritance diagram for agrpc::ClientRPC< agrpc::ClientRPCType::GENERIC_UNARY, Executor >:
+ Collaboration diagram for agrpc::ClientRPC< agrpc::ClientRPCType::GENERIC_UNARY, Executor >:

Classes

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

Public Types

using Stub = grpc::GenericStub
 The stub type.
 
using Request = grpc::ByteBuffer
 The response message type.
 
using Response = grpc::ByteBuffer
 The request message type.
 
using executor_type = Executor
 The executor type.
 

Public Member Functions

void start (const std::string &method, grpc::GenericStub &stub, const grpc::ByteBuffer &req)
 Start the rpc. More...
 
template<class CompletionToken = detail::DefaultCompletionTokenT<Executor>>
auto finish (grpc::ByteBuffer &response, CompletionToken &&token=detail::DefaultCompletionTokenT< Executor >{})
 Finish the rpc. More...
 
auto read_initial_metadata (CompletionToken &&token=detail::DefaultCompletionTokenT< Executor >{})
 Read initial metadata. More...
 
const executor_typeget_executor () const noexcept
 Get the executor. More...
 
const executor_typeget_scheduler () const noexcept
 Get the scheduler. More...
 
grpc::ClientContext & context ()
 Get the underlying grpc::ClientContext
 
const grpc::ClientContext & context () const
 Get the underlying grpc::ClientContext (const overload)
 
void cancel () noexcept
 Cancel this RPC. More...
 

Static Public Member Functions

template<class CompletionToken = detail::DefaultCompletionTokenT<Executor>>
static auto request (agrpc::GrpcContext &grpc_context, const std::string &method, grpc::GenericStub &stub, grpc::ClientContext &context, const grpc::ByteBuffer &request, grpc::ByteBuffer &response, CompletionToken &&token=detail::DefaultCompletionTokenT< Executor >{})
 Start a generic unary request. More...
 
template<class CompletionToken = detail::DefaultCompletionTokenT<Executor>>
static auto request (const Executor &executor, const std::string &method, grpc::GenericStub &stub, grpc::ClientContext &context, const grpc::ByteBuffer &request, grpc::ByteBuffer &response, CompletionToken &&token=detail::DefaultCompletionTokenT< Executor >{})
 Start a generic unary request (executor overload)
 

Static Public Attributes

static constexpr agrpc::ClientRPCType TYPE = agrpc::ClientRPCType::GENERIC_UNARY
 The rpc type.
 

Detailed Description

template<class Executor>
class agrpc::ClientRPC< agrpc::ClientRPCType::GENERIC_UNARY, Executor >

I/O object for client-side, generic, unary rpcs.

Example:

asio::awaitable<void> client_rpc_generic_unary(agrpc::GrpcContext& grpc_context, grpc::GenericStub& stub)
{
grpc::ClientContext client_context;
client_context.set_deadline(std::chrono::system_clock::now() + std::chrono::seconds(5));
example::v1::Request request;
grpc::ByteBuffer request_buffer;
bool own_buffer;
grpc::GenericSerialize<grpc::ProtoBufferWriter, example::v1::Request>(request, &request_buffer, &own_buffer);
(void)own_buffer;
grpc::ByteBuffer response_buffer;
if (const grpc::Status status =
co_await RPC::request(grpc_context, "/example.v1.Example/Unary", stub, client_context, request_buffer,
response_buffer, asio::use_awaitable);
!status.ok())
{
std::cerr << "Rpc failed: " << status.error_message();
co_return;
}
example::v1::Response response;
if (const grpc::Status status =
grpc::GenericDeserialize<grpc::ProtoBufferReader, example::v1::Response>(&response_buffer, &response);
!status.ok())
{
std::cerr << "Rpc failed: unexpected response type";
co_return;
}
std::cout << "Response: " << response.integer();
}
static auto request(agrpc::GrpcContext &grpc_context, const std::string &method, grpc::GenericStub &stub, grpc::ClientContext &context, const grpc::ByteBuffer &request, grpc::ByteBuffer &response, CompletionToken &&token=detail::DefaultCompletionTokenT< Executor >{})
Start a generic unary request.
Definition: client_rpc.hpp:321
Execution context based on grpc::CompletionQueue
Definition: grpc_context.hpp:50
agrpc::ClientRPC< agrpc::ClientRPCType::GENERIC_UNARY > GenericUnaryClientRPC
I/O object for client-side, generic, unary rpcs (type alias)
Definition: client_rpc.hpp:395

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
ExecutorThe executor type, must be capable of referring to a agrpc::GrpcContext.

Per-Operation Cancellation

Terminal and partial. Cancellation is performed by invoking grpc::ClientContext::TryCancel. Operations are also cancelled when the deadline of the rpc has been reached (see grpc::ClientContext::set_deadline).

Since
2.6.0

Member Function Documentation

◆ request()

template<class Executor >
template<class CompletionToken = detail::DefaultCompletionTokenT<Executor>>
static auto agrpc::ClientRPC< agrpc::ClientRPCType::GENERIC_UNARY, Executor >::request ( agrpc::GrpcContext grpc_context,
const std::string &  method,
grpc::GenericStub &  stub,
grpc::ClientContext &  context,
const grpc::ByteBuffer &  request,
grpc::ByteBuffer &  response,
CompletionToken &&  token = detail::DefaultCompletionTokenT<Executor>{} 
)
inlinestatic

Start a generic unary request.

Parameters
methodThe gRPC method to call, e.g. "/test.v1.Test/Unary"
requestThe request message, save to delete when this function returns, unless a deferred completion token is used like agrpc::use_sender or asio::deferred.
responseThe response message, will be filled by the server upon finishing this rpc. Must remain alive until this rpc is finished.
tokenA completion token like asio::yield_context or agrpc::use_sender. The completion signature is void(grpc::Status). Use grpc::Status::ok() to check whether the request was successful.

◆ start()

template<class Executor >
void agrpc::ClientRPC< agrpc::ClientRPCType::GENERIC_UNARY, Executor >::start ( const std::string &  method,
grpc::GenericStub &  stub,
const grpc::ByteBuffer &  req 
)
inline

Start the rpc.

Parameters
reqThe request message, save to delete when this function returns, unless a deferred completion token like agrpc::use_sender or asio::deferred is used.
methodThe RPC method to call, e.g. "/test.v1.Test/Unary"

◆ finish()

template<class Executor >
template<class CompletionToken = detail::DefaultCompletionTokenT<Executor>>
auto agrpc::ClientRPC< agrpc::ClientRPCType::GENERIC_UNARY, Executor >::finish ( grpc::ByteBuffer &  response,
CompletionToken &&  token = detail::DefaultCompletionTokenT<Executor>{} 
)
inline

Finish the rpc.

Receive the server's response message and final status for the call.

This operation will finish when either:

  • The server's response message and status have been received.
  • The server has returned a non-OK status (no message expected in this case).
  • The call failed for some reason and the library generated a non-OK status.

Side effect:

  • The ClientContext associated with the call is updated with possible initial and trailing metadata sent from the server.
Parameters
tokenA completion token like asio::yield_context or agrpc::use_sender. The completion signature is void(grpc::Status).

◆ read_initial_metadata()

auto agrpc::detail::ClientRPCBase< grpc::GenericClientAsyncResponseReader , Executor >::read_initial_metadata ( CompletionToken &&  token = detail::DefaultCompletionTokenT<Executor>{})
inlineinherited

Read initial metadata.

Request notification of the reading of the initial metadata.

This call is optional.

Side effect:

  • Upon receiving initial metadata from the server, the ClientContext associated with this call is updated, and the calling code can access the received metadata through the ClientContext.
Attention
If the server does not explicitly send initial metadata (e.g. by calling agrpc::send_initial_metadata) but waits for a message from the client instead then this function won't complete until write() is called.
Parameters
tokenA completion token like asio::yield_context or agrpc::use_sender. The completion signature is void(bool). true indicates that the metadata was read. If it is false, then the call is dead.

◆ 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()

void agrpc::detail::ClientRPCContextBase< grpc::GenericClientAsyncResponseReader >::cancel ( )
inlinenoexceptinherited

Cancel this RPC.

Effectively calls context().TryCancel().

Thread-safe