server.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (c) 2014 Baidu, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // A server to receive EchoRequest and send back EchoResponse.
  15. #include <gflags/gflags.h>
  16. #include <butil/logging.h>
  17. #include <brpc/server.h>
  18. #include <brpc/policy/hulu_pbrpc_controller.h>
  19. #include "echo.pb.h"
  20. DEFINE_bool(echo_attachment, true, "Echo attachment as well");
  21. DEFINE_int32(port, 8000, "TCP Port of this server");
  22. DEFINE_int32(idle_timeout_s, -1, "Connection will be closed if there is no "
  23. "read/write operations during the last `idle_timeout_s'");
  24. DEFINE_int32(logoff_ms, 2000, "Maximum duration of server's LOGOFF state "
  25. "(waiting for client to close connection before server stops)");
  26. // Your implementation of example::EchoService
  27. // Notice that implementing brpc::Describable grants the ability to put
  28. // additional information in /status.
  29. namespace example {
  30. class EchoServiceImpl : public EchoService {
  31. public:
  32. EchoServiceImpl() {};
  33. virtual ~EchoServiceImpl() {};
  34. virtual void Echo(google::protobuf::RpcController* cntl_base,
  35. const EchoRequest* request,
  36. EchoResponse* response,
  37. google::protobuf::Closure* done) {
  38. // This object helps you to call done->Run() in RAII style. If you need
  39. // to process the request asynchronously, pass done_guard.release().
  40. brpc::ClosureGuard done_guard(done);
  41. brpc::Controller* cntl =
  42. static_cast<brpc::Controller*>(cntl_base);
  43. // The purpose of following logs is to help you to understand
  44. // how clients interact with servers more intuitively. You should
  45. // remove these logs in performance-sensitive servers.
  46. // You should also noticed that these logs are different from what
  47. // we wrote in other projects: they use << instead of printf-style
  48. // functions. But don't worry, these logs are fully compatible with
  49. // comlog. You can mix them with comlog or ullog functions freely.
  50. // The noflush prevents the log from being flushed immediately.
  51. LOG(INFO) << "Received request[log_id=" << cntl->log_id()
  52. << "] from " << cntl->remote_side()
  53. << " to " << cntl->local_side() << noflush;
  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. << '\"' << noflush;
  61. }
  62. LOG(INFO) << ": " << request->message() << noflush;
  63. if (!cntl->request_attachment().empty()) {
  64. LOG(INFO) << " (attached=" << cntl->request_attachment() << ")" << noflush;
  65. }
  66. LOG(INFO);
  67. // Fill response.
  68. response->set_message(request->message());
  69. // You can compress the response by setting Controller, but be aware
  70. // that compression may be costly, evaluate before turning on.
  71. // cntl->set_response_compress_type(brpc::COMPRESS_TYPE_GZIP);
  72. if (FLAGS_echo_attachment) {
  73. // Set attachment which is wired to network directly instead of
  74. // being serialized into protobuf messages.
  75. cntl->response_attachment().append(cntl->request_attachment());
  76. }
  77. if (hulu_controller) {
  78. hulu_controller->set_response_source_addr(
  79. hulu_controller->request_source_addr() + 1);
  80. hulu_controller->set_response_user_data("server user data");
  81. }
  82. }
  83. };
  84. } // namespace example
  85. int main(int argc, char* argv[]) {
  86. // Parse gflags. We recommend you to use gflags as well.
  87. google::ParseCommandLineFlags(&argc, &argv, true);
  88. // Generally you only need one Server.
  89. brpc::Server server;
  90. // Instance of your service.
  91. example::EchoServiceImpl echo_service_impl;
  92. // Add the service into server. Notice the second parameter, because the
  93. // service is put on stack, we don't want server to delete it, otherwise
  94. // use brpc::SERVER_OWNS_SERVICE.
  95. if (server.AddService(&echo_service_impl,
  96. brpc::SERVER_DOESNT_OWN_SERVICE) != 0) {
  97. LOG(ERROR) << "Fail to add service";
  98. return -1;
  99. }
  100. // Start the server.
  101. brpc::ServerOptions options;
  102. options.idle_timeout_sec = FLAGS_idle_timeout_s;
  103. if (server.Start(FLAGS_port, &options) != 0) {
  104. LOG(ERROR) << "Fail to start EchoServer";
  105. return -1;
  106. }
  107. // Wait until Ctrl-C is pressed, then Stop() and Join() the server.
  108. server.RunUntilAskedToQuit();
  109. return 0;
  110. }