server.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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/thrift_service.h>
  22. #include "gen-cpp/echo_types.h"
  23. DEFINE_int32(port, 8019, "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(max_concurrency, 0, "Limit of request processing in parallel");
  27. // Adapt your own thrift-based protocol to use brpc
  28. class EchoServiceImpl : public brpc::ThriftService {
  29. public:
  30. void ProcessThriftFramedRequest(brpc::Controller* cntl,
  31. brpc::ThriftFramedMessage* req,
  32. brpc::ThriftFramedMessage* res,
  33. google::protobuf::Closure* done) override {
  34. // Dispatch calls to different methods
  35. if (cntl->thrift_method_name() == "Echo") {
  36. return Echo(cntl, req->Cast<example::EchoRequest>(),
  37. res->Cast<example::EchoResponse>(), done);
  38. } else {
  39. cntl->SetFailed(brpc::ENOMETHOD, "Fail to find method=%s",
  40. cntl->thrift_method_name().c_str());
  41. done->Run();
  42. }
  43. }
  44. void Echo(brpc::Controller* cntl,
  45. const example::EchoRequest* req,
  46. example::EchoResponse* res,
  47. google::protobuf::Closure* done) {
  48. // This object helps you to call done->Run() in RAII style. If you need
  49. // to process the request asynchronously, pass done_guard.release().
  50. brpc::ClosureGuard done_guard(done);
  51. res->data = req->data + " (Echo)";
  52. }
  53. };
  54. int main(int argc, char* argv[]) {
  55. // Parse gflags. We recommend you to use gflags as well.
  56. google::ParseCommandLineFlags(&argc, &argv, true);
  57. brpc::Server server;
  58. brpc::ServerOptions options;
  59. options.thrift_service = new EchoServiceImpl;
  60. options.idle_timeout_sec = FLAGS_idle_timeout_s;
  61. options.max_concurrency = FLAGS_max_concurrency;
  62. // Start the server.
  63. if (server.Start(FLAGS_port, &options) != 0) {
  64. LOG(ERROR) << "Fail to start EchoServer";
  65. return -1;
  66. }
  67. // Wait until Ctrl-C is pressed, then Stop() and Join() the server.
  68. server.RunUntilAskedToQuit();
  69. return 0;
  70. }