概述
- 接口层:客户端实现了服务端的接口代理,而服务端的接口为具体实现。
- 协议层:客户端根据规则实现接口参数的打包,序列化等动作。服务端根据规则将参数反序列化,并将其传输到服务接口。
- 传输层:传输介质:TCP/UDP,HTTP,蓝牙,串口,USB等。

RPC的特点:
RPC的优缺点:
优点:实现模块的分布式部署,可以实现更好的维护性,扩展性以及协同式开发。
缺点:①通信延迟;②地址空间隔离;③局部故障;④并发问题。
eRPC (Embedded RPC)
eRPC是什么
eRPC (Embedded RPC) is an open source Remote Procedure Call (RPC) system for multichip embedded systems and heterogeneous multicore SoCs.
eRPC(嵌入式RPC)是一种用于多芯片嵌入式系统和异构多核SoC的开源远程过程调用(RPC)系统。
Unlike other modern RPC systems, such as the excellent Apache Thrift, eRPC distinguishes itself by being designed for tightly coupled systems, using plain C for remote functions, and having a small code size (<5kB). It is not intended for high performance distributed systems over a network.
与其他现代RPC系统(如出色的Apache Thrift)不同,eRPC的与众不同之处在于它是为紧密耦合的系统设计的,使用纯C实现远程功能,并且代码大小较小(<5kB)。它不适用于网络上的高性能分布式系统。
eRPC源码
eRPC源码路径「https://github.com/EmbeddedRPC/erpc」
eRPC源码目录我们关注两个目录erpc_c和erpcgen。其中:erpc_c是eRPC的C/C++实现。erpcgen是将IDL文件转为C或Python源文件。
.
├── doxygen
├── erpc_c
│ ├── config
│ ├── infra
│ ├── port
│ ├── setup
│ └── transports
├── erpcgen
├── erpc_python
├── erpcsniffer
├── examples
├── mk
├── README.md
├── test
└── utilities
目录 | 说明 |
---|---|
erpc_c/config | eRPC的配置文件 |
erpc_c/infra | eRPC的核心代码 |
erpc_c/port | eRPC的移植层,适配不同的开发环境 |
erpc_c/setup | eRPC的C接口 |
erpc_c/transports | eRPC的传输层,包含不同介质的驱动 |
eRPC编译
我们需要编译两个东西,其中:①需要将编译erpc_c编译成库,②编译erpcgen编译成可执行文件,用于.erpc的IDL语法生成service和client的代码。
编译eRPC库为了方便我们编译,我们将eRPC编程库,然后我们的应用通过链接方式生成可执行文件。步骤:
eRPC为了能过够更加方便供开发者使用,提供了IDL的解析器erpcgen及生成规则,减少了我们编码。erpcgen在eRPC中非常重要。步骤:
eRPC例子
我们写一个简单的例子,传输层采用TCP,Client发一个字符串,Server端回复一个字符串。步骤:
program youyeetoo //指定生成的文件名
interface youyeetoo { //接口定义,包好一个或者多个函数
helloYouyeetoo(binary txInput) -> binary, //函数:helloYouyeetoo,入参类型:binary,返回值类型:binary
}
youyeetoo@youyeetoo:~/youyeetoo$ erpcgen ./youyeetoo.erpc
上述执行完会在当前目录下生成4个文件:"youyeetoo_client.cpp","youyeetoo_server.cpp","youyeetoo_server.h","youyeetoo.h"。其中:
- 根据.erpc文件会生成一个接口:binary_t * helloYouyeetoo(const binary_t * txInput);。
- 客户端无需实现这个接口的定义,它的实现已经自动生成放在youyeetoo_client.cpp。上层应用直接使用即可。
- 服务端需要没有实现这个接口,所以需要在上层应用实现函数体的内容。
创建一个客户端的上层应用文件:client_app.cpp。其中:
- 创建一个TCP传输层通道。
- 初始化eRPC客户端对象。
- 通过helloYouyeetoo函数进行远程调用,发送一条消息:"hello youyeetoo!"。
- 将远程调用的返回值打印出来。
- 编译命令:「g++ -Wall -I. -I/usr/local/include/erpc -L/usr/local/lib youyeetoo_client.cpp client_app.cpp -lerpc -lpthread -o client_app」
- 生成client_app可执行文件。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <erpc_client_setup.h>
#include <erpc_port.h>
#include "youyeetoo.h"
static void free_binary_t_struct(binary_t *data)
{
if (data->data) {
erpc_free(data->data);
}
}
int main(int argc, char *argv[])
{
erpc_transport_t transport = erpc_transport_tcp_init("127.0.0.1", 5555, false);
erpc_mbf_t message_buffer_factory = erpc_mbf_dynamic_init();
erpc_client_init(transport, message_buffer_factory);
char *msg = "hello, youyeetoo!";
binary_t b = {(uint8_t *)msg, (uint32_t)strlen(msg)};
printf("Request: %s\n", msg);
binary_t *resp = helloYouyeetoo(&b);
if (resp != NULL) {
char *buf = (char *)malloc(resp->dataLength + 1);
strncpy(buf, (const char *)resp->data, resp->dataLength);
buf[resp->dataLength] = '\0';
printf("Respond: %s\n", buf);
free_binary_t_struct(resp);
free(buf);
}
erpc_transport_tcp_close();
return 0;
}
- 创建一个TCP传输层通道。
- 初始化eRPC服务端对象。
- 注册服务到服务端对象中。
- 运行服务端线程。
- 当客户端进行远程调用时,将会进入helloYouyeetoo函数,并返回。
- 编译命令:「g++ -Wall -I. -I/usr/local/include/erpc -L/usr/local/lib youyeetoo_server.cpp server_app.cpp -lerpc -lpthread -o server_app」
- 生成client_app可执行文件。
#include <sys/time.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <erpc_server_setup.h>
#include "youyeetoo_server.h"
binary_t * helloYouyeetoo(const binary_t * input)
{
size_t len = 0;
char *buf;
printf("recv: %s\r\n", input->data);
buf = (char *)malloc(strlen("hi, good!"));
memset(buf, 0, strlen("hi, good!"));
strncpy(buf, "hi, good!", strlen("hi, good!"));
printf("send: hi, good!\n");
len = strlen("hi, good!");
return new binary_t{(uint8_t*)buf, (uint32_t)len};
}
int main(int argc, char *argv[])
{
erpc_transport_t transport = erpc_transport_tcp_init("127.0.0.1", 5555, true);
erpc_mbf_t message_buffer_factory = erpc_mbf_dynamic_init();
erpc_server_t server = erpc_server_init(transport, message_buffer_factory);
erpc_add_service_to_server(server, create_youyeetoo_service());
while(1){
erpc_server_run(server);
}
erpc_transport_tcp_close();
return 0;
}

总结
关注微信公众号『Rice嵌入式开发技术分享』,后台回复“微信”添加作者微信,备注”入群“,便可邀请进入技术交流群。
