2023-06-13 06:00:11 -05:00
|
|
|
//!
|
2023-05-15 02:19:34 -05:00
|
|
|
|
2023-06-13 06:00:11 -05:00
|
|
|
use {
|
2024-03-22 05:13:17 -05:00
|
|
|
super::{message::Message, protocol::Protocol},
|
2023-06-13 06:00:11 -05:00
|
|
|
crossbeam_queue::{ArrayQueue, SegQueue},
|
|
|
|
};
|
2023-09-07 14:36:53 -05:00
|
|
|
pub enum BufferTypes {
|
2023-05-15 02:19:34 -05:00
|
|
|
Unbound(SegQueue<Message>),
|
|
|
|
Bound(ArrayQueue<Message>),
|
|
|
|
}
|
2023-06-13 06:00:11 -05:00
|
|
|
/// Interproccess buffer
|
2023-05-15 02:19:34 -05:00
|
|
|
pub struct IpcBuffer {
|
2023-10-29 07:27:10 -05:00
|
|
|
pub protocol: Protocol,
|
|
|
|
pub buffer: BufferTypes,
|
2023-05-15 02:19:34 -05:00
|
|
|
}
|
|
|
|
|
2023-05-28 04:51:51 -05:00
|
|
|
impl IpcBuffer {
|
2023-09-09 02:35:16 -05:00
|
|
|
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 {
|
2024-03-22 05:13:17 -05:00
|
|
|
protocol: Protocol::void(),
|
2023-09-09 02:35:16 -05:00
|
|
|
buffer: buftype,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(true, length) => {
|
|
|
|
let buftype = BufferTypes::Bound(ArrayQueue::new(length as usize));
|
|
|
|
Self {
|
2024-03-22 05:13:17 -05:00
|
|
|
protocol: Protocol::void(),
|
2023-09-09 02:35:16 -05:00
|
|
|
buffer: buftype,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-13 06:00:11 -05:00
|
|
|
/// Validate a message to match the `IPC.protocol`
|
2023-05-28 04:51:51 -05:00
|
|
|
pub fn validate_messages(&mut self) -> Result<(), IpcError> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-10-29 07:27:10 -05:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2024-08-19 13:13:58 -05:00
|
|
|
pub fn pop(&mut self) -> Result<Message, IpcError> {
|
2023-10-29 07:27:10 -05:00
|
|
|
let msg = match &self.buffer {
|
|
|
|
BufferTypes::Unbound(buff) => buff.pop(),
|
|
|
|
BufferTypes::Bound(buff) => buff.pop(),
|
|
|
|
};
|
|
|
|
match msg {
|
2024-08-19 13:13:58 -05:00
|
|
|
Some(msg) => return Ok(msg),
|
|
|
|
None => return Err(IpcError::NoMessagesInBuffer),
|
2023-10-29 07:27:10 -05:00
|
|
|
}
|
|
|
|
}
|
2023-05-28 04:51:51 -05:00
|
|
|
}
|
2023-06-13 06:00:11 -05:00
|
|
|
/// Interprocess Communication Errors
|
2024-08-19 13:13:58 -05:00
|
|
|
#[derive(Debug)]
|
2023-05-28 04:51:51 -05:00
|
|
|
pub enum IpcError {
|
2023-06-13 06:00:11 -05:00
|
|
|
/// An invalid message error returned to the sender
|
2023-05-28 04:51:51 -05:00
|
|
|
InvalidMessage,
|
2024-08-19 13:13:58 -05:00
|
|
|
NoMessagesInBuffer,
|
2023-05-28 04:51:51 -05:00
|
|
|
}
|