| 33 | // end-snippet |
| 34 | |
| 35 | void register_request_handler(agrpc::GrpcContext& grpc_context, helloworld::Greeter::AsyncService& service, |
| 36 | example::ServerShutdown& shutdown) |
| 37 | { |
| 38 | using RPC = agrpc::ServerRPC<&helloworld::Greeter::AsyncService::RequestSayHello>; |
| 39 | agrpc::register_callback_rpc_handler<RPC>( |
| 40 | grpc_context, service, |
| 41 | [&](RPC::Ptr ptr, helloworld::HelloRequest& request) |
| 42 | { |
| 43 | helloworld::HelloReply response; |
| 44 | response.set_message("Hello " + request.name()); |
| 45 | auto& rpc = *ptr; |
| 46 | rpc.finish(response, grpc::Status::OK, |
| 47 | [&, p = std::move(ptr)](bool) |
| 48 | { |
| 49 | // In this example we shut down the server after 20 requests |
| 50 | static std::atomic_int counter{}; |
| 51 | if (19 == counter.fetch_add(1)) |
| 52 | { |
| 53 | shutdown.shutdown(); |
| 54 | } |
| 55 | }); |
| 56 | }, |
| 57 | example::RethrowFirstArg{}); |
| 58 | } |
| 59 | |
| 60 | int main(int argc, const char** argv) |
| 61 | { |