/
server.cpp
99 lines (86 loc) · 3.18 KB
/
server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2024 Dennis Hezel
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "example/v1/example.grpc.pb.h"
#include "grpc/health/v1/health.grpc.pb.h"
#include "helper.hpp"
#include "server_shutdown_asio.hpp"
#include <agrpc/asio_grpc.hpp>
#include <agrpc/health_check_service.hpp>
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/coroutine.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/experimental/promise.hpp>
#include <boost/asio/experimental/use_promise.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/use_awaitable.hpp>
#include <grpcpp/server.h>
#include <grpcpp/server_builder.h>
#include <optional>
namespace asio = boost::asio;
void create_server_grpc_context()
{
/* [create-grpc_context-server-side] */
grpc::ServerBuilder builder;
agrpc::GrpcContext grpc_context{builder.AddCompletionQueue()};
/* [create-grpc_context-server-side] */
}
void health_check_service()
{
/* [add-health-check-service] */
std::unique_ptr<grpc::Server> server;
grpc::ServerBuilder builder;
agrpc::GrpcContext grpc_context{builder.AddCompletionQueue()};
agrpc::add_health_check_service(builder);
server = builder.BuildAndStart();
agrpc::start_health_check_service(*server, grpc_context);
/* [add-health-check-service] */
}
void server_main()
{
example::v1::Example::AsyncService service;
std::unique_ptr<grpc::Server> server;
// begin-snippet: create-grpc_context-server-side
grpc::ServerBuilder builder;
agrpc::GrpcContext grpc_context{builder.AddCompletionQueue()};
// end-snippet
builder.AddListeningPort("0.0.0.0:50051", grpc::InsecureServerCredentials());
builder.RegisterService(&service);
server = builder.BuildAndStart();
std::optional guard{asio::require(grpc_context.get_executor(), asio::execution::outstanding_work_t::tracked)};
asio::co_spawn(
grpc_context,
[&]() -> asio::awaitable<void>
{
co_return;
},
asio::detached);
grpc_context.run();
server->Shutdown();
}
void register_handlers(agrpc::GrpcContext&, example::v1::Example::AsyncService&) {}
void server_main_cheat_sheet()
{
/* [server-main-cheat-sheet] */
example::v1::Example::AsyncService service;
std::unique_ptr<grpc::Server> server;
grpc::ServerBuilder builder;
agrpc::GrpcContext grpc_context{builder.AddCompletionQueue()};
builder.AddListeningPort("0.0.0.0:50051", grpc::InsecureServerCredentials());
builder.RegisterService(&service);
server = builder.BuildAndStart();
example::ServerShutdown shutdown{*server, grpc_context};
register_handlers(grpc_context, service);
grpc_context.run();
/* [server-main-cheat-sheet] */
}