-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathserver.cpp
More file actions
75 lines (63 loc) · 2.63 KB
/
Copy pathserver.cpp
File metadata and controls
75 lines (63 loc) · 2.63 KB
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
#include <gflags/gflags.h>
#include <butil/logging.h>
#include <brpc/server.h>
#include "test.brpc.fb.h"
DEFINE_bool(echo_attachment, true, "Echo attachment as well");
DEFINE_int32(port, 8080, "TCP Port of this server");
DEFINE_int32(idle_timeout_s, -1, "Connection will be closed if there is no "
"read/write operations during the last `idle_timeout_s'");
DEFINE_int32(max_concurrency, 0, "Limit of request processing in parallel");
DEFINE_int32(internal_port, -1, "Only allow builtin services at this port");
namespace test{
class BenchmarkServiceImpl : public BenchmarkService {
public:
BenchmarkServiceImpl() {}
~BenchmarkServiceImpl() {}
void Test(google::protobuf::RpcController* controller,
const brpc::flatbuffers::Message* request_base,
brpc::flatbuffers::Message* response,
google::protobuf::Closure* done) {
brpc::ClosureGuard done_guard(done);
brpc::Controller* cntl =
static_cast<brpc::Controller*>(controller);
const test::BenchmarkRequest* request = request_base->GetRoot<test::BenchmarkRequest>();
// Set Response Message
brpc::flatbuffers::MessageBuilder mb_;
const auto* msg = request->message();
const char* req_str = msg ? msg->c_str() : "";
auto message = mb_.CreateString(req_str);
auto resp = test::CreateBenchmarkResponse(mb_, request->opcode(),
request->echo_attachment(), request->attachment_size(),
request->request_id(),request->reserved(), message);
mb_.Finish(resp);
*response = mb_.ReleaseMessage();
if (FLAGS_echo_attachment) {
cntl->response_attachment().append(cntl->request_attachment());
}
}
};
}
int main(int argc, char* argv[]) {
GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true);
// Generally you only need one Server.
brpc::Server server;
// Instance of your service.
test::BenchmarkServiceImpl benchmark_service_impl;
if (server.AddService(&benchmark_service_impl,
brpc::SERVER_DOESNT_OWN_SERVICE) != 0) {
LOG(ERROR) << "Fail to add service";
return -1;
}
// Start the server.
brpc::ServerOptions options;
options.idle_timeout_sec = FLAGS_idle_timeout_s;
options.max_concurrency = FLAGS_max_concurrency;
options.internal_port = FLAGS_internal_port;
if (server.Start(FLAGS_port, &options) != 0) {
LOG(ERROR) << "Fail to start Server";
return -1;
}
// Wait until Ctrl-C is pressed, then Stop() and Join() the server.
server.RunUntilAskedToQuit();
return 0;
}