-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.cpp
45 lines (35 loc) · 1.2 KB
/
main.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
#include <cstdio>
#include <grpcxx/server.h>
#include "helloworld/v1/greeter.grpcxx.pb.h"
using namespace helloworld::v1::Greeter;
// Implement rpc application logic using template specialisation for generated `ServiceImpl` struct
template <>
rpcHello::result_type ServiceImpl::call<rpcHello>(
grpcxx::context &, const GreeterHelloRequest &req) {
GreeterHelloResponse res;
res.set_message("Hello `" + req.name() + "` 👋");
return {grpcxx::status::code_t::ok, res};
}
// Application defined struct implementing rpc application logic
struct GreeterImpl {
template <typename T>
typename T::result_type call(grpcxx::context &, const typename T::request_type &) {
return {grpcxx::status::code_t::unimplemented, std::nullopt};
}
};
template <>
rpcHello::result_type GreeterImpl::call<rpcHello>(
grpcxx::context &, const helloworld::v1::GreeterHelloRequest &req) {
helloworld::v1::GreeterHelloResponse res;
res.set_message("Hello `" + req.name() + "` 👋");
return {grpcxx::status::code_t::ok, res};
}
int main() {
GreeterImpl greeter;
Service service(greeter);
grpcxx::server server;
server.add(service);
std::printf("Listening on [127.0.0.1:7000] ...\n");
server.run("127.0.0.1", 7000);
return 0;
}