1
0
Fork 0
forked from AbleOS/ableos
ableos_time/ableos/src/ipc/socket.rs

44 lines
965 B
Rust
Raw Normal View History

2022-07-28 10:57:28 -05:00
// SEEALSO: https://git.ablecorp.us/able/ableos/src/branch/master/ableos/src/relib/network/socket.rs
2022-07-29 06:13:26 -05:00
use super::IPCError;
2022-07-28 20:15:02 -05:00
#[derive(Debug)]
2022-07-28 10:57:28 -05:00
pub struct Socket {
2022-07-29 06:13:26 -05:00
pub public: bool,
name: String,
2022-07-28 10:57:28 -05:00
stream: Vec<u8>,
}
impl Socket {
2022-07-29 06:13:26 -05:00
pub fn new<S: Into<String>>(name: S) -> Self {
2022-07-28 20:15:02 -05:00
Self {
2022-07-29 06:13:26 -05:00
public: false,
name: name.into(),
2022-07-28 20:15:02 -05:00
stream: vec![],
}
2022-07-28 10:57:28 -05:00
}
2022-07-29 06:13:26 -05:00
// <S: Into<String>>(name: S)
pub fn write<S: Into<Vec<u8>>>(&mut self, data: S) -> Result<(), IPCError> {
for c in data.into() {
2022-07-28 10:57:28 -05:00
self.stream.push(c as u8);
}
Ok(())
}
2022-07-29 06:13:26 -05:00
pub fn read(&mut self) -> Result<Vec<u8>, IPCError> {
2022-07-28 10:57:28 -05:00
if self.stream.len() != 0 {
let skt = self.stream.clone();
self.stream = vec![];
return Ok(skt);
}
2022-07-29 06:13:26 -05:00
return Err(IPCError::EmptySocket);
2022-07-28 10:57:28 -05:00
}
2022-07-28 20:15:02 -05:00
2022-07-29 06:13:26 -05:00
pub fn rename(&mut self, name: String) {
self.name = name;
2022-07-28 20:15:02 -05:00
}
2022-07-28 10:57:28 -05:00
}