a tiny C++14 rpc library, supports all platforms (macOS, Linux, Windows, iOS, Android, etc.) and most microchips ( Arduino, STM32, ESP32/ESP8266, etc.)
Recommend TCP-based implementation: asio_net
Full-feature rpc frameworks (e.g. gRPC
and bRPC
) are very complex and not suitable for embedded systems.
This project offers a lightweight and user-friend rpc library 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 only offers the protocol layer and API, it does not include the implementation of the transport layer. For TCP-based implementation: asio_net
- Header-Only
- No-Schema
- Support performance-limited platforms including microchips
- Support any connection type (
tcp socket
,serial port
, etc.) - High Performance Serialization, support most STL containers and user type
- Serialization plugins implementations for
flatbuffers
andnlohmann::json
- RAII-based
dispose
for automatic cancel request - Timeout and Retry API
- Support
std::future
interface - Support
co_await
, depend onC++20
andasio
, or custom implementation
-
C++
-
Rust
- C++14
- Provide your connection implementation: connection
NOTICE: complete data packets are required for data transmission, such aswebsocket
.
If usingtcp socket
,serial port
, etc., message pack and unpack need to be implemented. Or you can use stream_connection. - Optional: C++20 (for coroutine api, co_await async_call)
// 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();
// Or use C++20 co_await with asio
auto rsp = co_await rpc->cmd("cmd")->msg(std::string("hello"))->async_call<std::string>();
assert(rsp.data == "world");
// Or you can use custom async implementation, and co_await it!
Addition:
msg
andrsp
support any serializable type, see Serialization.- Detailed usages and unittests can be found here: rpc_test.cpp
- There is an example shows custom async impl: rpc_c_coroutine.hpp
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"}
library | bytes |
---|---|
json | 31 |
flatbuffers | 44 |
protobuf | 10 |
msgpack | 8 |
rpc_core | 8 |
- std::string
- std::wstring
- std::array
- std::vector
- std::list
- std::forward_list
- std::deque
- std::pair
- std::tuple
- std::map
- std::unordered_map
- std::multimap
- std::unordered_multimap
- std::set
- std::unordered_set
- std::multiset
- std::unordered_multiset
- std::stack
- std::queue
- std::priority_queue
- std::bitset
- std::complex
- std::chrono::duration
- std::chrono::time_point
- std::unique_ptr
- std::shared_ptr
- rpc_core::binary_wrap
- custom struct/class
choose
#include "rpc_core/serialize.hpp" struct TestStruct { uint8_t a; std::string b; OtherType c // RPC_CORE_DEFINE_TYPE_INNER(a, b, c); }; RPC_CORE_DEFINE_TYPE(TestStruct, a, b, c);
RPC_CORE_DEFINE_TYPE
orRPC_CORE_DEFINE_TYPE_INNER
for private member variable.
-
flatbuffers.hpp
Supports using types generated byflatbuffers
directly as message
(add the option--gen-object-api
when usingflatc
) -
json_msg.hpp
Supports using types supported by nlohmann/json directly as message
(theto_json/from_json
rules innlohmann/json
need to be satisfied, and useDEFINE_JSON_CLASS
). -
json.hpp
A flexible way to usenlohmann/json
This project is licensed under the MIT license.