server.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include <string>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <signal.h>
  5. #include <vector>
  6. #include <string>
  7. #include "echo.srpc.h"
  8. #include "msg.srpc.h"
  9. using namespace srpc;
  10. using namespace example;
  11. class ExamplePBServiceImpl : public ExamplePB::Service
  12. {
  13. public:
  14. void Echo(EchoRequest *request, EchoResponse *response, RPCContext *ctx) override
  15. {
  16. /*
  17. ctx->set_compress_type(RPCCompressGzip);
  18. char buf[100000];
  19. for (int i = 0; i < 100000; i++)
  20. buf[i] = 'a';
  21. buf[99999] = '\0';
  22. response->set_message(buf);
  23. */
  24. response->set_message("Hi back");
  25. // printf("Server Echo()\nget_req:\n%s\nset_resp:\n%s\n",
  26. // request->DebugString().c_str(),
  27. // response->DebugString().c_str());
  28. }
  29. };
  30. class ExampleThriftServiceImpl : public ExampleThrift::Service
  31. {
  32. /*
  33. public:
  34. void Message(MessageResult& _return, const std::string& message, const std::string& name)
  35. {
  36. _return.message = "Hi back";
  37. }*/
  38. public:
  39. void Message(ExampleThrift::MessageRequest *request, ExampleThrift::MessageResponse *response, RPCContext *ctx) override
  40. {
  41. //ctx->set_data_type(RPCDataJson);
  42. ctx->set_compress_type(RPCCompressGzip);
  43. char buf[10000];
  44. for (int i = 0; i < 10000; i++)
  45. buf[i] = 'a';
  46. buf[9999] = '\0';
  47. response->result.message = buf;
  48. // response->result.message = "Hi back";
  49. //response->result.__isset.arr = true;
  50. //response->result.state = 1;
  51. //response->result.__isset.state = true;
  52. //response->result.error = 0;
  53. //response->result.__isset.error = true;
  54. // printf("Server Echo()\nget_req:\n%s\nset_resp:\n%s\n",
  55. // request->message.c_str(),
  56. // response->result.message.c_str());
  57. }
  58. };
  59. static void sig_handler(int signo)
  60. {
  61. }
  62. template<class SERVER>
  63. static void run_server(unsigned short port)
  64. {
  65. RPCServerParams params = RPC_SERVER_PARAMS_DEFAULT;
  66. params.max_connections = 2048;
  67. SERVER server(&params);
  68. ExamplePBServiceImpl pb_impl;
  69. ExampleThriftServiceImpl thrift_impl;
  70. server.add_service(&pb_impl);
  71. server.add_service(&thrift_impl);
  72. // some server options
  73. if (server.start(port) == 0)
  74. {
  75. pause();
  76. server.stop();
  77. }
  78. else
  79. perror("server start");
  80. }
  81. template<class SERVER>
  82. static void run_thrift_server(unsigned short port)
  83. {
  84. RPCServerParams params = RPC_SERVER_PARAMS_DEFAULT;
  85. params.max_connections = 2048;
  86. SERVER server(&params);
  87. ExampleThriftServiceImpl thrift_impl;
  88. server.add_service(&thrift_impl);
  89. // some server options
  90. if (server.start(port) == 0)
  91. {
  92. pause();
  93. server.stop();
  94. }
  95. else
  96. perror("server start");
  97. }
  98. int main(int argc, char* argv[])
  99. {
  100. GOOGLE_PROTOBUF_VERIFY_VERSION;
  101. if (argc != 3)
  102. {
  103. fprintf(stderr, "Usage: %s <PORT> <srpc|brpc|thrift>\n", argv[0]);
  104. return 0;
  105. }
  106. signal(SIGINT, sig_handler);
  107. signal(SIGTERM, sig_handler);
  108. WFGlobalSettings my = GLOBAL_SETTINGS_DEFAULT;
  109. my.poller_threads = 16;
  110. my.handler_threads = 16;
  111. WORKFLOW_library_init(&my);
  112. unsigned short port = atoi(argv[1]);
  113. std::string server_type = argv[2];
  114. if (server_type == "srpc")
  115. run_server<SRPCServer>(port);
  116. else if (server_type == "brpc")
  117. run_server<BRPCServer>(port);
  118. else if (server_type == "thrift")
  119. run_thrift_server<ThriftServer>(port);
  120. else if (server_type == "srpc_http")
  121. run_server<SRPCHttpServer>(port);
  122. else if (server_type == "thrift_http")
  123. run_thrift_server<ThriftHttpServer>(port);
  124. else
  125. abort();
  126. google::protobuf::ShutdownProtobufLibrary();
  127. return 0;
  128. }