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

Client and server-side function object to read from streaming RPCs. More...

#include <agrpc/rpc.hpp>

Public Member Functions

template<class Response , class Request , class CompletionToken = agrpc::DefaultCompletionToken>
auto operator() (grpc::ServerAsyncReader< Response, Request > &reader, Request &request, CompletionToken &&token={}) const noexcept(detail::IS_NOTRHOW_GRPC_INITIATE_COMPLETION_TOKEN< CompletionToken >)
 Read from a client stream. More...
 
template<class Response , class Request , class CompletionToken = agrpc::DefaultCompletionToken>
auto operator() (grpc::ServerAsyncReaderWriter< Response, Request > &reader_writer, Request &request, CompletionToken &&token={}) const noexcept(detail::IS_NOTRHOW_GRPC_INITIATE_COMPLETION_TOKEN< CompletionToken >)
 Read from a bidirectional server stream. More...
 
template<class Response , class CompletionToken = agrpc::DefaultCompletionToken>
auto operator() (grpc::ClientAsyncReader< Response > &reader, Response &response, CompletionToken &&token={}) const noexcept(detail::IS_NOTRHOW_GRPC_INITIATE_COMPLETION_TOKEN< CompletionToken >)
 Read from a server stream. More...
 
template<class Request , class Response , class CompletionToken = agrpc::DefaultCompletionToken>
auto operator() (grpc::ClientAsyncReaderWriter< Request, Response > &reader_writer, Response &response, CompletionToken &&token={}) const noexcept(detail::IS_NOTRHOW_GRPC_INITIATE_COMPLETION_TOKEN< CompletionToken >)
 Read from a bidirectional client stream. More...
 

Detailed Description

Client and server-side function object to read from streaming RPCs.

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:1394

Member Function Documentation

◆ operator()() [1/4]

template<class Response , class CompletionToken = agrpc::DefaultCompletionToken>
auto agrpc::detail::ReadFn::operator() ( grpc::ClientAsyncReader< Response > &  reader,
Response &  response,
CompletionToken &&  token = {} 
) const
inlinenoexcept

Read from a server stream.

It should not be called concurrently with other streaming APIs on the same stream. It is not meaningful to call it concurrently with another read on the same stream since reads on the same stream are delivered in order.

Example:

example::v1::Response response;
bool read_ok = co_await agrpc::read(*reader, response, asio::use_awaitable);
constexpr detail::ReadFn read
Read from a streaming RPC.
Definition: rpc.hpp:1403
Parameters
tokenA completion token like asio::yield_context or the one created by agrpc::use_sender. The completion signature is void(bool). true indicates that a valid message was read, false when the call is dead.

◆ operator()() [2/4]

template<class Request , class Response , class CompletionToken = agrpc::DefaultCompletionToken>
auto agrpc::detail::ReadFn::operator() ( grpc::ClientAsyncReaderWriter< Request, Response > &  reader_writer,
Response &  response,
CompletionToken &&  token = {} 
) const
inlinenoexcept

Read from a bidirectional client stream.

This is thread-safe with respect to write or writes_done methods. It should not be called concurrently with other streaming APIs on the same stream. It is not meaningful to call it concurrently with another read on the same stream since reads on the same stream are delivered in order.

Example:

example::v1::Response response;
bool read_ok = co_await agrpc::read(*reader_writer, response, 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). true indicates that a valid message was read, false when the call is dead.

◆ operator()() [3/4]

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

Read from a client stream.

It should not be called concurrently with other streaming APIs on the same stream. It is not meaningful to call it concurrently with another read on the same stream since reads on the same stream are delivered in order.

Example:

example::v1::Request request;
bool read_ok = co_await agrpc::read(reader, request, 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). true indicates that a valid message was read. If not, you know that there are certainly no more messages that can ever be read from this stream. This could happen because the client has done a WritesDone already.

◆ operator()() [4/4]

template<class Response , class Request , class CompletionToken = agrpc::DefaultCompletionToken>
auto agrpc::detail::ReadFn::operator() ( grpc::ServerAsyncReaderWriter< Response, Request > &  reader_writer,
Request &  request,
CompletionToken &&  token = {} 
) const
inlinenoexcept

Read from a bidirectional server stream.

This is thread-safe with respect to write or writes_done methods on the same stream. It should not be called concurrently with another read on the same stream as the order of delivery will not be defined.

Example:

example::v1::Request request;
bool read_ok = co_await agrpc::read(reader_writer, request, 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). true indicates that a valid message was read. false when there will be no more incoming messages, either because the other side has called WritesDone() or the stream has failed (or been cancelled).