server.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. // A server to receive requests from ubrpc clients.
  18. // This server can be accessed by the client in public/baidu-rpc-ub/example/echo_c++_compack_ubrpc as well.
  19. #include <gflags/gflags.h>
  20. #include <butil/logging.h>
  21. #include <brpc/server.h>
  22. #include <brpc/policy/ubrpc2pb_protocol.h>
  23. #include "echo.pb.h"
  24. DEFINE_int32(port, 8500, "TCP Port of this server");
  25. DEFINE_int32(idle_timeout_s, -1, "Connection will be closed if there is no "
  26. "read/write operations during the last `idle_timeout_s'");
  27. DEFINE_int32(logoff_ms, 2000, "Maximum duration of server's LOGOFF state "
  28. "(waiting for client to close connection before server stops)");
  29. // Your implementation of EchoService
  30. namespace example {
  31. class EchoServiceImpl : public EchoService {
  32. public:
  33. EchoServiceImpl() {};
  34. virtual ~EchoServiceImpl() {};
  35. virtual void Echo(google::protobuf::RpcController* cntl_base,
  36. const EchoRequest* request,
  37. EchoResponse* response,
  38. google::protobuf::Closure* done) {
  39. // This object helps you to call done->Run() in RAII style. If you need
  40. // to process the request asynchronously, pass done_guard.release().
  41. brpc::ClosureGuard done_guard(done);
  42. brpc::Controller* cntl =
  43. static_cast<brpc::Controller*>(cntl_base);
  44. LOG(INFO) << "Received request[log_id=" << cntl->log_id()
  45. << "] from " << cntl->remote_side()
  46. << " to " << cntl->local_side()
  47. << ": " << request->DebugString();
  48. // Fill response.
  49. response->set_message(request->message());
  50. // the idl method returns void, no need to set_idl_result().
  51. }
  52. virtual void EchoWithMultiArgs(
  53. google::protobuf::RpcController* cntl_base,
  54. const MultiRequests* request,
  55. MultiResponses* response,
  56. google::protobuf::Closure* done) {
  57. // This object helps you to call done->Run() in RAII style. If you need
  58. // to process the request asynchronously, pass done_guard.release().
  59. brpc::ClosureGuard done_guard(done);
  60. brpc::Controller* cntl =
  61. static_cast<brpc::Controller*>(cntl_base);
  62. LOG(INFO) << "Received request[log_id=" << cntl->log_id()
  63. << "] from " << cntl->remote_side()
  64. << " to " << cntl->local_side()
  65. << ": req1=" << request->req1().message()
  66. << " req2=" << request->req2().message();
  67. // Fill response.
  68. response->mutable_res1()->set_message(request->req1().message());
  69. response->mutable_res2()->set_message(request->req2().message());
  70. // tell RPC that the idl method have more than one request/response.
  71. cntl->set_idl_names(brpc::idl_multi_req_multi_res);
  72. // the idl method returns uint32_t, we need to set it.
  73. cntl->set_idl_result(17);
  74. }
  75. };
  76. } // namespace
  77. int main(int argc, char* argv[]) {
  78. // Parse gflags. We recommend you to use gflags as well.
  79. GFLAGS_NS::ParseCommandLineFlags(&argc, &argv, true);
  80. // Generally you only need one Server.
  81. brpc::Server server;
  82. // Instance of your service.
  83. example::EchoServiceImpl echo_service_impl;
  84. // Add the service into server. Notice the second parameter, because the
  85. // service is put on stack, we don't want server to delete it, otherwise
  86. // use brpc::SERVER_OWNS_SERVICE.
  87. if (server.AddService(&echo_service_impl,
  88. brpc::SERVER_DOESNT_OWN_SERVICE) != 0) {
  89. LOG(ERROR) << "Fail to add service";
  90. return -1;
  91. }
  92. // Start the server.
  93. brpc::ServerOptions options;
  94. options.idle_timeout_sec = FLAGS_idle_timeout_s;
  95. options.nshead_service = new brpc::policy::UbrpcCompackAdaptor;
  96. if (server.Start(FLAGS_port, &options) != 0) {
  97. LOG(ERROR) << "Fail to start EchoServer";
  98. return -1;
  99. }
  100. // Wait until Ctrl-C is pressed, then Stop() and Join() the server.
  101. server.RunUntilAskedToQuit();
  102. return 0;
  103. }