macro_rules! client_requests {
($request_mod:ident;
$( ($id:expr, $_fn:expr, $method:ident ( $req_type:ty, $req_opt_buf:ident, $rep_type:ty, $rep_opt_buf:ident)) ),*) => { ... };
}Expand description
Macro that builds the required types to make calls via RPC from the client.
ยงExamples
use urpc::{client_requests, client, consts, OptBufNo, OptBufYes};
mod cli {
use urpc::client_requests;
client_requests! {
client_requests;
(0, ping, Ping([u8; 4], OptBufNo, [u8; 4], OptBufNo)),
(1, send_bytes, SendBytes((), OptBufYes, (), OptBufNo))
}
}
let mut rpc_client = client::RpcClient::new(32);
let mut send_buf = vec![0; 32];
let mut recv_buf = vec![0; 32];
let mut req1 = cli::Ping::new([0, 1, 2, 3]);
let send_buf_bytes = req1.request(&mut rpc_client, &mut send_buf).unwrap();
println!("request bytes: {:02x?}", &send_buf[..send_buf_bytes]);
// Send send_buf over the network
// [...]
// Read from the network into recv_buf
// [...]
// We fill recv_buf with some precalculated replies to simulate a server reply
recv_buf[..10].copy_from_slice(&[0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03]);
// Parse read bytes with rpc_client and try to match replies from each request
let mut pos = 0;
let mut read_len = consts::REP_HEADER_LEN;
loop {
let buf = &recv_buf[pos..pos + read_len];
pos += read_len;
read_len = rpc_client.parse(&buf).unwrap().0;
if let Some(r) = req1.take_reply(&mut rpc_client) {
println!("reply ping: {:?}", r.unwrap());
break;
}
}
let req_buf = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
let mut req2 = cli::SendBytes::new(());
let send_buf_bytes = req2.request(&req_buf, &mut rpc_client, &mut send_buf).unwrap();
println!("request bytes: {:02x?}", &send_buf[..send_buf_bytes]);
// Send send_buf over the network
// [...]
// Read from the network into recv_buf
// [...]
// We fill recv_buf with some precalculated replies to simulate a server reply
recv_buf[..10].copy_from_slice(&[0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03]);
// Parse read bytes with rpc_client and try to match replies from each request
let mut pos = 0;
let mut read_len = consts::REP_HEADER_LEN;
loop {
let buf = &recv_buf[pos..pos + read_len];
pos += read_len;
read_len = rpc_client.parse(&buf).unwrap().0;
if let Some(r) = req2.take_reply(&mut rpc_client) {
println!("reply send_bytes: {:?}", r.unwrap());
break;
}
}