Skip to content

Latest commit

 

History

History

example

Examples

Click on snippet source to jump to the code of an individual example.

Asio client-side

Streaming RPCs (ClientRPC API)

// ---------------------------------------------------
// A simple client-streaming request with coroutines.
// ---------------------------------------------------

snippet source | anchor

// ---------------------------------------------------
// A simple server-streaming request with coroutines.
// ---------------------------------------------------

snippet source | anchor

// ---------------------------------------------------
// A bidirectional-streaming request that simply sends the response from the server back to it.
// ---------------------------------------------------

snippet source | anchor

// ---------------------------------------------------
// A unary request with a per-RPC step timeout. Using a unary RPC for demonstration purposes, the same mechanism can be
// applied to streaming RPCs, where it is arguably more useful.
// For unary RPCs, `grpc::ClientContext::set_deadline` should be preferred.
// ---------------------------------------------------

snippet source | anchor

Multi-threaded

// ---------------------------------------------------
// Multi-threaded client performing 20 unary requests
// ---------------------------------------------------

snippet source | anchor

Generic

// ---------------------------------------------------
// A simple generic unary with Boost.Coroutine.
// ---------------------------------------------------

snippet source | anchor

// ---------------------------------------------------
// A generic bidirectional-streaming request that simply sends the response from the server back to it using Asio's
// stackless coroutines.
// ---------------------------------------------------

snippet source | anchor

Share io_context

// ---------------------------------------------------
// Example showing how to run an io_context and a GrpcContext on the same thread for gRPC clients.
// ---------------------------------------------------

snippet source | anchor

io_uring file transfer

// ---------------------------------------------------
// Example showing how to transfer files over a streaming RPC. Stack buffers are used to customize memory allocation.
// ---------------------------------------------------

snippet source | anchor

Asio server-side

Helloworld

// ---------------------------------------------------
// Server-side hello world which handles exactly one request from the client before shutting down.
// ---------------------------------------------------

snippet source | anchor

Streaming RPCs

// ---------------------------------------------------
// A simple client-streaming rpc handler using C++20 coroutines.
// ---------------------------------------------------

snippet source | anchor

// ---------------------------------------------------
// A simple server-streaming rpc handler using C++20 coroutines.
// ---------------------------------------------------

snippet source | anchor

// ---------------------------------------------------
// The following bidirectional-streaming example shows how to dispatch requests to a thread_pool and write responses
// back to the client.
// ---------------------------------------------------

snippet source | anchor

Multi-threaded

// ---------------------------------------------------
// Multi-threaded server performing 20 unary requests using callback API
// ---------------------------------------------------

snippet source | anchor

Generic

// ---------------------------------------------------
// Handle a simple generic unary request with Boost.Coroutine.
// ---------------------------------------------------

snippet source | anchor

// ---------------------------------------------------
// A bidirectional-streaming example that shows how to dispatch requests to a thread_pool and write responses
// back to the client.
// ---------------------------------------------------

snippet source | anchor

Share io_context

// ---------------------------------------------------
// Example showing how to run an io_context and a GrpcContext on the same thread for gRPC servers.
// This can i.e. be useful when writing an HTTP server that occasionally reaches out to a gRPC server. In that case
// creating a separate thread for the GrpcContext might be undesirable due to added synchronization complexity.
// ---------------------------------------------------

snippet source | anchor

io_uring file transfer

// ---------------------------------------------------
// Example showing how to transfer files over a streaming RPC. Stack buffers are used to customize memory allocation.
// ---------------------------------------------------

snippet source | anchor

Libunifex

Client-side

// ---------------------------------------------------
// A simple unary request with unifex coroutines.
// ---------------------------------------------------

snippet source | anchor

// ---------------------------------------------------
// A server-streaming request with unifex sender/receiver.
// ---------------------------------------------------

snippet source | anchor

// ---------------------------------------------------
// A unifex, unary request with a per-RPC step timeout. Using a unary RPC for demonstration purposes, the same mechanism
// can be applied to streaming RPCs, where it is arguably more useful. For unary RPCs,
// `grpc::ClientContext::set_deadline` is the preferred way of specifying a timeout.
// ---------------------------------------------------

snippet source | anchor

Server-side

// ---------------------------------------------------
// Register a request handler to unary requests. A bit of boilerplate code regarding stop_source has been added to make
// the example testable.
// ---------------------------------------------------

snippet source | anchor

// ---------------------------------------------------
// A simple server-streaming request handler using coroutines.
// ---------------------------------------------------

snippet source | anchor