server.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 EchoRequest and send back EchoResponse.
  18. #include <gflags/gflags.h>
  19. #include <butil/logging.h>
  20. #include <brpc/server.h>
  21. #include <brpc/policy/hulu_pbrpc_controller.h>
  22. #include "echo.pb.h"
  23. DEFINE_bool(echo_attachment, true, "Echo attachment as well");
  24. DEFINE_int32(port, 8000, "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 example::EchoService
  30. // Notice that implementing brpc::Describable grants the ability to put
  31. // additional information in /status.
  32. namespace example {
  33. class EchoServiceImpl : public EchoService {
  34. public:
  35. EchoServiceImpl() {};
  36. virtual ~EchoServiceImpl() {};
  37. virtual void Echo(google::protobuf::RpcController* cntl_base,
  38. const EchoRequest* request,
  39. EchoResponse* response,
  40. google::protobuf::Closure* done) {
  41. // This object helps you to call done->Run() in RAII style. If you need
  42. // to process the request asynchronously, pass done_guard.release().
  43. brpc::ClosureGuard done_guard(done);
  44. brpc::Controller* cntl =
  45. static_cast<brpc::Controller*>(cntl_base);
  46. // The purpose of following logs is to help you to understand
  47. // how clients interact with servers more intuitively. You should
  48. // remove these logs in performance-sensitive servers.
  49. LOG(INFO) << "Received request[log_id=" << cntl->log_id()
  50. << "] from " << cntl->remote_side()
  51. << " to " << cntl->local_side()
  52. << ": " << request->message()
  53. << " (attached=" << cntl->request_attachment() << ")";
  54. brpc::policy::HuluController* hulu_controller
  55. = dynamic_cast<brpc::policy::HuluController*>(cntl);
  56. if (hulu_controller) {
  57. LOG(INFO) << "source_addr="
  58. << hulu_controller->request_source_addr()
  59. << " user_data=\"" << hulu_controller->request_user_data()
  60. << '\"';
  61. }
  62. // Fill response.
  63. response->set_message(request->message());
  64. // You can compress the response by setting Controller, but be aware
  65. // that compression may be costly, evaluate before turning on.
  66. // cntl->set_response_compress_type(brpc::COMPRESS_TYPE_GZIP);
  67. if (FLAGS_echo_attachment) {
  68. // Set attachment which is wired to network directly instead of
  69. // being serialized into protobuf messages.
  70. cntl->response_attachment().append(cntl->request_attachment());
  71. }
  72. if (hulu_controller) {
  73. hulu_controller->set_response_source_addr(
  74. hulu_controller->request_source_addr() + 1);
  75. hulu_controller->set_response_user_data("server user data");
  76. }
  77. }
  78. };
  79. } // namespace example
  80. int main(int argc, char* argv[]) {
  81. // Parse gflags. We recommend you to use gflags as well.
  82. GFLAGS_NS::ParseCommandLineFlags(&argc, &argv, true);
  83. // Generally you only need one Server.
  84. brpc::Server server;
  85. // Instance of your service.
  86. example::EchoServiceImpl echo_service_impl;
  87. // Add the service into server. Notice the second parameter, because the
  88. // service is put on stack, we don't want server to delete it, otherwise
  89. // use brpc::SERVER_OWNS_SERVICE.
  90. if (server.AddService(&echo_service_impl,
  91. brpc::SERVER_DOESNT_OWN_SERVICE) != 0) {
  92. LOG(ERROR) << "Fail to add service";
  93. return -1;
  94. }
  95. // Start the server.
  96. brpc::ServerOptions options;
  97. options.idle_timeout_sec = FLAGS_idle_timeout_s;
  98. if (server.Start(FLAGS_port, &options) != 0) {
  99. LOG(ERROR) << "Fail to start EchoServer";
  100. return -1;
  101. }
  102. // Wait until Ctrl-C is pressed, then Stop() and Join() the server.
  103. server.RunUntilAskedToQuit();
  104. return 0;
  105. }