# HTTP-JRPC-C **Repository Path**: alpha_skyate/http-jrpc-c ## Basic Information - **Project Name**: HTTP-JRPC-C - **Description**: C语言编写的HTTP协议JSON RPC - **Primary Language**: C - **License**: GPL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 4 - **Forks**: 1 - **Created**: 2022-04-25 - **Last Updated**: 2024-08-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: JRPC, JSON, rpc ## README ## 0 概述 ### 0.0 参考 [GitHub - hmng/jsonrpc-c: JSON-RPC in C (server only for now)](https://github.com/hmng/jsonrpc-c) [GitHub - cesanta/mongoose: Embedded Web Server](https://github.com/cesanta/mongoose/) [GitHub - DaveGamble/cJSON: Ultralightweight JSON parser in ANSI C](https://github.com/DaveGamble/cJSON) ### 0.1 HTTP 服务 HTTP-JRPC-C 内置了HTTP服务用于提供JRPC API服务和静态文件服务。纯C语言编写。在最开始我是使用的[hmng](https://github.com/hmng)/**[jsonrpc-c](https://github.com/hmng/jsonrpc-c)** 但是由于该服务只能提供TCP连接,不太适用于我的应用场景,因此结合mongoos库对其进行了重构。 ### 0.2 JSON RPC HTTP错误码 | 错误码 sys-error | 描述 | | ---------------- | ---------------- | | 000 | 正常 | | 101 | 未找到指定的方法 | | 102 | 格式错误 | | 103 | 请求方法错误 | ### 0.3 目录结构 | 一级 | 二级 | 三级 | 四级 | 文件说明 | | ------- | ---- | ----------- | ---------- | ------------------------------------------------------------ | | build | | | | 编译输出目录 | | | bin | | | 编译输出目录,也是运行目录 | | | | lib | | | | | | web | | JRPC-HTTP会默认穷举该目录下的文件作为服务器静态文件 | | | | | index.html | 静态服务器默认界面,可在运行JRPC-HTTP后尝试访问http://xxx.xxx.xxx.xxx:8000 | | | | JRPC-HTTP | | 编译后得到的可执行文件,在Ubuntu 16下使用GCC编译。 | | CommLib | | | | | | | inc | | | | | | | cJSON.h | | cJSON.h头文件 | | | | comm.h | | 常用函数,如delay_ms延时函数 | | | src | | | | | | | cJSON.c | | cJSON.h源文件 | | | | comm.c | | 常用函数,如delay_ms延时函数 | | JRPC | | | | | | | inc | | | | | | | jsonrpc-c.h | | jsonrpc头文件 | | | | mongoose.h | | mongoose头文件 | | | src | | | | | | | jsonrpc-c.c | | jsonrpc源文件 | | | | mongoose.c | | mongoose源文件 | ## 1 使用 ### 1.1 接口访问示例 方法原型 main.c:30 ```c cJSON * add(jrpc_context * ctx, cJSON * params, cJSON *id) { cJSON * a = cJSON_GetArrayItem(params,0); cJSON * b = cJSON_GetArrayItem(params,1); return cJSON_CreateNumber(a->valueint + b->valueint); } ``` POST方法,路径:http://xxx.xxx.xxx.xxx:8000 使用POSTMAN测试如下: ![](./img/微信截图_20220425171538.png) ### 1.2 方法注册 在main()中创建了一个HTTP线程,线程的一开始会对JRPC进行初始化。在初始化完成后可以在任何时候调用jrpc_register_procedure()注册方法。 ```c //线程句柄 pthread_t HTTP_main_thread_tid; //RPC 方法 cJSON * say_hello(jrpc_context * ctx, cJSON * params, cJSON *id) { return cJSON_CreateString("Hello!"); } cJSON * add(jrpc_context * ctx, cJSON * params, cJSON *id) { cJSON * a = cJSON_GetArrayItem(params,0); cJSON * b = cJSON_GetArrayItem(params,1); return cJSON_CreateNumber(a->valueint + b->valueint); } extern struct jrpc_server my_server; void main(int argc, char *argv[]) { int ret; //获取运行路径 ProgramRootPath = malloc(strlen(argv[0])); memset(ProgramRootPath,0x00,strlen(argv[0])); memcpy(ProgramRootPath,argv[0],strlen(argv[0])-17); printf("PATH : %s",ProgramRootPath); //创建HTTP线程 ret = pthread_create(&HTTP_main_thread_tid,NULL,HTTP_MainThread,NULL); jrpc_register_procedure(&my_server, say_hello, "sayHello", NULL ); jrpc_register_procedure(&my_server, add, "add", NULL ); while (1) { delay_ms(2000); } } ``` ### 1.3 修改监听端口 程序默认监听的端口号为8000,可在/JRPC/src/jsonrpc-c.c中修改为你所需要的端口。 ```c void *HTTP_MainThread(void *arg) { struct mg_mgr mgr; mg_mgr_init(&mgr); printf("Server listen on port 8000\r\n"); //监听地址与端口设置 mg_http_listen(&mgr, "0.0.0.0:8000", fn, NULL); // Create listening connection while (1) { mg_mgr_poll(&mgr, 100); // Block forever } } ```