userver: userver/ugrpc/server/impl/async_service.hpp Source File
Loading...
Searching...
No Matches
async_service.hpp
1#pragma once
2
3#include <grpcpp/completion_queue.h>
4#include <grpcpp/impl/service_type.h>
5#include <grpcpp/server_context.h>
6
7#include <userver/ugrpc/server/impl/call_traits.hpp>
8
9USERVER_NAMESPACE_BEGIN
10
11namespace ugrpc::server::impl {
12
13template <typename Service>
14class AsyncService final : public Service::Service {
15 public:
16 explicit AsyncService(std::size_t method_count) {
17 // Mark all methods as implemented
18 for (std::size_t i = 0; i < method_count; ++i) {
19 this->MarkMethodAsync(i);
20 }
21 }
22
23 template <typename CallTraits>
24 void Prepare(int method_id, grpc::ServerContext& context,
25 typename CallTraits::InitialRequest& initial_request,
26 typename CallTraits::RawCall& stream,
27 grpc::CompletionQueue& call_cq,
28 grpc::ServerCompletionQueue& notification_cq, void* tag) {
29 constexpr auto kCallCategory = CallTraits::kCallCategory;
30
31 if constexpr (kCallCategory == CallCategory::kUnary) {
32 this->RequestAsyncUnary(method_id, &context, &initial_request, &stream,
33 &call_cq, &notification_cq, tag);
34 } else if constexpr (kCallCategory == CallCategory::kInputStream) {
35 this->RequestAsyncClientStreaming(method_id, &context, &stream, &call_cq,
36 &notification_cq, tag);
37 } else if constexpr (kCallCategory == CallCategory::kOutputStream) {
38 this->RequestAsyncServerStreaming(method_id, &context, &initial_request,
39 &stream, &call_cq, &notification_cq,
40 tag);
41 } else {
42 this->RequestAsyncBidiStreaming(method_id, &context, &stream, &call_cq,
43 &notification_cq, tag);
44 }
45 }
46};
47
48} // namespace ugrpc::server::impl
49
50USERVER_NAMESPACE_END