server.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 "echo.pb.h"
  19. DEFINE_bool(echo_attachment, true, "Echo attachment as well");
  20. DEFINE_int32(port, 8000, "TCP Port of this server");
  21. DEFINE_int32(idle_timeout_s, -1, "Connection will be closed if there is no "
  22. "read/write operations during the last `idle_timeout_s'");
  23. DEFINE_int32(logoff_ms, 2000, "Maximum duration of server's LOGOFF state "
  24. "(waiting for client to close connection before server stops)");
  25. // Your implementation of example::EchoService
  26. // Notice that implementing brpc::Describable grants the ability to put
  27. // additional information in /status.
  28. namespace example {
  29. class EchoServiceImpl : public EchoService {
  30. public:
  31. EchoServiceImpl() {};
  32. virtual ~EchoServiceImpl() {};
  33. virtual void Echo(google::protobuf::RpcController* cntl_base,
  34. const EchoRequest* request,
  35. EchoResponse* response,
  36. google::protobuf::Closure* done) {
  37. // This object helps you to call done->Run() in RAII style. If you need
  38. // to process the request asynchronously, pass done_guard.release().
  39. brpc::ClosureGuard done_guard(done);
  40. brpc::Controller* cntl =
  41. static_cast<brpc::Controller*>(cntl_base);
  42. // The purpose of following logs is to help you to understand
  43. // how clients interact with servers more intuitively. You should
  44. // remove these logs in performance-sensitive servers.
  45. // You should also noticed that these logs are different from what
  46. // we wrote in other projects: they use << instead of printf-style
  47. // functions. But don't worry, these logs are fully compatible with
  48. // comlog. You can mix them with comlog or ullog functions freely.
  49. // The noflush prevents the log from being flushed immediately.
  50. LOG(INFO) << "Received request[log_id=" << cntl->log_id()
  51. << "] from " << cntl->remote_side()
  52. << " to " << cntl->local_side() << noflush;
  53. LOG(INFO) << ": " << request->message() << noflush;
  54. if (!cntl->request_attachment().empty()) {
  55. LOG(INFO) << " (attached=" << cntl->request_attachment() << ")" << noflush;
  56. }
  57. LOG(INFO);
  58. // Fill response.
  59. response->set_message(request->message());
  60. // You can compress the response by setting Controller, but be aware
  61. // that compression may be costly, evaluate before turning on.
  62. // cntl->set_response_compress_type(brpc::COMPRESS_TYPE_GZIP);
  63. if (FLAGS_echo_attachment) {
  64. // Set attachment which is wired to network directly instead of
  65. // being serialized into protobuf messages.
  66. cntl->response_attachment().append(cntl->request_attachment());
  67. }
  68. }
  69. };
  70. } // namespace example
  71. int main(int argc, char* argv[]) {
  72. // Parse gflags. We recommend you to use gflags as well.
  73. google::ParseCommandLineFlags(&argc, &argv, true);
  74. // Generally you only need one Server.
  75. brpc::Server server;
  76. // Instance of your service.
  77. example::EchoServiceImpl echo_service_impl;
  78. // Add the service into server. Notice the second parameter, because the
  79. // service is put on stack, we don't want server to delete it, otherwise
  80. // use brpc::SERVER_OWNS_SERVICE.
  81. if (server.AddService(&echo_service_impl,
  82. brpc::SERVER_DOESNT_OWN_SERVICE) != 0) {
  83. LOG(ERROR) << "Fail to add service";
  84. return -1;
  85. }
  86. // Start the server.
  87. brpc::ServerOptions options;
  88. options.idle_timeout_sec = FLAGS_idle_timeout_s;
  89. if (server.Start(FLAGS_port, &options) != 0) {
  90. LOG(ERROR) << "Fail to start EchoServer";
  91. return -1;
  92. }
  93. // Wait until Ctrl-C is pressed, then Stop() and Join() the server.
  94. server.RunUntilAskedToQuit();
  95. return 0;
  96. }