tutorial-11-trpc_pb_server.cc 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. Copyright (c) 2021 sogou, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include <signal.h>
  14. #include "helloworld.srpc.h"
  15. #include "workflow/WFFacilities.h"
  16. #include "srpc/rpc_types.h"
  17. using namespace srpc;
  18. using namespace trpc::test::helloworld;
  19. static WFFacilities::WaitGroup wait_group(1);
  20. class GreeterServiceImpl : public Greeter::Service
  21. {
  22. public:
  23. void SayHello(HelloRequest *req, HelloReply *resp, RPCContext *ctx) override
  24. {
  25. ctx->set_compress_type(RPCCompressGzip);
  26. resp->set_msg("This is SRPC framework TRPC protocol. Hello back.");
  27. printf("Server SayHello()\nget_req:\n%s\nset_resp:\n%s\n",
  28. req->DebugString().c_str(), resp->DebugString().c_str());
  29. }
  30. void SayHi(HelloRequest *req, HelloReply *resp, RPCContext *ctx) override
  31. {
  32. resp->set_msg("This is SRPC framework TRPC protocol. Hi back.");
  33. printf("Server SayHi()\nget_req:\n%s\nset_resp:\n%s\n",
  34. req->DebugString().c_str(), resp->DebugString().c_str());
  35. }
  36. };
  37. static void sig_handler(int signo)
  38. {
  39. wait_group.done();
  40. }
  41. int main()
  42. {
  43. GOOGLE_PROTOBUF_VERIFY_VERSION;
  44. signal(SIGINT, sig_handler);
  45. signal(SIGTERM, sig_handler);
  46. TRPCServer server;
  47. GreeterServiceImpl impl;
  48. server.add_service(&impl);
  49. if (server.start(1412) == 0)
  50. {
  51. printf("SRPC framework TRPC server is running on 1412\n"
  52. "Try ./trpc_pb_client to send requests.\n\n");
  53. wait_group.wait();
  54. server.stop();
  55. }
  56. else
  57. perror("server start");
  58. google::protobuf::ShutdownProtobufLibrary();
  59. return 0;
  60. }