{"id":131,"date":"2024-08-27T15:18:00","date_gmt":"2024-08-27T07:18:00","guid":{"rendered":"https:\/\/aimc.skyate.com\/?p=131"},"modified":"2024-10-27T15:22:43","modified_gmt":"2024-10-27T07:22:43","slug":"c%e8%af%ad%e8%a8%80%e7%bc%96%e5%86%99%e7%9a%84%e7%ae%80%e5%8d%95http-json-rpc","status":"publish","type":"post","link":"https:\/\/aimc.skyate.com\/index.php\/2024\/08\/27\/c%e8%af%ad%e8%a8%80%e7%bc%96%e5%86%99%e7%9a%84%e7%ae%80%e5%8d%95http-json-rpc\/","title":{"rendered":"C\u8bed\u8a00\u7f16\u5199\u7684\u7b80\u5355HTTP json RPC"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">0 \u6982\u8ff0<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">0.0 \u53c2\u8003<\/h3>\n\n\n\n<p><a href=\"https:\/\/github.com\/hmng\/jsonrpc-c\">GitHub &#8211; hmng\/jsonrpc-c: JSON-RPC in C (server only for now)<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/cesanta\/mongoose\/\">GitHub &#8211; cesanta\/mongoose: Embedded Web Server<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/DaveGamble\/cJSON\">GitHub &#8211; DaveGamble\/cJSON: Ultralightweight JSON parser in ANSI C<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">0.1 HTTP \u670d\u52a1<\/h3>\n\n\n\n<p>HTTP-JRPC-C \u5185\u7f6e\u4e86HTTP\u670d\u52a1\u7528\u4e8e\u63d0\u4f9bJRPC API\u670d\u52a1\u548c\u9759\u6001\u6587\u4ef6\u670d\u52a1\u3002\u7eafC\u8bed\u8a00\u7f16\u5199\u3002\u5728\u6700\u5f00\u59cb\u6211\u662f\u4f7f\u7528\u7684<a href=\"https:\/\/github.com\/hmng\">hmng<\/a>\/<strong><a href=\"https:\/\/github.com\/hmng\/jsonrpc-c\">jsonrpc-c<\/a><\/strong>&nbsp;\u4f46\u662f\u7531\u4e8e\u8be5\u670d\u52a1\u53ea\u80fd\u63d0\u4f9bTCP\u8fde\u63a5\uff0c\u4e0d\u592a\u9002\u7528\u4e8e\u6211\u7684\u5e94\u7528\u573a\u666f\uff0c\u56e0\u6b64\u7ed3\u5408mongoos\u5e93\u5bf9\u5176\u8fdb\u884c\u4e86\u91cd\u6784\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">0.2 JSON RPC HTTP\u9519\u8bef\u7801<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u9519\u8bef\u7801 sys-error<\/th><th>\u63cf\u8ff0<\/th><\/tr><\/thead><tbody><tr><td>000<\/td><td>\u6b63\u5e38<\/td><\/tr><tr><td>101<\/td><td>\u672a\u627e\u5230\u6307\u5b9a\u7684\u65b9\u6cd5<\/td><\/tr><tr><td>102<\/td><td>\u683c\u5f0f\u9519\u8bef<\/td><\/tr><tr><td>103<\/td><td>\u8bf7\u6c42\u65b9\u6cd5\u9519\u8bef<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">0.3 \u76ee\u5f55\u7ed3\u6784<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u4e00\u7ea7<\/th><th>\u4e8c\u7ea7<\/th><th>\u4e09\u7ea7<\/th><th>\u56db\u7ea7<\/th><th>\u6587\u4ef6\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>build<\/td><td><\/td><td><\/td><td><\/td><td>\u7f16\u8bd1\u8f93\u51fa\u76ee\u5f55<\/td><\/tr><tr><td><\/td><td>bin<\/td><td><\/td><td><\/td><td>\u7f16\u8bd1\u8f93\u51fa\u76ee\u5f55\uff0c\u4e5f\u662f\u8fd0\u884c\u76ee\u5f55<\/td><\/tr><tr><td><\/td><td><\/td><td>lib<\/td><td><\/td><td><\/td><\/tr><tr><td><\/td><td><\/td><td>web<\/td><td><\/td><td>JRPC-HTTP\u4f1a\u9ed8\u8ba4\u7a77\u4e3e\u8be5\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u4f5c\u4e3a\u670d\u52a1\u5668\u9759\u6001\u6587\u4ef6<\/td><\/tr><tr><td><\/td><td><\/td><td><\/td><td>index.html<\/td><td>\u9759\u6001\u670d\u52a1\u5668\u9ed8\u8ba4\u754c\u9762\uff0c\u53ef\u5728\u8fd0\u884cJRPC-HTTP\u540e\u5c1d\u8bd5\u8bbf\u95ee<a href=\"http:\/\/xxx.xxx.xxx.xxx:8000\/\">http:\/\/xxx.xxx.xxx.xxx:8000<\/a><\/td><\/tr><tr><td><\/td><td><\/td><td>JRPC-HTTP<\/td><td><\/td><td>\u7f16\u8bd1\u540e\u5f97\u5230\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u5728Ubuntu 16\u4e0b\u4f7f\u7528GCC\u7f16\u8bd1\u3002<\/td><\/tr><tr><td>CommLib<\/td><td><\/td><td><\/td><td><\/td><td><\/td><\/tr><tr><td><\/td><td>inc<\/td><td><\/td><td><\/td><td><\/td><\/tr><tr><td><\/td><td><\/td><td>cJSON.h<\/td><td><\/td><td>cJSON.h\u5934\u6587\u4ef6<\/td><\/tr><tr><td><\/td><td><\/td><td>comm.h<\/td><td><\/td><td>\u5e38\u7528\u51fd\u6570\uff0c\u5982delay_ms\u5ef6\u65f6\u51fd\u6570<\/td><\/tr><tr><td><\/td><td>src<\/td><td><\/td><td><\/td><td><\/td><\/tr><tr><td><\/td><td><\/td><td>cJSON.c<\/td><td><\/td><td>cJSON.h\u6e90\u6587\u4ef6<\/td><\/tr><tr><td><\/td><td><\/td><td>comm.c<\/td><td><\/td><td>\u5e38\u7528\u51fd\u6570\uff0c\u5982delay_ms\u5ef6\u65f6\u51fd\u6570<\/td><\/tr><tr><td>JRPC<\/td><td><\/td><td><\/td><td><\/td><td><\/td><\/tr><tr><td><\/td><td>inc<\/td><td><\/td><td><\/td><td><\/td><\/tr><tr><td><\/td><td><\/td><td>jsonrpc-c.h<\/td><td><\/td><td>jsonrpc\u5934\u6587\u4ef6<\/td><\/tr><tr><td><\/td><td><\/td><td>mongoose.h<\/td><td><\/td><td>mongoose\u5934\u6587\u4ef6<\/td><\/tr><tr><td><\/td><td>src<\/td><td><\/td><td><\/td><td><\/td><\/tr><tr><td><\/td><td><\/td><td>jsonrpc-c.c<\/td><td><\/td><td>jsonrpc\u6e90\u6587\u4ef6<\/td><\/tr><tr><td><\/td><td><\/td><td>mongoose.c<\/td><td><\/td><td>mongoose\u6e90\u6587\u4ef6<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">1 \u4f7f\u7528<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1.1 \u63a5\u53e3\u8bbf\u95ee\u793a\u4f8b<\/h3>\n\n\n\n<p>\u65b9\u6cd5\u539f\u578b main.c:30<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cJSON * add(jrpc_context * ctx, cJSON * params, cJSON *id) {\n    cJSON * a = cJSON_GetArrayItem(params,0);\n    cJSON * b = cJSON_GetArrayItem(params,1);\n    return cJSON_CreateNumber(a-&gt;valueint + b-&gt;valueint);\n}<\/code><\/pre>\n\n\n\n<p>C\u70b9\u51fb\u590d\u5236<\/p>\n\n\n\n<p>POST\u65b9\u6cd5\uff0c\u8def\u5f84\uff1a<a href=\"http:\/\/xxx.xxx.xxx.xxx:8000\/\">http:\/\/xxx.xxx.xxx.xxx:8000<\/a><\/p>\n\n\n\n<p>\u4f7f\u7528POSTMAN\u6d4b\u8bd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/alpha.skyate.com\/usr\/uploads\/2023\/06\/3278563984.png\" alt=\"POSTMAN\u6d4b\u8bd5\u622a\u56fe\" title=\"POSTMAN\u6d4b\u8bd5\u622a\u56fe\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">1.2 \u65b9\u6cd5\u6ce8\u518c<\/h3>\n\n\n\n<p>\u5728main()\u4e2d\u521b\u5efa\u4e86\u4e00\u4e2aHTTP\u7ebf\u7a0b\uff0c\u7ebf\u7a0b\u7684\u4e00\u5f00\u59cb\u4f1a\u5bf9JRPC\u8fdb\u884c\u521d\u59cb\u5316\u3002\u5728\u521d\u59cb\u5316\u5b8c\u6210\u540e\u53ef\u4ee5\u5728\u4efb\u4f55\u65f6\u5019\u8c03\u7528jrpc_register_procedure()\u6ce8\u518c\u65b9\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u7ebf\u7a0b\u53e5\u67c4\npthread_t HTTP_main_thread_tid;\n\n\/\/RPC \u65b9\u6cd5\ncJSON * say_hello(jrpc_context * ctx, cJSON * params, cJSON *id) {\n    return cJSON_CreateString(\"Hello!\");\n}\n\n\ncJSON * add(jrpc_context * ctx, cJSON * params, cJSON *id) {\n    cJSON * a = cJSON_GetArrayItem(params,0);\n    cJSON * b = cJSON_GetArrayItem(params,1);\n    return cJSON_CreateNumber(a-&gt;valueint + b-&gt;valueint);\n}\n\nextern struct jrpc_server my_server;\nvoid main(int argc, char *argv&#91;])\n{\n    int ret;\n    \/\/\u83b7\u53d6\u8fd0\u884c\u8def\u5f84\n    ProgramRootPath = malloc(strlen(argv&#91;0]));\n    memset(ProgramRootPath,0x00,strlen(argv&#91;0]));\n    memcpy(ProgramRootPath,argv&#91;0],strlen(argv&#91;0])-17);    \n    printf(\"PATH : %s\",ProgramRootPath);\n   \n    \/\/\u521b\u5efaHTTP\u7ebf\u7a0b\n    ret = pthread_create(&amp;HTTP_main_thread_tid,NULL,HTTP_MainThread,NULL);\n    jrpc_register_procedure(&amp;my_server, say_hello, \"sayHello\", NULL );\n    jrpc_register_procedure(&amp;my_server, add, \"add\", NULL );\n    while (1)\n    {\n        delay_ms(2000);\n    }\n    \n}<\/code><\/pre>\n\n\n\n<p>C\u70b9\u51fb\u590d\u5236<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.3 \u4fee\u6539\u76d1\u542c\u7aef\u53e3<\/h3>\n\n\n\n<p>\u7a0b\u5e8f\u9ed8\u8ba4\u76d1\u542c\u7684\u7aef\u53e3\u53f7\u4e3a8000\uff0c\u53ef\u5728\/JRPC\/src\/jsonrpc-c.c\u4e2d\u4fee\u6539\u4e3a\u4f60\u6240\u9700\u8981\u7684\u7aef\u53e3\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void *HTTP_MainThread(void *arg)\n{\n    struct mg_mgr mgr;\n\n    mg_mgr_init(&amp;mgr);\n    printf(\"Server listen on port 8000\\r\\n\");\n    \/\/\u76d1\u542c\u5730\u5740\u4e0e\u7aef\u53e3\u8bbe\u7f6e\n    mg_http_listen(&amp;mgr, \"0.0.0.0:8000\", fn, NULL);     \/\/ Create listening connection\n    while (1)\n    {\n        mg_mgr_poll(&amp;mgr, 100);                        \/\/ Block forever\n    }     \n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4 \u6e90\u7801\u83b7\u53d6<\/h2>\n\n\n\n<p><a href=\"https:\/\/gitee.com\/alpha_skyate\/http-jrpc-c\">gitee\u83b7\u53d6\u6e90\u7801<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>0 \u6982\u8ff0 0.0 \u53c2\u8003 GitHub &#8211; hmng\/jsonrpc-c: JSON-RPC in  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":132,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[46,48,47,49],"class_list":["post-131","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32_arduino","tag-esp32","tag-json","tag-rpc","tag-49"],"_links":{"self":[{"href":"https:\/\/aimc.skyate.com\/index.php\/wp-json\/wp\/v2\/posts\/131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aimc.skyate.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aimc.skyate.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aimc.skyate.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aimc.skyate.com\/index.php\/wp-json\/wp\/v2\/comments?post=131"}],"version-history":[{"count":1,"href":"https:\/\/aimc.skyate.com\/index.php\/wp-json\/wp\/v2\/posts\/131\/revisions"}],"predecessor-version":[{"id":133,"href":"https:\/\/aimc.skyate.com\/index.php\/wp-json\/wp\/v2\/posts\/131\/revisions\/133"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aimc.skyate.com\/index.php\/wp-json\/wp\/v2\/media\/132"}],"wp:attachment":[{"href":"https:\/\/aimc.skyate.com\/index.php\/wp-json\/wp\/v2\/media?parent=131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aimc.skyate.com\/index.php\/wp-json\/wp\/v2\/categories?post=131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aimc.skyate.com\/index.php\/wp-json\/wp\/v2\/tags?post=131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}