C++轻量级RPC库rpc_core

C++11轻量级的RPC库,可用于任何项目中,甚至单片机。方便平台直接相互进行功能调用。

目前也有一些轻量的库,参考了protobuf(或者依赖它),基于IDL。
其实对于中小型的项目而言,使用IDL并不是一个好的选择,使用也不方便。
本库提供了性能更好、更加方便易用的序列化方案。

仓库地址

https://github.com/shuai132/rpc_core

以下是README,方便SEO。最新文档请打开上述链接。

rpc_core

a tiny c++11 rpc library, supports all platforms (macOS, Linux, Windows, iOS, Android, etc.) and most microchips (Arduino, STM32, ESP32/ESP8266, etc.)

Introduction

The complete rpc frameworks (such as gRPC and brpc) have complex functions
and are not practical on embedded platforms.

This project offers a lightweight and user-friendly implementation that is better suited for one-to-one rpc calls.
It supports all platforms and a wide range of microchips, including Arduino, STM32, ESP32/ESP8266, and more.

Note:
This project solely offers the protocol layer and API,
and does not include the implementation of the transport layer.
For TCP-based implementations: asio_net

Features

  • Header-Only
  • No schema
  • Support performance-limited platforms, including microchips
  • Compatible with any connection type (tcp socket, serial port, etc.)
  • Serialization for primitive types, most STL containers, and user struct/class
  • Serialization plugins implementations for flatbuffers and nlohmann::json
  • RAII-based dispose for automatic request cancellation
  • Timeout and retry settings
  • Support std::future interface

Requirements

  • C++11
  • Provide your connection implementation: connection
    NOTICE: complete data packets are required for data transmission, such as websocket.
    If using tcp socket, serial port, etc., message pack and unpack need to be implemented by yourself.
    Or you can use stream_connection.

Usage

// The Receiver
rpc->subscribe("cmd", [](const std::string& msg) -> std::string {
    assert(msg == "hello");
    return "world";
});

// The Sender
rpc->cmd("cmd")
    ->msg(std::string("hello"))
    ->rsp([](const std::string& rsp) {
      assert(rsp == "world");
    })
    ->call();

msg and rsp support any serializable type, see Serialization.

Detailed initialization process and unit tests can be found here: rpc_test.cpp

Serialization

High-performance and memory-saving binary serialization.

For example, user data:

struct Type {
  uint8_t id = 1;
  uint8_t age = 18;
  std::string name = "test";
};

json: {"id":1,"age":18,"name":"test"}

librarybytes
json31
flatbuffers44
protobuf10
msgpack8
rpc_core8

Serialization Plugins

  • flatbuffers.hpp
    Supports using types generated by flatbuffers directly as message
    (add the option --gen-object-api when using flatc)

  • json_msg.hpp
    Supports using types supported by nlohmann/json directly as message
    (the to_json/from_json rules in nlohmann/json need to be satisfied, and use DEFINE_JSON_CLASS).

  • json.hpp
    A flexible way to use nlohmann/json

Links

  • Implementation based on asio: asio_net
    support macOS, Linux, Windows, iOS, Android, etc. and can be used on MCUs that support asio, such as ESP32.

  • Implementation suitable for ESP8266: esp_rpc

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值