asio-grpc v3.5.0
Asynchronous gRPC with Asio/unified executors
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
agrpc::BasicServerUnaryReactor< Executor > Class Template Reference

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

#include <agrpc/server_callback.hpp>

+ Inheritance diagram for agrpc::BasicServerUnaryReactor< Executor >:
+ Collaboration diagram for agrpc::BasicServerUnaryReactor< Executor >:

Classes

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

Public Types

using executor_type = Executor
 The executor type.
 

Public Member Functions

grpc::ServerUnaryReactor * get () noexcept
 Get underlying gRPC reactor.
 
void initiate_send_initial_metadata ()
 Send initial metadata.
 
template<class CompletionToken = detail::DefaultCompletionTokenT<Executor>>
auto wait_for_send_initial_metadata (CompletionToken &&token=CompletionToken{})
 Wait for send initial metadata.
 
void initiate_finish (grpc::Status status)
 Finish rpc.
 
template<class CompletionToken = detail::DefaultCompletionTokenT<Executor>>
auto wait_for_finish (CompletionToken &&token=CompletionToken{})
 Wait for finish.
 
const Executor & get_executor () const noexcept
 Get the executor.
 

Detailed Description

template<class Executor>
class agrpc::BasicServerUnaryReactor< Executor >

(experimental) I/O object for server-side, unary rpcs

Create an object of this type using agrpc::make_reactor/agrpc::allocate_reactor or server_callback_coroutine.hpp. Note that grpc::CallbackServerContext::DefaultReactor() should be use instead of this class whenever possible.

Example:

grpc::ServerUnaryReactor* Unary(grpc::CallbackServerContext* context, const example::v1::Request*,
example::v1::Response* response) override
{
auto& rpc = *ptr;
context->AddInitialMetadata("example", "value");
rpc.initiate_send_initial_metadata();
rpc.wait_for_send_initial_metadata(
[response, ptr = std::move(ptr)](const error_code&, bool ok)
{
if (!ok)
{
return;
}
response->set_integer(42);
ptr->initiate_finish(grpc::Status::OK);
ptr->wait_for_finish(
[](const error_code&, bool ok)
{
if (!ok)
{
// ...
}
});
});
return rpc.get();
}

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.

Per-Operation Cancellation

All. Cancellation will merely interrupt the act of waiting and does not cancel the underlying rpc.

Since
3.5.0

Member Function Documentation

◆ get()

template<class Executor >
grpc::ServerUnaryReactor * agrpc::BasicServerUnaryReactor< Executor >::get ( )
inlinenodiscardnoexcept

Get underlying gRPC reactor.

The returned object should be passed to the gRPC library. Invoking any of its functions may result in undefined behavior.

◆ initiate_send_initial_metadata()

template<class Executor >
void agrpc::BasicServerUnaryReactor< Executor >::initiate_send_initial_metadata ( )
inline

Send initial metadata.

Send any initial metadata stored in the CallbackServerContext. If not invoked, any initial metadata will be passed along with initiate_finish.

◆ wait_for_send_initial_metadata()

template<class Executor >
template<class CompletionToken = detail::DefaultCompletionTokenT<Executor>>
auto agrpc::BasicServerUnaryReactor< Executor >::wait_for_send_initial_metadata ( CompletionToken && token = CompletionToken{})
inline

Wait for send initial metadata.

Waits for the completion of initiate_send_initial_metadata. Only one wait for send initial metadata may be outstanding at any time.

Completion signature is void(error_code, bool). If the bool is false then the rpc failed (cancelled, disconnected, deadline reached, ...).

◆ initiate_finish()

template<class Executor >
void agrpc::BasicServerUnaryReactor< Executor >::initiate_finish ( grpc::Status status)
inline

Finish rpc.

Indicate that the stream is to be finished and the trailing metadata and rpc status are to be sent. May only be called once. If the status is non-OK, any message will not be sent. Instead, the client will only receive the status and any trailing metadata.

◆ wait_for_finish()

template<class Executor >
template<class CompletionToken = detail::DefaultCompletionTokenT<Executor>>
auto agrpc::BasicServerUnaryReactor< Executor >::wait_for_finish ( CompletionToken && token = CompletionToken{})
inline

Wait for finish.

Wait until all operations associated with this rpc have completed. Only one wait for finish may be outstanding at any time.

Completion signature is void(error_code, bool). If the bool is false then the rpc failed (cancelled, disconnected, deadline reached, ...).

◆ get_executor()

template<class Executor >
const Executor & agrpc::detail::ReactorExecutorBase< Executor >::get_executor ( ) const
inlinenodiscardnoexceptinherited

Get the executor.

Thread-safe