Move request/response serialization to YunqMessage trait.
This commit is contained in:
parent
e41f58c714
commit
6ae111dc66
|
@ -4,22 +4,14 @@ use alloc::vec::Vec;
|
|||
use mammoth::zion::z_cap_t;
|
||||
use mammoth::zion::ZError;
|
||||
|
||||
const SENTINEL: u32 = 0xBEEFDEAD;
|
||||
|
||||
pub fn call_endpoint<Req: YunqMessage, Resp: YunqMessage, const N: usize>(
|
||||
request_id: u64,
|
||||
req: &Req,
|
||||
byte_buffer: &mut ByteBuffer<N>,
|
||||
endpoint_cap: z_cap_t,
|
||||
) -> Result<Resp, ZError> {
|
||||
byte_buffer.write_at(0, SENTINEL)?;
|
||||
byte_buffer.write_at(8, request_id as u64)?;
|
||||
|
||||
let mut cap_buffer = Vec::new();
|
||||
|
||||
let length = req.serialize(byte_buffer, 16, &mut cap_buffer)?;
|
||||
|
||||
byte_buffer.write_at(4, (16 + length) as u32)?;
|
||||
let length = req.serialize_as_request(request_id, byte_buffer, &mut cap_buffer)?;
|
||||
|
||||
let reply_port_cap = mammoth::syscall::endpoint_send(
|
||||
endpoint_cap,
|
||||
|
@ -36,14 +28,5 @@ pub fn call_endpoint<Req: YunqMessage, Resp: YunqMessage, const N: usize>(
|
|||
cap_buffer.as_mut_slice(),
|
||||
)?;
|
||||
|
||||
if byte_buffer.at::<u32>(0)? != SENTINEL {
|
||||
return Err(ZError::INVALID_RESPONSE);
|
||||
}
|
||||
|
||||
let resp_code: u64 = byte_buffer.at(8)?;
|
||||
if resp_code != 0 {
|
||||
return Err(ZError::from(resp_code));
|
||||
}
|
||||
|
||||
Ok(Resp::parse(&byte_buffer, 16, &cap_buffer)?)
|
||||
Ok(Resp::parse_from_request(&byte_buffer, &cap_buffer)?)
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ use mammoth::zion::z_cap_t;
|
|||
use mammoth::zion::ZError;
|
||||
|
||||
pub const MESSAGE_IDENT: u32 = 0x33441122;
|
||||
const SENTINEL: u32 = 0xBEEFDEAD;
|
||||
pub const MESSAGE_HEADER_SIZE: usize = 24; // 4x uint32, 1x uint64
|
||||
|
||||
pub fn field_offset(offset: usize, field_index: usize) -> usize {
|
||||
|
@ -19,12 +20,47 @@ pub trait YunqMessage {
|
|||
where
|
||||
Self: Sized;
|
||||
|
||||
fn parse_from_request<const N: usize>(
|
||||
buf: &ByteBuffer<N>,
|
||||
caps: &Vec<z_cap_t>,
|
||||
) -> Result<Self, ZError>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
if buf.at::<u32>(0)? != SENTINEL {
|
||||
return Err(ZError::INVALID_RESPONSE);
|
||||
}
|
||||
|
||||
let resp_code: u64 = buf.at(8)?;
|
||||
if resp_code != 0 {
|
||||
return Err(ZError::from(resp_code));
|
||||
}
|
||||
|
||||
Ok(Self::parse(&buf, 16, &caps)?)
|
||||
}
|
||||
|
||||
fn serialize<const N: usize>(
|
||||
&self,
|
||||
buf: &mut ByteBuffer<N>,
|
||||
offset: usize,
|
||||
caps: &mut Vec<z_cap_t>,
|
||||
) -> Result<usize, ZError>;
|
||||
|
||||
fn serialize_as_request<const N: usize>(
|
||||
&self,
|
||||
request_id: u64,
|
||||
buf: &mut ByteBuffer<N>,
|
||||
caps: &mut Vec<z_cap_t>,
|
||||
) -> Result<usize, ZError> {
|
||||
buf.write_at(0, SENTINEL)?;
|
||||
buf.write_at(8, request_id as u64)?;
|
||||
|
||||
let length = self.serialize(buf, 16, caps)?;
|
||||
|
||||
buf.write_at(4, (16 + length) as u32)?;
|
||||
|
||||
Ok(length + 16)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Empty {}
|
||||
|
|
Loading…
Reference in New Issue