Implement yunq server in rust.
This commit is contained in:
parent
dbc4e7e2ad
commit
76f8795a46
12 changed files with 312 additions and 23 deletions
|
|
@ -28,5 +28,11 @@ pub fn call_endpoint<Req: YunqMessage, Resp: YunqMessage, const N: usize>(
|
|||
cap_buffer.as_mut_slice(),
|
||||
)?;
|
||||
|
||||
let resp_code: u64 = byte_buffer.at(8)?;
|
||||
|
||||
if resp_code != 0 {
|
||||
return Err(ZError::from(resp_code));
|
||||
}
|
||||
|
||||
Ok(Resp::parse_from_request(&byte_buffer, &cap_buffer)?)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ extern crate alloc;
|
|||
mod buffer;
|
||||
pub mod client;
|
||||
pub mod message;
|
||||
pub mod server;
|
||||
|
||||
pub use buffer::ByteBuffer;
|
||||
pub use message::YunqMessage;
|
||||
|
|
|
|||
|
|
@ -4,13 +4,23 @@ 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
|
||||
const SENTINEL: u32 = 0xBEEFDEAD;
|
||||
const SERIALIZE_HEADER_SIZE: u32 = 0x10;
|
||||
|
||||
pub fn field_offset(offset: usize, field_index: usize) -> usize {
|
||||
offset + MESSAGE_HEADER_SIZE + (8 * field_index)
|
||||
}
|
||||
|
||||
pub fn serialize_error<const N: usize>(buf: &mut ByteBuffer<N>, err: ZError) {
|
||||
buf.write_at(0, SENTINEL)
|
||||
.expect("Failed to serialize SENTINEL");
|
||||
buf.write_at(4, SERIALIZE_HEADER_SIZE)
|
||||
.expect("Failed to serialize size");
|
||||
buf.write_at(8, err as u64)
|
||||
.expect("Failed to serialize error");
|
||||
}
|
||||
|
||||
pub trait YunqMessage {
|
||||
fn parse<const N: usize>(
|
||||
buf: &ByteBuffer<N>,
|
||||
|
|
@ -31,11 +41,6 @@ pub trait YunqMessage {
|
|||
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)?)
|
||||
}
|
||||
|
||||
|
|
|
|||
46
rust/lib/yunq/src/server.rs
Normal file
46
rust/lib/yunq/src/server.rs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
use crate::buffer::ByteBuffer;
|
||||
use alloc::vec::Vec;
|
||||
use mammoth::syscall;
|
||||
use mammoth::zion::z_cap_t;
|
||||
use mammoth::zion::ZError;
|
||||
|
||||
pub trait YunqServer {
|
||||
fn server_loop(&self) {
|
||||
loop {
|
||||
let mut byte_buffer = ByteBuffer::<1024>::new();
|
||||
let mut cap_buffer = vec![0; 10];
|
||||
let (_, _, reply_port_cap) = syscall::endpoint_recv(
|
||||
self.endpoint_cap(),
|
||||
byte_buffer.mut_slice(),
|
||||
&mut cap_buffer,
|
||||
)
|
||||
.expect("Failed to call endpoint recv");
|
||||
|
||||
let method = byte_buffer
|
||||
.at::<u64>(8)
|
||||
.expect("Failed to access request length.");
|
||||
let resp = self.handle_request(method, &mut byte_buffer, &mut cap_buffer);
|
||||
match resp {
|
||||
Ok(resp_len) => syscall::reply_port_send(
|
||||
reply_port_cap,
|
||||
byte_buffer.slice(resp_len),
|
||||
&cap_buffer,
|
||||
)
|
||||
.expect("Failed to reply"),
|
||||
Err(err) => {
|
||||
crate::message::serialize_error(&mut byte_buffer, err);
|
||||
syscall::reply_port_send(reply_port_cap, &byte_buffer.slice(0x10), &[])
|
||||
.expect("Failed to reply w/ error")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn endpoint_cap(&self) -> z_cap_t;
|
||||
fn handle_request(
|
||||
&self,
|
||||
method_number: u64,
|
||||
byte_buffer: &mut ByteBuffer<1024>,
|
||||
cap_buffer: &mut Vec<z_cap_t>,
|
||||
) -> Result<usize, ZError>;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue