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