a Tiny RPC library for ESP32/ESP8266, and can be ported to any platform easily.
RPC based on rpc_core
-
C++11
-
implement
namespace esp_rpc {
void dispatch(std::function<void()> runnable) {
// dispatch runnable to main thread looper
// because tcp callback may from interrupt or other thread
}
void set_timeout(uint32_t ms, std::function<void()> cb) {
// timeout implement
}
} // namespace esp_rpc
- RPC
// server
rpc_server server(8080);
server.on_session = [](const std::weak_ptr<rpc_session>& rs) {
auto session = rs.lock();
session->on_close = [rs] {
};
session->rpc->subscribe("cmd", [](const std::string& data) -> std::string {
return "world";
});
};
server.start();
// client
rpc_client client;
client.on_open = [&](const std::shared_ptr<rpc_core::rpc>& rpc) {
rpc->cmd("cmd")
->msg(std::string("hello"))
->rsp([&](const std::string& data) {
})
->call();
};
client.on_close = [&] {
};
client.open("localhost", 8080);
- for the platform with Operating System, there is a library based on asio: asio_net