Implement yunq server in rust.
This commit is contained in:
parent
dbc4e7e2ad
commit
76f8795a46
|
@ -114,6 +114,7 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"mammoth",
|
"mammoth",
|
||||||
"yellowstone",
|
"yellowstone",
|
||||||
|
"yunq",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
@ -508,7 +508,7 @@ pub struct ZReplyPortSendReq {
|
||||||
pub num_bytes: u64,
|
pub num_bytes: u64,
|
||||||
pub data: *const ::core::ffi::c_void,
|
pub data: *const ::core::ffi::c_void,
|
||||||
pub num_caps: u64,
|
pub num_caps: u64,
|
||||||
pub caps: *mut z_cap_t,
|
pub caps: *const z_cap_t,
|
||||||
}
|
}
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Debug, Copy, Clone)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
|
|
|
@ -122,6 +122,17 @@ pub fn port_poll(
|
||||||
Ok((num_bytes, num_caps))
|
Ok((num_bytes, num_caps))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn endpoint_create() -> Result<z_cap_t, ZError> {
|
||||||
|
let mut endpoint_cap: z_cap_t = 0;
|
||||||
|
syscall(
|
||||||
|
zion::kZionEndpointCreate,
|
||||||
|
&zion::ZEndpointCreateReq {
|
||||||
|
endpoint_cap: &mut endpoint_cap,
|
||||||
|
},
|
||||||
|
)?;
|
||||||
|
Ok(endpoint_cap)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn endpoint_send(
|
pub fn endpoint_send(
|
||||||
endpoint_cap: z_cap_t,
|
endpoint_cap: z_cap_t,
|
||||||
bytes: &[u8],
|
bytes: &[u8],
|
||||||
|
@ -134,7 +145,7 @@ pub fn endpoint_send(
|
||||||
endpoint_cap,
|
endpoint_cap,
|
||||||
data: bytes.as_ptr() as *const c_void,
|
data: bytes.as_ptr() as *const c_void,
|
||||||
num_bytes: bytes.len() as u64,
|
num_bytes: bytes.len() as u64,
|
||||||
reply_port_cap: &mut reply_port_cap as *mut z_cap_t,
|
reply_port_cap: &mut reply_port_cap,
|
||||||
};
|
};
|
||||||
|
|
||||||
syscall(zion::kZionEndpointSend, &send_req)?;
|
syscall(zion::kZionEndpointSend, &send_req)?;
|
||||||
|
@ -142,6 +153,45 @@ pub fn endpoint_send(
|
||||||
Ok(reply_port_cap)
|
Ok(reply_port_cap)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn endpoint_recv(
|
||||||
|
endpoint_cap: z_cap_t,
|
||||||
|
bytes: &mut [u8],
|
||||||
|
caps: &mut [z_cap_t],
|
||||||
|
) -> Result<(u64, u64, z_cap_t), ZError> {
|
||||||
|
let mut num_bytes = bytes.len() as u64;
|
||||||
|
let mut num_caps = caps.len() as u64;
|
||||||
|
let mut reply_port_cap = 0;
|
||||||
|
let recv_req = zion::ZEndpointRecvReq {
|
||||||
|
endpoint_cap,
|
||||||
|
data: bytes.as_mut_ptr() as *mut c_void,
|
||||||
|
num_bytes: &mut num_bytes,
|
||||||
|
caps: caps.as_mut_ptr(),
|
||||||
|
num_caps: &mut num_caps,
|
||||||
|
reply_port_cap: &mut reply_port_cap,
|
||||||
|
};
|
||||||
|
|
||||||
|
syscall(zion::kZionEndpointRecv, &recv_req)?;
|
||||||
|
|
||||||
|
Ok((num_bytes, num_caps, reply_port_cap))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn reply_port_send(
|
||||||
|
reply_port_cap: z_cap_t,
|
||||||
|
bytes: &[u8],
|
||||||
|
caps: &[z_cap_t],
|
||||||
|
) -> Result<(), ZError> {
|
||||||
|
syscall(
|
||||||
|
zion::kZionReplyPortSend,
|
||||||
|
&zion::ZReplyPortSendReq {
|
||||||
|
reply_port_cap,
|
||||||
|
data: bytes.as_ptr() as *const c_void,
|
||||||
|
num_bytes: bytes.len() as u64,
|
||||||
|
caps: caps.as_ptr(),
|
||||||
|
num_caps: caps.len() as u64,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn reply_port_recv(
|
pub fn reply_port_recv(
|
||||||
reply_port_cap: z_cap_t,
|
reply_port_cap: z_cap_t,
|
||||||
bytes: &mut [u8],
|
bytes: &mut [u8],
|
||||||
|
@ -152,9 +202,9 @@ pub fn reply_port_recv(
|
||||||
let recv_req = zion::ZReplyPortRecvReq {
|
let recv_req = zion::ZReplyPortRecvReq {
|
||||||
reply_port_cap,
|
reply_port_cap,
|
||||||
caps: caps.as_mut_ptr(),
|
caps: caps.as_mut_ptr(),
|
||||||
num_caps: &mut num_caps as *mut u64,
|
num_caps: &mut num_caps,
|
||||||
data: bytes.as_mut_ptr() as *mut c_void,
|
data: bytes.as_mut_ptr() as *mut c_void,
|
||||||
num_bytes: &mut num_bytes as *mut u64,
|
num_bytes: &mut num_bytes,
|
||||||
};
|
};
|
||||||
|
|
||||||
syscall(zion::kZionReplyPortRecv, &recv_req)?;
|
syscall(zion::kZionReplyPortRecv, &recv_req)?;
|
||||||
|
|
|
@ -2,39 +2,40 @@ use crate::syscall;
|
||||||
use crate::zion;
|
use crate::zion;
|
||||||
use crate::zion::z_cap_t;
|
use crate::zion::z_cap_t;
|
||||||
|
|
||||||
|
use alloc::boxed::Box;
|
||||||
use core::ffi::c_void;
|
use core::ffi::c_void;
|
||||||
|
|
||||||
pub type ThreadEntry = fn(*const c_void) -> ();
|
pub type ThreadEntry = fn(*const c_void) -> ();
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
extern "C" fn internal_entry_point(entry_ptr: *const ThreadEntry, arg1: *const c_void) -> ! {
|
extern "C" fn internal_entry_point(thread_ptr: *const Thread, arg1: *const c_void) -> ! {
|
||||||
let entry = unsafe { *entry_ptr };
|
let thread: &Thread = unsafe { thread_ptr.as_ref().expect("Failed to unwrap thread ref") };
|
||||||
|
|
||||||
entry(arg1);
|
(thread.entry)(arg1);
|
||||||
|
|
||||||
syscall::thread_exit()
|
syscall::thread_exit()
|
||||||
}
|
}
|
||||||
// TODO: Add a Drop implementation that kills this thread and drops its capability.
|
// TODO: Add a Drop implementation that kills this thread and drops its capability.
|
||||||
pub struct Thread<'a> {
|
pub struct Thread {
|
||||||
cap: z_cap_t,
|
cap: z_cap_t,
|
||||||
// This field only exists to ensure that the entry reference will outlive the thread object
|
// This field only exists to ensure that the entry reference will outlive the thread object
|
||||||
// itself.
|
// itself.
|
||||||
_entry: &'a ThreadEntry,
|
entry: ThreadEntry,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Thread<'a> {
|
impl Thread {
|
||||||
pub fn spawn(entry: &'a ThreadEntry, arg1: *const c_void) -> Result<Self, zion::ZError> {
|
pub fn spawn(entry: ThreadEntry, arg1: *const c_void) -> Result<Box<Self>, zion::ZError> {
|
||||||
let proc_cap = unsafe { crate::init::SELF_PROC_CAP };
|
let proc_cap = unsafe { crate::init::SELF_PROC_CAP };
|
||||||
let cap = syscall::thread_create(proc_cap)?;
|
let cap = syscall::thread_create(proc_cap)?;
|
||||||
|
let thread = Box::new(Self { cap, entry });
|
||||||
syscall::thread_start(
|
syscall::thread_start(
|
||||||
cap,
|
cap,
|
||||||
internal_entry_point as u64,
|
internal_entry_point as u64,
|
||||||
entry as *const ThreadEntry as u64,
|
thread.as_ref() as *const Thread as u64,
|
||||||
arg1 as u64,
|
arg1 as u64,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(Self { cap, _entry: entry })
|
Ok(thread)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn join(&self) -> Result<(), zion::ZError> {
|
pub fn join(&self) -> Result<(), zion::ZError> {
|
||||||
|
|
|
@ -28,5 +28,11 @@ pub fn call_endpoint<Req: YunqMessage, Resp: YunqMessage, const N: usize>(
|
||||||
cap_buffer.as_mut_slice(),
|
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)?)
|
Ok(Resp::parse_from_request(&byte_buffer, &cap_buffer)?)
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ extern crate alloc;
|
||||||
mod buffer;
|
mod buffer;
|
||||||
pub mod client;
|
pub mod client;
|
||||||
pub mod message;
|
pub mod message;
|
||||||
|
pub mod server;
|
||||||
|
|
||||||
pub use buffer::ByteBuffer;
|
pub use buffer::ByteBuffer;
|
||||||
pub use message::YunqMessage;
|
pub use message::YunqMessage;
|
||||||
|
|
|
@ -4,13 +4,23 @@ use mammoth::zion::z_cap_t;
|
||||||
use mammoth::zion::ZError;
|
use mammoth::zion::ZError;
|
||||||
|
|
||||||
pub const MESSAGE_IDENT: u32 = 0x33441122;
|
pub const MESSAGE_IDENT: u32 = 0x33441122;
|
||||||
const SENTINEL: u32 = 0xBEEFDEAD;
|
|
||||||
pub const MESSAGE_HEADER_SIZE: usize = 24; // 4x uint32, 1x uint64
|
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 {
|
pub fn field_offset(offset: usize, field_index: usize) -> usize {
|
||||||
offset + MESSAGE_HEADER_SIZE + (8 * field_index)
|
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 {
|
pub trait YunqMessage {
|
||||||
fn parse<const N: usize>(
|
fn parse<const N: usize>(
|
||||||
buf: &ByteBuffer<N>,
|
buf: &ByteBuffer<N>,
|
||||||
|
@ -31,11 +41,6 @@ pub trait YunqMessage {
|
||||||
return Err(ZError::INVALID_RESPONSE);
|
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)?)
|
Ok(Self::parse(&buf, 16, &caps)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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>;
|
||||||
|
}
|
|
@ -6,3 +6,4 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
mammoth = { path = "../../lib/mammoth" }
|
mammoth = { path = "../../lib/mammoth" }
|
||||||
yellowstone = { path = "../../lib/yellowstone" }
|
yellowstone = { path = "../../lib/yellowstone" }
|
||||||
|
yunq = { path = "../../lib/yunq" }
|
||||||
|
|
|
@ -8,11 +8,46 @@ use mammoth::debug;
|
||||||
use mammoth::define_entry;
|
use mammoth::define_entry;
|
||||||
use mammoth::thread;
|
use mammoth::thread;
|
||||||
use mammoth::zion::z_err_t;
|
use mammoth::zion::z_err_t;
|
||||||
|
use mammoth::zion::ZError;
|
||||||
use yellowstone::GetEndpointRequest;
|
use yellowstone::GetEndpointRequest;
|
||||||
use yellowstone::YellowstoneClient;
|
use yellowstone::YellowstoneClient;
|
||||||
|
use yellowstone::YellowstoneServer;
|
||||||
|
use yunq::server::YunqServer;
|
||||||
|
|
||||||
define_entry!();
|
define_entry!();
|
||||||
|
|
||||||
|
struct YellowstoneImpl {}
|
||||||
|
|
||||||
|
impl yellowstone::YellowstoneServerHandler for YellowstoneImpl {
|
||||||
|
fn register_endpoint(&self, req: &yellowstone::RegisterEndpointRequest) -> Result<(), ZError> {
|
||||||
|
debug!("{}", req.endpoint_name);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_endpoint(&self, req: &GetEndpointRequest) -> Result<yellowstone::Endpoint, ZError> {
|
||||||
|
debug!("{}", req.endpoint_name);
|
||||||
|
Ok(yellowstone::Endpoint {
|
||||||
|
endpoint: unsafe { mammoth::init::SELF_PROC_CAP },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_ahci_info(&self) -> Result<yellowstone::AhciInfo, ZError> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_xhci_info(&self) -> Result<yellowstone::XhciInfo, ZError> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_framebuffer_info(&self) -> Result<yellowstone::FramebufferInfo, ZError> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_denali(&self) -> Result<yellowstone::DenaliInfo, ZError> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn main() -> z_err_t {
|
pub extern "C" fn main() -> z_err_t {
|
||||||
debug!("Testing!");
|
debug!("Testing!");
|
||||||
|
@ -33,9 +68,25 @@ pub extern "C" fn main() -> z_err_t {
|
||||||
let e: thread::ThreadEntry = |_| {
|
let e: thread::ThreadEntry = |_| {
|
||||||
debug!("Testing 1 2 3");
|
debug!("Testing 1 2 3");
|
||||||
};
|
};
|
||||||
let t = thread::Thread::spawn(&e, core::ptr::null()).expect("Failed to spawn thread");
|
let t = thread::Thread::spawn(e, core::ptr::null()).expect("Failed to spawn thread");
|
||||||
|
|
||||||
t.join().expect("Failed to wait.");
|
t.join().expect("Failed to wait.");
|
||||||
|
|
||||||
|
let server = YellowstoneServer::new(YellowstoneImpl {}).expect("Failed to create server");
|
||||||
|
|
||||||
|
let mut yellowstone = YellowstoneClient::new(server.endpoint_cap());
|
||||||
|
|
||||||
|
let t = server.run_server().expect("Failed to start server");
|
||||||
|
|
||||||
|
let endpoint = yellowstone
|
||||||
|
.get_endpoint(&GetEndpointRequest {
|
||||||
|
endpoint_name: "test".to_string(),
|
||||||
|
})
|
||||||
|
.expect("Failed to get endpoint");
|
||||||
|
|
||||||
|
debug!("{:#x}", endpoint.endpoint);
|
||||||
|
|
||||||
|
t.join().expect("Failed to wait");
|
||||||
|
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,7 +50,7 @@ fn generate_method(method: &Method) -> TokenStream {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_interface(interface: &Interface) -> TokenStream {
|
fn generate_client(interface: &Interface) -> TokenStream {
|
||||||
let client_name = interface.name.clone() + "Client";
|
let client_name = interface.name.clone() + "Client";
|
||||||
let name = ident(&client_name);
|
let name = ident(&client_name);
|
||||||
let methods = interface.methods.iter().map(|m| generate_method(&m));
|
let methods = interface.methods.iter().map(|m| generate_method(&m));
|
||||||
|
@ -73,17 +73,144 @@ fn generate_interface(interface: &Interface) -> TokenStream {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn generate_server_case(method: &Method) -> TokenStream {
|
||||||
|
let id = proc_macro2::Literal::u64_suffixed(method.number);
|
||||||
|
let name = ident(&method.name.to_case(Case::Snake));
|
||||||
|
let maybe_req = method.request.clone().map(|r| ident(&r));
|
||||||
|
let maybe_resp = method.response.clone().map(|r| ident(&r));
|
||||||
|
match (maybe_req, maybe_resp) {
|
||||||
|
(Some(req), Some(_)) => quote! {
|
||||||
|
#id => {
|
||||||
|
let req = #req::parse_from_request(byte_buffer, cap_buffer)?;
|
||||||
|
let resp = self.handler.#name(&req)?;
|
||||||
|
cap_buffer.resize(0, 0);
|
||||||
|
let resp_len = resp.serialize_as_request(0, byte_buffer, cap_buffer)?;
|
||||||
|
Ok(resp_len)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
(Some(req), None) => quote! {
|
||||||
|
#id => {
|
||||||
|
let req = #req::parse_from_request(byte_buffer, cap_buffer)?;
|
||||||
|
self.handler.#name(&req)?;
|
||||||
|
cap_buffer.resize(0, 0);
|
||||||
|
// TODO: Implement serialization for EmptyMessage so this is less hacky.
|
||||||
|
yunq::message::serialize_error(byte_buffer, ZError::from(0));
|
||||||
|
Ok(0x10)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
(None, Some(_)) => quote! {
|
||||||
|
#id => {
|
||||||
|
let resp = self.handler.#name()?;
|
||||||
|
cap_buffer.resize(0, 0);
|
||||||
|
let resp_len = resp.serialize_as_request(0, byte_buffer, cap_buffer)?;
|
||||||
|
Ok(resp_len)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_server_method(method: &Method) -> TokenStream {
|
||||||
|
let name = ident(&method.name.to_case(Case::Snake));
|
||||||
|
let maybe_req = method.request.clone().map(|r| ident(&r));
|
||||||
|
let maybe_resp = method.response.clone().map(|r| ident(&r));
|
||||||
|
match (maybe_req, maybe_resp) {
|
||||||
|
(Some(req), Some(resp)) => quote! {
|
||||||
|
fn #name (&self, req: & #req) -> Result<#resp, ZError>;
|
||||||
|
},
|
||||||
|
(Some(req), None) => quote! {
|
||||||
|
fn #name (&self, req: & #req) -> Result<(), ZError>;
|
||||||
|
},
|
||||||
|
(None, Some(resp)) => quote! {
|
||||||
|
fn #name (&self) -> Result<#resp, ZError>;
|
||||||
|
},
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_server(interface: &Interface) -> TokenStream {
|
||||||
|
let server_name = ident(&(interface.name.clone() + "Server"));
|
||||||
|
let server_trait = ident(&(interface.name.clone() + "ServerHandler"));
|
||||||
|
let server_trait_methods = interface.methods.iter().map(|m| generate_server_method(&m));
|
||||||
|
let server_match_cases = interface.methods.iter().map(|m| generate_server_case(&m));
|
||||||
|
quote! {
|
||||||
|
pub trait #server_trait {
|
||||||
|
#(#server_trait_methods)*
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct #server_name<T: #server_trait> {
|
||||||
|
endpoint_cap: z_cap_t,
|
||||||
|
handler: T
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: #server_trait> #server_name<T> {
|
||||||
|
pub fn new(handler: T) -> Result<Self, ZError> {
|
||||||
|
Ok(Self {
|
||||||
|
endpoint_cap: syscall::endpoint_create()?,
|
||||||
|
handler,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run_server(&self) -> Result<Box<thread::Thread>, ZError> {
|
||||||
|
let thread_entry = |server_ptr: *const c_void| {
|
||||||
|
let server = unsafe { (server_ptr as *const #server_name<T>).as_ref().expect("Failed to convert to server") };
|
||||||
|
server.server_loop();
|
||||||
|
};
|
||||||
|
thread::Thread::spawn(
|
||||||
|
thread_entry,
|
||||||
|
self as *const Self as *const core::ffi::c_void,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: #server_trait> yunq::server::YunqServer for #server_name<T> {
|
||||||
|
fn endpoint_cap(&self) -> z_cap_t {
|
||||||
|
self.endpoint_cap
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_request(
|
||||||
|
&self,
|
||||||
|
method_number: u64,
|
||||||
|
byte_buffer: &mut ByteBuffer<1024>,
|
||||||
|
cap_buffer: &mut Vec<z_cap_t>,
|
||||||
|
) -> Result<usize, ZError> {
|
||||||
|
match method_number {
|
||||||
|
#(#server_match_cases)*
|
||||||
|
|
||||||
|
_ => Err(ZError::UNIMPLEMENTED)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_interface(interface: &Interface) -> TokenStream {
|
||||||
|
let client = generate_client(interface);
|
||||||
|
let server = generate_server(interface);
|
||||||
|
quote! {
|
||||||
|
#client
|
||||||
|
|
||||||
|
#server
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn generate_code(ast: &Vec<Decl>) -> String {
|
pub fn generate_code(ast: &Vec<Decl>) -> String {
|
||||||
let prelude = quote! {
|
let prelude = quote! {
|
||||||
|
|
||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
|
|
||||||
|
use alloc::boxed::Box;
|
||||||
use alloc::string::String;
|
use alloc::string::String;
|
||||||
use alloc::string::ToString;
|
use alloc::string::ToString;
|
||||||
|
use alloc::vec::Vec;
|
||||||
|
use core::ffi::c_void;
|
||||||
|
use mammoth::syscall;
|
||||||
|
use mammoth::thread;
|
||||||
use mammoth::zion::z_cap_t;
|
use mammoth::zion::z_cap_t;
|
||||||
use mammoth::zion::ZError;
|
use mammoth::zion::ZError;
|
||||||
use yunq::ByteBuffer;
|
use yunq::ByteBuffer;
|
||||||
use yunq::YunqMessage;
|
use yunq::YunqMessage;
|
||||||
|
use yunq::server::YunqServer;
|
||||||
use yunq_derive::YunqMessage;
|
use yunq_derive::YunqMessage;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -56,7 +56,7 @@ SYS6(EndpointSend, z_cap_t, endpoint_cap, uint64_t, num_bytes, const void*,
|
||||||
SYS6(EndpointRecv, z_cap_t, endpoint_cap, uint64_t*, num_bytes, void*, data,
|
SYS6(EndpointRecv, z_cap_t, endpoint_cap, uint64_t*, num_bytes, void*, data,
|
||||||
uint64_t*, num_caps, z_cap_t*, caps, z_cap_t*, reply_port_cap);
|
uint64_t*, num_caps, z_cap_t*, caps, z_cap_t*, reply_port_cap);
|
||||||
SYS5(ReplyPortSend, z_cap_t, reply_port_cap, uint64_t, num_bytes, const void*,
|
SYS5(ReplyPortSend, z_cap_t, reply_port_cap, uint64_t, num_bytes, const void*,
|
||||||
data, uint64_t, num_caps, z_cap_t*, caps);
|
data, uint64_t, num_caps, const z_cap_t*, caps);
|
||||||
SYS5(ReplyPortRecv, z_cap_t, reply_port_cap, uint64_t*, num_bytes, void*, data,
|
SYS5(ReplyPortRecv, z_cap_t, reply_port_cap, uint64_t*, num_bytes, void*, data,
|
||||||
uint64_t*, num_caps, z_cap_t*, caps);
|
uint64_t*, num_caps, z_cap_t*, caps);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue