3 unstable releases
0.2.1 | Dec 6, 2024 |
---|---|
0.2.0 | Nov 17, 2024 |
0.1.0 | Nov 12, 2024 |
#751 in Network programming
1,262 downloads per month
Used in raftpico
29KB
597 lines
jsonlrpc_mio
Non-blocking jsonlrpc
server and client using mio
.
Examples
use std::net::SocketAddr;
use jsonlrpc::{RequestId, RequestObject, ResponseObject};
use jsonlrpc_mio::{RpcClient, RpcServer};
use mio::{Events, Poll, Token};
let mut poller = Poll::new()?;
let mut events = Events::with_capacity(1024);
let mut server: RpcServer = RpcServer::start(
&mut poller,
SocketAddr::from(([127, 0, 0, 1], 0)),
Token(0),
Token(9),
)?;
let mut client = RpcClient::new(Token(10), server.listen_addr());
let request = RequestObject {
jsonrpc: jsonlrpc::JsonRpcVersion::V2,
method: "ping".to_owned(),
params: None,
id: Some(RequestId::Number(123)),
};
client.send(&mut poller, &request)?;
loop {
poller.poll(&mut events, None)?;
for event in events.iter() {
server.handle_event(&mut poller, event)?;
if let Some((from, request)) = server.try_recv() {
assert_eq!(request.method, "ping");
let response = ResponseObject::Ok {
jsonrpc: jsonlrpc::JsonRpcVersion::V2,
result: serde_json::json! { "pong" },
id: request.id.unwrap(),
};
server.reply(&mut poller, from, &response)?;
}
client.handle_event(&mut poller, event)?;
if let Some(response) = client.try_recv() {
let value = response.into_std_result().unwrap();
assert_eq!(value, serde_json::json! { "pong" });
return Ok(());
}
}
}
Dependencies
~1–8.5MB
~82K SLoC