1
0

jsonrpc_client.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * json rpc client
  3. *
  4. * @build make jsonrpc
  5. * @server bin/jsonrpc_server 1234
  6. * @client bin/jsonrpc_client 127.0.0.1 1234 add 1 2
  7. *
  8. */
  9. #include "hloop.h"
  10. #include "hatomic.h"
  11. #include "hbase.h"
  12. #include "hsocket.h"
  13. #include "jsonrpc.h"
  14. #include "cJSON.h"
  15. // hloop_create_tcp_client -> on_connect -> hio_write -> hio_read -> on_recv
  16. static int verbose = 0;
  17. static unpack_setting_t jsonrpc_unpack_setting;
  18. static void on_close(hio_t* io) {
  19. printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
  20. cJSON* jreq = (cJSON*)(hevent_userdata(io));
  21. if (jreq) {
  22. cJSON_Delete(jreq);
  23. hevent_set_userdata(io, NULL);
  24. }
  25. hloop_stop(hevent_loop(io));
  26. }
  27. static void on_recv(hio_t* io, void* readbuf, int readbytes) {
  28. // printf("on_recv fd=%d readbytes=%d\n", hio_fd(io), readbytes);
  29. if (verbose) {
  30. char localaddrstr[SOCKADDR_STRLEN] = {0};
  31. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  32. printf("[%s] <=> [%s]\n",
  33. SOCKADDR_STR(hio_localaddr(io), localaddrstr),
  34. SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
  35. }
  36. // unpack
  37. jsonrpc_message msg;
  38. memset(&msg, 0, sizeof(msg));
  39. int packlen = jsonrpc_unpack(&msg, readbuf, readbytes);
  40. if (packlen < 0) {
  41. printf("jsonrpc_unpack failed!\n");
  42. return;
  43. }
  44. assert(packlen == readbytes);
  45. printf("< %.*s\n", msg.head.length, msg.body);
  46. // cJSON_Parse
  47. cJSON* jres = cJSON_ParseWithLength(msg.body, msg.head.length);
  48. cJSON* jerror = cJSON_GetObjectItem(jres, "error");
  49. cJSON* jresult = cJSON_GetObjectItem(jres, "result");
  50. // ...
  51. cJSON_Delete(jres);
  52. hio_close(io);
  53. }
  54. static void on_connect(hio_t* io) {
  55. printf("on_connect fd=%d\n", hio_fd(io));
  56. if (verbose) {
  57. char localaddrstr[SOCKADDR_STRLEN] = {0};
  58. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  59. printf("connect connfd=%d [%s] => [%s]\n", hio_fd(io),
  60. SOCKADDR_STR(hio_localaddr(io), localaddrstr),
  61. SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
  62. }
  63. hio_setcb_read(io, on_recv);
  64. hio_set_unpack(io, &jsonrpc_unpack_setting);
  65. hio_read(io);
  66. cJSON* jreq = (cJSON*)(hevent_userdata(io));
  67. hevent_set_userdata(io, NULL);
  68. assert(jreq != NULL);
  69. // cJSON_Print -> pack -> hio_write
  70. jsonrpc_message msg;
  71. memset(&msg, 0, sizeof(msg));
  72. msg.body = cJSON_PrintUnformatted(jreq);
  73. msg.head.length = strlen(msg.body);
  74. printf("> %.*s\n", msg.head.length, msg.body);
  75. // pack
  76. unsigned int packlen = jsonrpc_package_length(&msg.head);
  77. unsigned char* writebuf = NULL;
  78. HV_ALLOC(writebuf, packlen);
  79. packlen = jsonrpc_pack(&msg, writebuf, packlen);
  80. if (packlen > 0) {
  81. hio_write(io, writebuf, packlen);
  82. }
  83. cJSON_Delete(jreq);
  84. cJSON_free((void*)msg.body);
  85. HV_FREE(writebuf);
  86. }
  87. static int jsonrpc_call(hloop_t* loop, const char* host, int port, const char* method, const char* param1, const char* param2) {
  88. hio_t* connio = hio_create(loop, host, port, SOCK_STREAM);
  89. if (connio == NULL) {
  90. return -1;
  91. }
  92. // printf("connfd=%d\n", hio_fd(connio));
  93. // construct request
  94. cJSON* jreq = cJSON_CreateObject();
  95. static hatomic_t s_id = HATOMIC_VAR_INIT(0);
  96. cJSON_AddItemToObject(jreq, "id", cJSON_CreateNumber(++s_id));
  97. cJSON_AddItemToObject(jreq, "method", cJSON_CreateString(method));
  98. cJSON* jparams = cJSON_CreateArray();
  99. cJSON_AddItemToArray(jparams, cJSON_CreateNumber(atoi(param1)));
  100. cJSON_AddItemToArray(jparams, cJSON_CreateNumber(atoi(param2)));
  101. cJSON_AddItemToObject(jreq, "params", jparams);
  102. hevent_set_userdata(connio, jreq);
  103. hio_setcb_connect(connio, on_connect);
  104. hio_setcb_close(connio, on_close);
  105. hio_connect(connio);
  106. return 0;
  107. }
  108. int main(int argc, char** argv) {
  109. if (argc < 6) {
  110. printf("Usage: %s host port method param1 param2\n", argv[0]);
  111. printf("method = [add, sub, mul, div]\n");
  112. printf("Examples:\n");
  113. printf(" %s 127.0.0.1 1234 add 1 2\n", argv[0]);
  114. printf(" %s 127.0.0.1 1234 div 1 0\n", argv[0]);
  115. return -10;
  116. }
  117. const char* host = argv[1];
  118. int port = atoi(argv[2]);
  119. const char* method = argv[3];
  120. const char* param1 = argv[4];
  121. const char* param2 = argv[5];
  122. // init jsonrpc_unpack_setting
  123. memset(&jsonrpc_unpack_setting, 0, sizeof(unpack_setting_t));
  124. jsonrpc_unpack_setting.mode = UNPACK_BY_LENGTH_FIELD;
  125. jsonrpc_unpack_setting.package_max_length = DEFAULT_PACKAGE_MAX_LENGTH;
  126. jsonrpc_unpack_setting.body_offset = JSONRPC_HEAD_LENGTH;
  127. jsonrpc_unpack_setting.length_field_offset = 1;
  128. jsonrpc_unpack_setting.length_field_bytes = 4;
  129. jsonrpc_unpack_setting.length_field_coding = ENCODE_BY_BIG_ENDIAN;
  130. hloop_t* loop = hloop_new(0);
  131. jsonrpc_call(loop, host, port, method, param1, param2);
  132. hloop_run(loop);
  133. hloop_free(&loop);
  134. return 0;
  135. }