ableos/kernel/src/ipc/buffer.rs

89 lines
2.4 KiB
Rust

//!
use {
super::message::Message,
crossbeam_queue::{ArrayQueue, SegQueue},
};
pub enum BufferTypes {
Unbound(SegQueue<Message>),
Bound(ArrayQueue<Message>),
}
/// Interproccess buffer
pub struct IpcBuffer {
pub protocol: Protocol,
pub buffer: BufferTypes,
}
impl IpcBuffer {
pub fn new(bounded: bool, length: u64) -> Self {
log::debug!(
"New IPCBuffer\r
bounded: {}\r
length: {:?}\r",
bounded,
length
);
match (bounded, length) {
(false, a) => {
let buftype = BufferTypes::Unbound(SegQueue::new());
Self {
protocol: Protocol {},
buffer: buftype,
}
}
(true, length) => {
let buftype = BufferTypes::Bound(ArrayQueue::new(length as usize));
Self {
protocol: Protocol {},
buffer: buftype,
}
}
}
}
/// Validate a message to match the `IPC.protocol`
pub fn validate_messages(&mut self) -> Result<(), IpcError> {
Ok(())
}
pub fn push(&mut self, msg: Message) {
match &self.buffer {
BufferTypes::Unbound(buff) => buff.push(msg.clone()),
BufferTypes::Bound(buff) => {
let _ = buff.push(msg.clone());
}
};
}
pub fn pop(&mut self) -> Message {
let msg = match &self.buffer {
BufferTypes::Unbound(buff) => buff.pop(),
BufferTypes::Bound(buff) => buff.pop(),
};
match msg {
Some(msg) => return msg,
None => panic!("Recieving msg error. No messages!"),
}
}
}
/// Interprocess Communication Errors
pub enum IpcError {
/// An invalid message error returned to the sender
InvalidMessage,
}
/// TODO: define this, possibly as the binary form of the IDL
/// DEPEND: This depends on an IDL
pub struct Protocol {
// TODO: add in settings
// like `invalid_message_handler` with some options similar to
// `Deny` Drops the message
// `Allow` Allows invalid messages (This disables validators IPC side and relies on programs to handle invalid messages)
// `CustomFunct` a callback
// and `report_invalid_messages_to_sender`
// `True`
// `False`
// settings: PSettings,
}