| 1 |
/* |
| 2 |
* json rpc server |
| 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 "hbase.h" |
| 12 |
#include "hsocket.h" |
| 13 |
|
| 14 |
#include "cJSON.h" |
| 15 |
#include "router.h" |
| 16 |
#include "handler.h" |
| 17 |
|
| 18 |
// hloop_create_tcp_server -> on_accept -> hio_read -> on_recv -> hio_write |
| 19 |
|
| 20 |
static int verbose = 0; |
| 21 |
static unpack_setting_t jsonrpc_unpack_setting; |
| 22 |
|
| 23 |
jsonrpc_router router[] = { |
| 24 |
{"add", calc_add}, |
| 25 |
{"sub", calc_sub}, |
| 26 |
{"mul", calc_mul}, |
| 27 |
{"div", calc_div}, |
| 28 |
}; |
| 29 |
#define JSONRPC_ROUTER_NUM (sizeof(router)/sizeof(router[0])) |
| 30 |
|
| 31 |
static void on_close(hio_t* io) { |
| 32 |
printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io)); |
| 33 |
} |
| 34 |
|
| 35 |
static void on_recv(hio_t* io, void* readbuf, int readbytes) { |
| 36 |
// printf("on_recv fd=%d readbytes=%d\n", hio_fd(io), readbytes); |
| 37 |
if (verbose) { |
| 38 |
char localaddrstr[SOCKADDR_STRLEN] = {0}; |
| 39 |
char peeraddrstr[SOCKADDR_STRLEN] = {0}; |
| 40 |
printf("[%s] <=> [%s]\n", |
| 41 |
SOCKADDR_STR(hio_localaddr(io), localaddrstr), |
| 42 |
SOCKADDR_STR(hio_peeraddr(io), peeraddrstr)); |
| 43 |
} |
| 44 |
|
| 45 |
// cJSON_Parse -> router -> cJSON_Print -> hio_write |
| 46 |
char* req_str = (char*)readbuf; |
| 47 |
printf("> %s\n", req_str); |
| 48 |
cJSON* jreq = cJSON_Parse(req_str); |
| 49 |
cJSON* jres = cJSON_CreateObject(); |
| 50 |
cJSON* jid = cJSON_GetObjectItem(jreq, "id"); |
| 51 |
cJSON* jmethod = cJSON_GetObjectItem(jreq, "method"); |
| 52 |
if (cJSON_IsNumber(jid)) { |
| 53 |
long id = cJSON_GetNumberValue(jid); |
| 54 |
cJSON_AddItemToObject(jres, "id", cJSON_CreateNumber(id)); |
| 55 |
} |
| 56 |
if (cJSON_IsString(jmethod)) { |
| 57 |
// router |
| 58 |
char* method = cJSON_GetStringValue(jmethod); |
| 59 |
bool found = false; |
| 60 |
for (int i = 0; i < JSONRPC_ROUTER_NUM; ++i) { |
| 61 |
if (strcmp(method, router[i].method) == 0) { |
| 62 |
found = true; |
| 63 |
router[i].handler(jreq, jres); |
| 64 |
break; |
| 65 |
} |
| 66 |
} |
| 67 |
if (!found) { |
| 68 |
not_found(jreq, jres); |
| 69 |
} |
| 70 |
} else { |
| 71 |
bad_request(jreq, jres); |
| 72 |
} |
| 73 |
|
| 74 |
char* resp_str = cJSON_PrintUnformatted(jres); |
| 75 |
printf("< %s\n", resp_str); |
| 76 |
// NOTE: +1 for \0 |
| 77 |
hio_write(io, resp_str, strlen(resp_str) + 1); |
| 78 |
|
| 79 |
cJSON_Delete(jreq); |
| 80 |
cJSON_Delete(jres); |
| 81 |
cJSON_free(resp_str); |
| 82 |
} |
| 83 |
|
| 84 |
static void on_accept(hio_t* io) { |
| 85 |
printf("on_accept connfd=%d\n", hio_fd(io)); |
| 86 |
if (verbose) { |
| 87 |
char localaddrstr[SOCKADDR_STRLEN] = {0}; |
| 88 |
char peeraddrstr[SOCKADDR_STRLEN] = {0}; |
| 89 |
printf("accept connfd=%d [%s] <= [%s]\n", hio_fd(io), |
| 90 |
SOCKADDR_STR(hio_localaddr(io), localaddrstr), |
| 91 |
SOCKADDR_STR(hio_peeraddr(io), peeraddrstr)); |
| 92 |
} |
| 93 |
|
| 94 |
hio_setcb_close(io, on_close); |
| 95 |
hio_setcb_read(io, on_recv); |
| 96 |
hio_set_unpack(io, &jsonrpc_unpack_setting); |
| 97 |
hio_read(io); |
| 98 |
} |
| 99 |
|
| 100 |
int main(int argc, char** argv) { |
| 101 |
if (argc < 2) { |
| 102 |
printf("Usage: %s port\n", argv[0]); |
| 103 |
return -10; |
| 104 |
} |
| 105 |
int port = atoi(argv[1]); |
| 106 |
|
| 107 |
// init jsonrpc_unpack_setting |
| 108 |
memset(&jsonrpc_unpack_setting, 0, sizeof(unpack_setting_t)); |
| 109 |
jsonrpc_unpack_setting.mode = UNPACK_BY_DELIMITER; |
| 110 |
jsonrpc_unpack_setting.package_max_length = DEFAULT_PACKAGE_MAX_LENGTH; |
| 111 |
jsonrpc_unpack_setting.delimiter[0] = '\0'; |
| 112 |
jsonrpc_unpack_setting.delimiter_bytes = 1; |
| 113 |
|
| 114 |
hloop_t* loop = hloop_new(0); |
| 115 |
hio_t* listenio = hloop_create_tcp_server(loop, "0.0.0.0", port, on_accept); |
| 116 |
if (listenio == NULL) { |
| 117 |
return -20; |
| 118 |
} |
| 119 |
printf("listenfd=%d\n", hio_fd(listenio)); |
| 120 |
hloop_run(loop); |
| 121 |
hloop_free(&loop); |
| 122 |
return 0; |
| 123 |
} |