-
Notifications
You must be signed in to change notification settings - Fork 0
/
arith_server.cpp
98 lines (78 loc) · 2.09 KB
/
arith_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
//
// Copyright (c) 2023-present DeepGrace (complex dot invoke at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/deepgrace/srpc
//
#define BOOST_ASIO_HAS_IO_URING
#define BOOST_ASIO_DISABLE_EPOLL
#include <iostream>
#include <srpc.hpp>
#include <arith.pb.h>
namespace net = boost::asio;
namespace gp = google::protobuf;
void done()
{
std::cout << "got called" << std::endl << std::endl;
}
class service : public pb::service
{
public:
service()
{
}
void compute(gp::RpcController* controller, const pb::request* request, pb::response* response, gp::Closure* done)
{
auto op = request->op();
auto lhs = request->lhs();
auto rhs = request->rhs();
int64_t value = 0;
std::cout << "request: " << request->DebugString();
switch (op)
{
case pb::Add:
value = lhs + rhs;
break;
case pb::Sub:
value = lhs - rhs;
break;
case pb::Mul:
value = lhs * rhs;
break;
case pb::Div:
if (rhs == 0)
controller->SetFailed("divisor can't be 0");
else
value = lhs / rhs;
break;
default:
controller->SetFailed("out of operation");
break;
}
response->set_value(value);
std::cout << "response " << response->DebugString();
done->Run();
}
~service()
{
}
};
int main(int argc, char* argv[])
{
if (argc != 3)
{
std::cout << "Usage: " << argv[0] << " <host> <port>" << std::endl;
return 1;
}
std::string host(argv[1]);
std::string port(argv[2]);
snp::asio_context ctx;
service s;
srpc::server server(ctx, host, port);
server.register_service(&s, gp::NewPermanentCallback(&done));
server.run();
ctx.run();
return 0;
}