server.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "echo.pb.h"
  22. DEFINE_bool(send_attachment, true, "Carry attachment along with response");
  23. DEFINE_int32(port, 8003, "TCP Port of this server");
  24. DEFINE_int32(idle_timeout_s, -1, "Connection will be closed if there is no "
  25. "read/write operations during the last `idle_timeout_s'");
  26. DEFINE_int32(logoff_ms, 2000, "Maximum duration of server's LOGOFF state "
  27. "(waiting for client to close connection before server stops)");
  28. // Your implementation of example::EchoService
  29. class EchoServiceImpl : public example::EchoService {
  30. public:
  31. EchoServiceImpl() {};
  32. virtual ~EchoServiceImpl() {};
  33. virtual void Echo(google::protobuf::RpcController* cntl_base,
  34. const example::EchoRequest* request,
  35. example::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. LOG(INFO) << "Received request[log_id=" << cntl->log_id()
  46. << "] from " << cntl->remote_side()
  47. << ": " << request->message()
  48. << " (attached=" << cntl->request_attachment() << ")";
  49. // Fill response.
  50. response->set_message(request->message());
  51. // You can compress the response by setting Controller, but be aware
  52. // that compression may be costly, evaluate before turning on.
  53. // cntl->set_response_compress_type(brpc::COMPRESS_TYPE_GZIP);
  54. if (FLAGS_send_attachment) {
  55. // Set attachment which is wired to network directly instead of
  56. // being serialized into protobuf messages.
  57. cntl->response_attachment().append("bar");
  58. }
  59. }
  60. };
  61. int main(int argc, char* argv[]) {
  62. // Parse gflags. We recommend you to use gflags as well.
  63. GFLAGS_NS::ParseCommandLineFlags(&argc, &argv, true);
  64. // Generally you only need one Server.
  65. brpc::Server server;
  66. // Instance of your service.
  67. EchoServiceImpl echo_service_impl;
  68. // Add the service into server. Notice the second parameter, because the
  69. // service is put on stack, we don't want server to delete it, otherwise
  70. // use brpc::SERVER_OWNS_SERVICE.
  71. if (server.AddService(&echo_service_impl,
  72. brpc::SERVER_DOESNT_OWN_SERVICE) != 0) {
  73. LOG(ERROR) << "Fail to add service";
  74. return -1;
  75. }
  76. // Start the server.
  77. brpc::ServerOptions options;
  78. options.idle_timeout_sec = FLAGS_idle_timeout_s;
  79. if (server.Start(FLAGS_port, &options) != 0) {
  80. LOG(ERROR) << "Fail to start EchoServer";
  81. return -1;
  82. }
  83. // Wait until Ctrl-C is pressed, then Stop() and Join() the server.
  84. server.RunUntilAskedToQuit();
  85. return 0;
  86. }