Commit 76d13164 authored by Kaiden Fey's avatar Kaiden Fey Committed by Katharina Fey

qrpc-sdk: refactoring builder funciotns

parent 6491e57d
......@@ -7,11 +7,7 @@
//! In your service you will likely not need to consume this API. It
//! is included for debugging purposes.
use crate::{io::MsgReader, rpc::*, Service};
use byteorder::{BigEndian, ByteOrder};
use capnp::{message::Builder as Bld, serialize_packed};
use identity::Identity;
use socket2::Socket;
/// Generate an registry message for this service
pub fn register(service: &Service) -> (String, Vec<u8>) {
......@@ -27,44 +23,51 @@ pub fn upgrade(s: &Service, hash_id: Identity) -> (String, Vec<u8>) {
todo!()
}
/// This function is only included for debugging reasons. There's
/// This module is only included for debugging reasons. There's
/// basically no reason to call this function directly.
#[doc(hidden)]
pub fn _internal_to(target: String, data: Vec<u8>) -> Vec<u8> {
let mut msg = Bld::new_default();
let mut carrier = msg.init_root::<carrier::Builder>();
carrier.set_target(&target);
carrier.set_data(&data);
pub mod _internal {
use crate::{io::MsgReader, rpc::*, Service};
use byteorder::{BigEndian, ByteOrder};
use capnp::{message::Builder as Bld, serialize_packed};
use socket2::Socket;
let mut buffer = vec![];
serialize_packed::write_message(&mut buffer, &msg).unwrap();
pub fn to(target: String, data: Vec<u8>) -> Vec<u8> {
let mut msg = Bld::new_default();
let mut carrier = msg.init_root::<carrier::Builder>();
carrier.set_target(&target);
carrier.set_data(&data);
let len = buffer.len();
let mut message = vec![8];
BigEndian::write_u64(&mut message, len as u64);
let mut buffer = vec![];
serialize_packed::write_message(&mut buffer, &msg).unwrap();
message.append(&mut buffer);
message
}
let len = buffer.len();
let mut message = vec![8];
BigEndian::write_u64(&mut message, len as u64);
pub fn _internal_from(socket: &Socket) -> (String, Vec<u8>) {
let mut len = vec![0; 8];
loop {
let (l, a) = socket.peek_from(&mut len).unwrap();
if l == 8 {
break;
}
message.append(&mut buffer);
message
}
let (_, _) = socket.recv_from(&mut len).unwrap();
let len = BigEndian::read_u64(&len);
let mut buffer = vec![0; len as usize];
socket.recv_from(&mut buffer).unwrap();
pub fn from(socket: &Socket) -> (String, Vec<u8>) {
let mut len = vec![0; 8];
loop {
let (l, a) = socket.peek_from(&mut len).unwrap();
if l == 8 {
break;
}
}
let msg = MsgReader::new(buffer).unwrap();
let carrier: carrier::Reader = msg.get_root().unwrap();
let target = carrier.get_target().unwrap();
let data = carrier.get_data().unwrap();
let (_, _) = socket.recv_from(&mut len).unwrap();
let len = BigEndian::read_u64(&len);
let mut buffer = vec![0; len as usize];
socket.recv_from(&mut buffer).unwrap();
(target.to_string(), data.to_vec())
let msg = MsgReader::new(buffer).unwrap();
let carrier: carrier::Reader = msg.get_root().unwrap();
let target = carrier.get_target().unwrap();
let data = carrier.get_data().unwrap();
(target.to_string(), data.to_vec())
}
}
......@@ -106,13 +106,10 @@ impl RpcSocket {
F: Future<Output = RpcResult<T>> + Send + 'static,
S: Into<String>,
{
let msg = builders::_internal_to(target.into(), msg);
let msg = builders::_internal::to(target.into(), msg);
let _self = Arc::clone(self);
self.with_timeout(async move {
// let len = match _self.inner.send_to(&msg, &_self.addr) {
// Ok(l) => l,
// Err(e) => return Err(RpcError::Other(e.to_string())),
// };
let (_, buf) = builders::_internal::from(&_self.inner);
todo!()
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment