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)。它不适用于网络上的高性能分布式系统。
https://github.com/EmbeddedRPC/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_c编译成库,②编译erpcgen编译成可执行文件,用于.erpc的IDL语法生成service和client的代码。
为了方便我们编译,我们将eRPC编程库,然后我们的应用通过链接方式生成可执行文件。步骤:
eRPC为了能过够更加方便供开发者使用,提供了IDL的解析器erpcgen及生成规则,减少了我们编码。erpcgen在eRPC中非常重要。步骤:
我们写一个简单的例子,传输层采用TCP,Client发一个字符串,Server端回复一个字符串。步骤:
program youyeetoo //指定生成的文件名
interface youyeetoo { //接口定义,包好一个或者多个函数
helloYouyeetoo(binary txInput) -> binary, //函数:helloYouyeetoo,入参类型:binary,返回值类型:binary
}
youyeetoo@youyeetoo:~/youyeetoo$ erpcgen ./youyeetoo.erpc
#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;
}
#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;
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。