Skip to content

Commit 423f3d2

Browse files
author
ithewei
committed
Update jsonrpc protorpc
1 parent 14b199c commit 423f3d2

12 files changed

+102
-169
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ websocket_client_test: prepare
149149
jsonrpc: jsonrpc_client jsonrpc_server
150150

151151
jsonrpc_client: prepare
152-
$(MAKEF) TARGET=$@ SRCDIRS="$(CORE_SRCDIRS)" SRCS="examples/jsonrpc/jsonrpc_client.c examples/jsonrpc/jsonrpc.c examples/jsonrpc/cJSON.c"
152+
$(MAKEF) TARGET=$@ SRCDIRS="$(CORE_SRCDIRS)" SRCS="examples/jsonrpc/jsonrpc_client.c examples/jsonrpc/cJSON.c"
153153

154154
jsonrpc_server: prepare
155155
$(RM) examples/jsonrpc/*.o
156-
$(MAKEF) TARGET=$@ SRCDIRS="$(CORE_SRCDIRS)" SRCS="examples/jsonrpc/jsonrpc_server.c examples/jsonrpc/jsonrpc.c examples/jsonrpc/cJSON.c"
156+
$(MAKEF) TARGET=$@ SRCDIRS="$(CORE_SRCDIRS)" SRCS="examples/jsonrpc/jsonrpc_server.c examples/jsonrpc/cJSON.c"
157157

158158
protorpc: protorpc_client protorpc_server
159159

event/hloop.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ HV_EXPORT int hio_read_once (hio_t* io);
317317
HV_EXPORT int hio_read_until_length(hio_t* io, unsigned int len);
318318
// hio_read_once => hread_cb(...delim)
319319
HV_EXPORT int hio_read_until_delim (hio_t* io, unsigned char delim);
320+
// @see examples/tinyhttpd.c
320321
#define hio_readline(io) hio_read_until_delim(io, '\n')
321322
#define hio_readstring(io) hio_read_until_delim(io, '\0')
322323
#define hio_readbytes(io, len) hio_read_until_length(io, len)
@@ -413,13 +414,13 @@ HV_EXPORT hio_t* hio_get_upstream(hio_t* io);
413414

414415
// @tcp_upstream: hio_create -> hio_setup_upstream -> hio_setcb_close(hio_close_upstream) -> hconnect -> on_connect -> hio_read_upstream
415416
// @return upstream_io
416-
// @see examples/tcp_proxy_server
417+
// @see examples/tcp_proxy_server.c
417418
HV_EXPORT hio_t* hio_setup_tcp_upstream(hio_t* io, const char* host, int port, int ssl DEFAULT(0));
418419
#define hio_setup_ssl_upstream(io, host, port) hio_setup_tcp_upstream(io, host, port, 1)
419420

420421
// @udp_upstream: hio_create -> hio_setup_upstream -> hio_read_upstream
421422
// @return upstream_io
422-
// @see examples/udp_proxy_server
423+
// @see examples/udp_proxy_server.c
423424
HV_EXPORT hio_t* hio_setup_udp_upstream(hio_t* io, const char* host, int port);
424425

425426
//-----------------unpack---------------------------------------------
@@ -488,6 +489,7 @@ typedef struct unpack_setting_s {
488489
#endif
489490
} unpack_setting_t;
490491

492+
// @see examples/jsonrpc examples/protorpc
491493
HV_EXPORT void hio_set_unpack(hio_t* io, unpack_setting_t* setting);
492494
HV_EXPORT void hio_unset_unpack(hio_t* io);
493495

@@ -569,6 +571,7 @@ typedef struct kcp_setting_s {
569571
#endif
570572
} kcp_setting_t;
571573

574+
// @see examples/udp_echo_server.c => #define TEST_KCP 1
572575
HV_EXPORT int hio_set_kcp(hio_t* io, kcp_setting_t* setting DEFAULT(NULL));
573576
#endif
574577

examples/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ target_link_libraries(udp_echo_server ${HV_LIBRARIES})
4141
add_executable(udp_proxy_server udp_proxy_server.c)
4242
target_link_libraries(udp_proxy_server ${HV_LIBRARIES})
4343

44-
add_executable(jsonrpc_client jsonrpc/jsonrpc_client.c jsonrpc/jsonrpc.c jsonrpc/cJSON.c)
44+
add_executable(jsonrpc_client jsonrpc/jsonrpc_client.c jsonrpc/cJSON.c)
4545
target_link_libraries(jsonrpc_client ${HV_LIBRARIES})
4646

47-
add_executable(jsonrpc_server jsonrpc/jsonrpc_server.c jsonrpc/jsonrpc.c jsonrpc/cJSON.c)
47+
add_executable(jsonrpc_server jsonrpc/jsonrpc_server.c jsonrpc/cJSON.c)
4848
target_link_libraries(jsonrpc_server ${HV_LIBRARIES})
4949

5050
if(WITH_EVPP)

examples/jsonrpc/jsonrpc.c

-49
This file was deleted.

examples/jsonrpc/jsonrpc.h

-27
This file was deleted.

examples/jsonrpc/jsonrpc_client.c

+12-35
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "hbase.h"
1313
#include "hsocket.h"
1414

15-
#include "jsonrpc.h"
1615
#include "cJSON.h"
1716

1817
// hloop_create_tcp_client -> on_connect -> hio_write -> hio_read -> on_recv
@@ -41,19 +40,10 @@ static void on_recv(hio_t* io, void* readbuf, int readbytes) {
4140
SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
4241
}
4342

44-
// unpack
45-
jsonrpc_message msg;
46-
memset(&msg, 0, sizeof(msg));
47-
int packlen = jsonrpc_unpack(&msg, readbuf, readbytes);
48-
if (packlen < 0) {
49-
printf("jsonrpc_unpack failed!\n");
50-
return;
51-
}
52-
assert(packlen == readbytes);
53-
54-
printf("< %.*s\n", msg.head.length, msg.body);
43+
char* resp_str = (char*)readbuf;
44+
printf("< %s\n", resp_str);
5545
// cJSON_Parse
56-
cJSON* jres = cJSON_ParseWithLength(msg.body, msg.head.length);
46+
cJSON* jres = cJSON_Parse(resp_str);
5747
cJSON* jerror = cJSON_GetObjectItem(jres, "error");
5848
cJSON* jresult = cJSON_GetObjectItem(jres, "result");
5949
// ...
@@ -80,25 +70,14 @@ static void on_connect(hio_t* io) {
8070
hevent_set_userdata(io, NULL);
8171
assert(jreq != NULL);
8272

83-
// cJSON_Print -> pack -> hio_write
84-
jsonrpc_message msg;
85-
memset(&msg, 0, sizeof(msg));
86-
msg.body = cJSON_PrintUnformatted(jreq);
87-
msg.head.length = strlen(msg.body);
88-
printf("> %.*s\n", msg.head.length, msg.body);
89-
90-
// pack
91-
unsigned int packlen = jsonrpc_package_length(&msg.head);
92-
unsigned char* writebuf = NULL;
93-
HV_ALLOC(writebuf, packlen);
94-
packlen = jsonrpc_pack(&msg, writebuf, packlen);
95-
if (packlen > 0) {
96-
hio_write(io, writebuf, packlen);
97-
}
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);
9878

9979
cJSON_Delete(jreq);
100-
cJSON_free((void*)msg.body);
101-
HV_FREE(writebuf);
80+
cJSON_free(req_str);
10281
}
10382

10483
static int jsonrpc_call(hloop_t* loop, const char* host, int port, const char* method, const char* param1, const char* param2) {
@@ -142,12 +121,10 @@ int main(int argc, char** argv) {
142121

143122
// init jsonrpc_unpack_setting
144123
memset(&jsonrpc_unpack_setting, 0, sizeof(unpack_setting_t));
145-
jsonrpc_unpack_setting.mode = UNPACK_BY_LENGTH_FIELD;
124+
jsonrpc_unpack_setting.mode = UNPACK_BY_DELIMITER;
146125
jsonrpc_unpack_setting.package_max_length = DEFAULT_PACKAGE_MAX_LENGTH;
147-
jsonrpc_unpack_setting.body_offset = JSONRPC_HEAD_LENGTH;
148-
jsonrpc_unpack_setting.length_field_offset = 1;
149-
jsonrpc_unpack_setting.length_field_bytes = 4;
150-
jsonrpc_unpack_setting.length_field_coding = ENCODE_BY_BIG_ENDIAN;
126+
jsonrpc_unpack_setting.delimiter[0] = '\0';
127+
jsonrpc_unpack_setting.delimiter_bytes = 1;
151128

152129
hloop_t* loop = hloop_new(0);
153130

examples/jsonrpc/jsonrpc_server.c

+12-36
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "hbase.h"
1212
#include "hsocket.h"
1313

14-
#include "jsonrpc.h"
1514
#include "cJSON.h"
1615
#include "router.h"
1716
#include "handler.h"
@@ -43,20 +42,10 @@ static void on_recv(hio_t* io, void* readbuf, int readbytes) {
4342
SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
4443
}
4544

46-
// unpack -> cJSON_Parse -> router -> cJSON_Print -> pack -> hio_write
47-
// unpack
48-
jsonrpc_message msg;
49-
memset(&msg, 0, sizeof(msg));
50-
int packlen = jsonrpc_unpack(&msg, readbuf, readbytes);
51-
if (packlen < 0) {
52-
printf("jsonrpc_unpack failed!\n");
53-
return;
54-
}
55-
assert(packlen == readbytes);
56-
57-
// cJSON_Parse
58-
printf("> %.*s\n", msg.head.length, msg.body);
59-
cJSON* jreq = cJSON_ParseWithLength(msg.body, msg.head.length);
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);
6049
cJSON* jres = cJSON_CreateObject();
6150
cJSON* jid = cJSON_GetObjectItem(jreq, "id");
6251
cJSON* jmethod = cJSON_GetObjectItem(jreq, "method");
@@ -82,25 +71,14 @@ static void on_recv(hio_t* io, void* readbuf, int readbytes) {
8271
bad_request(jreq, jres);
8372
}
8473

85-
// cJSON_Print
86-
memset(&msg, 0, sizeof(msg));
87-
msg.body = cJSON_PrintUnformatted(jres);
88-
msg.head.length = strlen(msg.body);
89-
printf("< %.*s\n", msg.head.length, msg.body);
90-
91-
// pack
92-
packlen = jsonrpc_package_length(&msg.head);
93-
unsigned char* writebuf = NULL;
94-
HV_ALLOC(writebuf, packlen);
95-
packlen = jsonrpc_pack(&msg, writebuf, packlen);
96-
if (packlen > 0) {
97-
hio_write(io, writebuf, packlen);
98-
}
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);
9978

10079
cJSON_Delete(jreq);
10180
cJSON_Delete(jres);
102-
cJSON_free((void*)msg.body);
103-
HV_FREE(writebuf);
81+
cJSON_free(resp_str);
10482
}
10583

10684
static void on_accept(hio_t* io) {
@@ -128,12 +106,10 @@ int main(int argc, char** argv) {
128106

129107
// init jsonrpc_unpack_setting
130108
memset(&jsonrpc_unpack_setting, 0, sizeof(unpack_setting_t));
131-
jsonrpc_unpack_setting.mode = UNPACK_BY_LENGTH_FIELD;
109+
jsonrpc_unpack_setting.mode = UNPACK_BY_DELIMITER;
132110
jsonrpc_unpack_setting.package_max_length = DEFAULT_PACKAGE_MAX_LENGTH;
133-
jsonrpc_unpack_setting.body_offset = JSONRPC_HEAD_LENGTH;
134-
jsonrpc_unpack_setting.length_field_offset = 1;
135-
jsonrpc_unpack_setting.length_field_bytes = 4;
136-
jsonrpc_unpack_setting.length_field_coding = ENCODE_BY_BIG_ENDIAN;
111+
jsonrpc_unpack_setting.delimiter[0] = '\0';
112+
jsonrpc_unpack_setting.delimiter_bytes = 1;
137113

138114
hloop_t* loop = hloop_new(0);
139115
hio_t* listenio = hloop_create_tcp_server(loop, "0.0.0.0", port, on_accept);

examples/protorpc/proto/base.proto

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ message Request {
1515

1616
message Response {
1717
uint64 id = 1;
18-
optional bytes result = 2;
19-
optional Error error = 3;
18+
bytes result = 2;
19+
Error error = 3;
2020
}

examples/protorpc/protorpc.c

+14-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@ int protorpc_pack(const protorpc_message* msg, void* buf, int len) {
1111
return -2;
1212
}
1313
unsigned char* p = (unsigned char*)buf;
14-
// flags
14+
*p++ = head->protocol[0];
15+
*p++ = head->protocol[1];
16+
*p++ = head->protocol[2];
17+
*p++ = head->protocol[3];
18+
*p++ = head->version;
1519
*p++ = head->flags;
20+
*p++ = head->reserved[0];
21+
*p++ = head->reserved[1];
1622
// hton length
1723
unsigned int length = head->length;
1824
*p++ = (length >> 24) & 0xFF;
@@ -32,8 +38,14 @@ int protorpc_unpack(protorpc_message* msg, const void* buf, int len) {
3238
if (len < PROTORPC_HEAD_LENGTH) return -2;
3339
protorpc_head* head = &(msg->head);
3440
const unsigned char* p = (const unsigned char*)buf;
35-
// flags
41+
head->protocol[0] = *p++;
42+
head->protocol[1] = *p++;
43+
head->protocol[2] = *p++;
44+
head->protocol[3] = *p++;
45+
head->version = *p++;
3646
head->flags = *p++;
47+
head->reserved[0] = *p++;
48+
head->reserved[1] = *p++;
3749
// ntoh length
3850
head->length = ((unsigned int)*p++) << 24;
3951
head->length |= ((unsigned int)*p++) << 16;

0 commit comments

Comments
 (0)