1
0

protorpc_server.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * proto rpc server
  3. *
  4. * @build make protorpc
  5. * @server bin/protorpc_server 1234
  6. * @client bin/protorpc_client 127.0.0.1 1234 add 1 2
  7. *
  8. */
  9. #include "TcpServer.h"
  10. using namespace hv;
  11. #include "protorpc.h"
  12. #include "router.h"
  13. #include "handler/handler.h"
  14. #include "handler/calc.h"
  15. #include "handler/login.h"
  16. protorpc_router router[] = {
  17. {"add", calc_add},
  18. {"sub", calc_sub},
  19. {"mul", calc_mul},
  20. {"div", calc_div},
  21. {"login", login},
  22. };
  23. #define PROTORPC_ROUTER_NUM (sizeof(router)/sizeof(router[0]))
  24. class ProtoRpcServer : public TcpServer {
  25. public:
  26. ProtoRpcServer() : TcpServer()
  27. {
  28. onConnection = [](const SocketChannelPtr& channel) {
  29. std::string peeraddr = channel->peeraddr();
  30. if (channel->isConnected()) {
  31. printf("%s connected! connfd=%d\n", peeraddr.c_str(), channel->fd());
  32. } else {
  33. printf("%s disconnected! connfd=%d\n", peeraddr.c_str(), channel->fd());
  34. }
  35. };
  36. onMessage = handleMessage;
  37. // init protorpc_unpack_setting
  38. unpack_setting_t protorpc_unpack_setting;
  39. memset(&protorpc_unpack_setting, 0, sizeof(unpack_setting_t));
  40. protorpc_unpack_setting.mode = UNPACK_BY_LENGTH_FIELD;
  41. protorpc_unpack_setting.package_max_length = DEFAULT_PACKAGE_MAX_LENGTH;
  42. protorpc_unpack_setting.body_offset = PROTORPC_HEAD_LENGTH;
  43. protorpc_unpack_setting.length_field_offset = 1;
  44. protorpc_unpack_setting.length_field_bytes = 4;
  45. protorpc_unpack_setting.length_field_coding = ENCODE_BY_BIG_ENDIAN;
  46. setUnpack(&protorpc_unpack_setting);
  47. }
  48. int listen(int port) { return createsocket(port); }
  49. private:
  50. static void handleMessage(const SocketChannelPtr& channel, Buffer* buf) {
  51. // unpack -> Request::ParseFromArray -> router -> Response::SerializeToArray -> pack -> Channel::write
  52. // protorpc_unpack
  53. protorpc_message msg;
  54. memset(&msg, 0, sizeof(msg));
  55. int packlen = protorpc_unpack(&msg, buf->data(), buf->size());
  56. if (packlen < 0) {
  57. printf("protorpc_unpack failed!\n");
  58. return;
  59. }
  60. assert(packlen == buf->size());
  61. // Request::ParseFromArray
  62. protorpc::Request req;
  63. protorpc::Response res;
  64. if (req.ParseFromArray(msg.body, msg.head.length)) {
  65. printf("> %s\n", req.DebugString().c_str());
  66. res.set_id(req.id());
  67. // router
  68. const char* method = req.method().c_str();
  69. bool found = false;
  70. for (int i = 0; i < PROTORPC_ROUTER_NUM; ++i) {
  71. if (strcmp(method, router[i].method) == 0) {
  72. found = true;
  73. router[i].handler(req, &res);
  74. break;
  75. }
  76. }
  77. if (!found) {
  78. not_found(req, &res);
  79. }
  80. } else {
  81. bad_request(req, &res);
  82. }
  83. // Response::SerializeToArray + protorpc_pack
  84. memset(&msg, 0, sizeof(msg));
  85. msg.head.length = res.ByteSizeLong();
  86. packlen = protorpc_package_length(&msg.head);
  87. unsigned char* writebuf = NULL;
  88. HV_ALLOC(writebuf, packlen);
  89. packlen = protorpc_pack(&msg, writebuf, packlen);
  90. if (packlen > 0) {
  91. printf("< %s\n", res.DebugString().c_str());
  92. res.SerializeToArray(writebuf + PROTORPC_HEAD_LENGTH, msg.head.length);
  93. channel->write(writebuf, packlen);
  94. }
  95. HV_FREE(writebuf);
  96. }
  97. };
  98. int main(int argc, char** argv) {
  99. if (argc < 2) {
  100. printf("Usage: %s port\n", argv[0]);
  101. return -10;
  102. }
  103. int port = atoi(argv[1]);
  104. ProtoRpcServer srv;
  105. int listenfd = srv.listen(port);
  106. if (listenfd < 0) {
  107. return -20;
  108. }
  109. printf("protorpc_server listen on port %d, listenfd=%d ...\n", port, listenfd);
  110. srv.setThreadNum(4);
  111. srv.start();
  112. while (1) hv_sleep(1);
  113. return 0;
  114. }