1
0
ithewei 4 жил өмнө
parent
commit
900833f832

+ 4 - 4
examples/jsonrpc/handler.h

@@ -18,7 +18,7 @@ void bad_request(cJSON* jreq, cJSON* jres) {
     error_response(jres, 400, "Bad Request");
 }
 
-void do_add(cJSON* jreq, cJSON* jres) {
+void calc_add(cJSON* jreq, cJSON* jres) {
     cJSON* jparams = cJSON_GetObjectItem(jreq, "params");
     if (cJSON_GetArraySize(jparams) != 2) {
         return bad_request(jreq, jres);
@@ -31,7 +31,7 @@ void do_add(cJSON* jreq, cJSON* jres) {
     cJSON_AddItemToObject(jres, "result", cJSON_CreateNumber(result));
 }
 
-void do_sub(cJSON* jreq, cJSON* jres) {
+void calc_sub(cJSON* jreq, cJSON* jres) {
     cJSON* jparams = cJSON_GetObjectItem(jreq, "params");
     if (cJSON_GetArraySize(jparams) != 2) {
         return bad_request(jreq, jres);
@@ -44,7 +44,7 @@ void do_sub(cJSON* jreq, cJSON* jres) {
     cJSON_AddItemToObject(jres, "result", cJSON_CreateNumber(result));
 }
 
-void do_mul(cJSON* jreq, cJSON* jres) {
+void calc_mul(cJSON* jreq, cJSON* jres) {
     cJSON* jparams = cJSON_GetObjectItem(jreq, "params");
     if (cJSON_GetArraySize(jparams) != 2) {
         return bad_request(jreq, jres);
@@ -57,7 +57,7 @@ void do_mul(cJSON* jreq, cJSON* jres) {
     cJSON_AddItemToObject(jres, "result", cJSON_CreateNumber(result));
 }
 
-void do_div(cJSON* jreq, cJSON* jres) {
+void calc_div(cJSON* jreq, cJSON* jres) {
     cJSON* jparams = cJSON_GetObjectItem(jreq, "params");
     if (cJSON_GetArraySize(jparams) != 2) {
         return bad_request(jreq, jres);

+ 21 - 26
examples/jsonrpc/jsonrpc_client.c

@@ -8,6 +8,7 @@
  */
 
 #include "hloop.h"
+#include "hatomic.h"
 #include "hbase.h"
 #include "hsocket.h"
 
@@ -19,18 +20,15 @@
 static int verbose = 0;
 static unpack_setting_t jsonrpc_unpack_setting;
 
-typedef struct  {
-    char method[64];
-    char params[2][64];
-} jsonrpc_call_data;
-
 static void on_close(hio_t* io) {
     printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
-    void* userdata = hevent_userdata(io);
-    if (userdata) {
-        HV_FREE(userdata);
+    cJSON* jreq = (cJSON*)(hevent_userdata(io));
+    if (jreq) {
+        cJSON_Delete(jreq);
         hevent_set_userdata(io, NULL);
     }
+
+    hloop_stop(hevent_loop(io));
 }
 
 static void on_recv(hio_t* io, void* readbuf, int readbytes) {
@@ -60,7 +58,8 @@ static void on_recv(hio_t* io, void* readbuf, int readbytes) {
     cJSON* jresult = cJSON_GetObjectItem(jres, "result");
     // ...
     cJSON_Delete(jres);
-    hloop_stop(hevent_loop(io));
+
+    hio_close(io);
 }
 
 static void on_connect(hio_t* io) {
@@ -77,17 +76,11 @@ static void on_connect(hio_t* io) {
     hio_set_unpack(io, &jsonrpc_unpack_setting);
     hio_read(io);
 
-    jsonrpc_call_data* rpc_data = (jsonrpc_call_data*)hevent_userdata(io);
-    assert(rpc_data != NULL);
+    cJSON* jreq = (cJSON*)(hevent_userdata(io));
+    hevent_set_userdata(io, NULL);
+    assert(jreq != NULL);
 
     // cJSON_Print -> pack -> hio_write
-    cJSON* jreq = cJSON_CreateObject();
-    cJSON_AddItemToObject(jreq, "method", cJSON_CreateString(rpc_data->method));
-    cJSON* jparams = cJSON_CreateArray();
-    cJSON_AddItemToArray(jparams, cJSON_CreateNumber(atoi(rpc_data->params[0])));
-    cJSON_AddItemToArray(jparams, cJSON_CreateNumber(atoi(rpc_data->params[1])));
-    cJSON_AddItemToObject(jreq, "params", jparams);
-
     jsonrpc_message msg;
     memset(&msg, 0, sizeof(msg));
     msg.body = cJSON_PrintUnformatted(jreq);
@@ -106,8 +99,6 @@ static void on_connect(hio_t* io) {
     cJSON_Delete(jreq);
     cJSON_free((void*)msg.body);
     HV_FREE(writebuf);
-    HV_FREE(rpc_data);
-    hevent_set_userdata(io, NULL);
 }
 
 static int jsonrpc_call(hloop_t* loop, const char* host, int port, const char* method, const char* param1, const char* param2) {
@@ -117,12 +108,16 @@ static int jsonrpc_call(hloop_t* loop, const char* host, int port, const char* m
     }
     // printf("connfd=%d\n", hio_fd(connio));
 
-    jsonrpc_call_data* rpc_data = NULL;
-    HV_ALLOC_SIZEOF(rpc_data);
-    strcpy(rpc_data->method, method);
-    strcpy(rpc_data->params[0], param1);
-    strcpy(rpc_data->params[1], param2);
-    hevent_set_userdata(connio, rpc_data);
+    // construct request
+    cJSON* jreq = cJSON_CreateObject();
+    static hatomic_t s_id = HATOMIC_VAR_INIT(0);
+    cJSON_AddItemToObject(jreq, "id", cJSON_CreateNumber(++s_id));
+    cJSON_AddItemToObject(jreq, "method", cJSON_CreateString(method));
+    cJSON* jparams = cJSON_CreateArray();
+    cJSON_AddItemToArray(jparams, cJSON_CreateNumber(atoi(param1)));
+    cJSON_AddItemToArray(jparams, cJSON_CreateNumber(atoi(param2)));
+    cJSON_AddItemToObject(jreq, "params", jparams);
+    hevent_set_userdata(connio, jreq);
 
     hio_setcb_connect(connio, on_connect);
     hio_setcb_close(connio, on_close);

+ 12 - 7
examples/jsonrpc/jsonrpc_server.c

@@ -22,10 +22,10 @@ static int verbose = 0;
 static unpack_setting_t jsonrpc_unpack_setting;
 
 jsonrpc_router router[] = {
-    {"add", do_add},
-    {"sub", do_sub},
-    {"mul", do_mul},
-    {"div", do_div},
+    {"add", calc_add},
+    {"sub", calc_sub},
+    {"mul", calc_mul},
+    {"div", calc_div},
 };
 #define JSONRPC_ROUTER_NUM  (sizeof(router)/sizeof(router[0]))
 
@@ -58,10 +58,13 @@ static void on_recv(hio_t* io, void* readbuf, int readbytes) {
     printf("> %.*s\n", msg.head.length, msg.body);
     cJSON* jreq = cJSON_ParseWithLength(msg.body, msg.head.length);
     cJSON* jres = cJSON_CreateObject();
+    cJSON* jid = cJSON_GetObjectItem(jreq, "id");
     cJSON* jmethod = cJSON_GetObjectItem(jreq, "method");
-    if (!jmethod || !cJSON_IsString(jmethod)) {
-        bad_request(jreq, jres);
-    } else {
+    if (cJSON_IsNumber(jid)) {
+        long id = cJSON_GetNumberValue(jid);
+        cJSON_AddItemToObject(jres, "id", cJSON_CreateNumber(id));
+    }
+    if (cJSON_IsString(jmethod)) {
         // router
         char* method = cJSON_GetStringValue(jmethod);
         bool found = false;
@@ -75,6 +78,8 @@ static void on_recv(hio_t* io, void* readbuf, int readbytes) {
         if (!found) {
             not_found(jreq, jres);
         }
+    } else {
+        bad_request(jreq, jres);
     }
 
     // cJSON_Print

+ 5 - 4
examples/jsonrpc/router.h

@@ -10,12 +10,13 @@ typedef struct {
     jsonrpc_handler handler;
 } jsonrpc_router;
 
+void error_response(cJSON* jres, int code, const char* message);
 void not_found(cJSON* jreq, cJSON* jres);
 void bad_request(cJSON* jreq, cJSON* jres);
 
-void do_add(cJSON* jreq, cJSON* jres);
-void do_sub(cJSON* jreq, cJSON* jres);
-void do_mul(cJSON* jreq, cJSON* jres);
-void do_div(cJSON* jreq, cJSON* jres);
+void calc_add(cJSON* jreq, cJSON* jres);
+void calc_sub(cJSON* jreq, cJSON* jres);
+void calc_mul(cJSON* jreq, cJSON* jres);
+void calc_div(cJSON* jreq, cJSON* jres);
 
 #endif // HV_JSON_RPC_ROUTER_H_