server.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/nshead_mcpack_protocol.h>
  22. #include "echo.pb.h"
  23. DEFINE_int32(port, 8002, "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. DEFINE_int32(max_concurrency, 0, "Limit of request processing in parallel");
  29. DEFINE_int32(internal_port, -1, "Only allow builtin services at this port");
  30. namespace example {
  31. // Your implementation of EchoService
  32. class EchoServiceImpl : public EchoService {
  33. public:
  34. EchoServiceImpl() {}
  35. ~EchoServiceImpl() {};
  36. void Echo(google::protobuf::RpcController* cntl_base,
  37. const EchoRequest* request,
  38. EchoResponse* response,
  39. google::protobuf::Closure* done) {
  40. brpc::ClosureGuard done_guard(done);
  41. response->set_message(request->message());
  42. }
  43. };
  44. } // namespace example
  45. int main(int argc, char* argv[]) {
  46. // Parse gflags. We recommend you to use gflags as well.
  47. GFLAGS_NS::ParseCommandLineFlags(&argc, &argv, true);
  48. // Generally you only need one Server.
  49. brpc::Server server;
  50. // Instance of your service.
  51. example::EchoServiceImpl echo_service_impl;
  52. // Add the service into server. Notice the second parameter, because the
  53. // service is put on stack, we don't want server to delete it, otherwise
  54. // use brpc::SERVER_OWNS_SERVICE.
  55. if (server.AddService(&echo_service_impl,
  56. brpc::SERVER_DOESNT_OWN_SERVICE) != 0) {
  57. LOG(ERROR) << "Fail to add service";
  58. return -1;
  59. }
  60. // Start the server.
  61. brpc::ServerOptions options;
  62. options.idle_timeout_sec = FLAGS_idle_timeout_s;
  63. options.nshead_service = new brpc::policy::NsheadMcpackAdaptor;
  64. options.max_concurrency = FLAGS_max_concurrency;
  65. options.internal_port = FLAGS_internal_port;
  66. if (server.Start(FLAGS_port, &options) != 0) {
  67. LOG(ERROR) << "Fail to start EchoServer";
  68. return -1;
  69. }
  70. // Wait until Ctrl-C is pressed, then Stop() and Join() the server.
  71. server.RunUntilAskedToQuit();
  72. return 0;
  73. }