tutorial-01-srpc_pb_server.cc 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. Copyright (c) 2020 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 "echo_pb.srpc.h"
  15. #include "workflow/WFFacilities.h"
  16. #include "srpc/rpc_types.h"
  17. using namespace srpc;
  18. static WFFacilities::WaitGroup wait_group(1);
  19. class ExampleServiceImpl : public Example::Service
  20. {
  21. public:
  22. void Echo(EchoRequest *req, EchoResponse *resp, RPCContext *ctx) override
  23. {
  24. resp->set_message("Hi back");
  25. printf("Server Echo()\nget_req:\n%s\nset_resp:\n%s\n",
  26. req->DebugString().c_str(), resp->DebugString().c_str());
  27. }
  28. };
  29. static void sig_handler(int signo)
  30. {
  31. wait_group.done();
  32. }
  33. int main()
  34. {
  35. GOOGLE_PROTOBUF_VERIFY_VERSION;
  36. signal(SIGINT, sig_handler);
  37. signal(SIGTERM, sig_handler);
  38. SRPCServer server;
  39. ExampleServiceImpl impl;
  40. server.add_service(&impl);
  41. if (server.start(1412) == 0)
  42. {
  43. printf("SRPC framework SRPC Protobuf server is running on 1412\n"
  44. "Try ./srpc_pb_client to send requests.\n\n");
  45. wait_group.wait();
  46. server.stop();
  47. }
  48. else
  49. perror("server start");
  50. google::protobuf::ShutdownProtobufLibrary();
  51. return 0;
  52. }