rust/lib-hedgewars-engine/src/ipc.rs
author unC0Rr
Wed, 21 Nov 2018 15:49:16 +0100
changeset 14265 f0c0d2d217c3
parent 14260 a239e4243cf9
child 14266 1b8c889027a3
permissions -rw-r--r--
IPC implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14265
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
     1
use hedgewars_engine_messages::{messages::*, parser::extract_message};
14260
a239e4243cf9 Start outlining ipc subsystem
unC0Rr
parents:
diff changeset
     2
use netbuf::*;
14265
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
     3
use std::io::*;
14260
a239e4243cf9 Start outlining ipc subsystem
unC0Rr
parents:
diff changeset
     4
a239e4243cf9 Start outlining ipc subsystem
unC0Rr
parents:
diff changeset
     5
pub struct IPC {
a239e4243cf9 Start outlining ipc subsystem
unC0Rr
parents:
diff changeset
     6
    in_buffer: Buf,
a239e4243cf9 Start outlining ipc subsystem
unC0Rr
parents:
diff changeset
     7
    out_buffer: Buf,
a239e4243cf9 Start outlining ipc subsystem
unC0Rr
parents:
diff changeset
     8
}
a239e4243cf9 Start outlining ipc subsystem
unC0Rr
parents:
diff changeset
     9
a239e4243cf9 Start outlining ipc subsystem
unC0Rr
parents:
diff changeset
    10
impl IPC {
a239e4243cf9 Start outlining ipc subsystem
unC0Rr
parents:
diff changeset
    11
    pub fn new() -> Self {
14265
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    12
        Self {
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    13
            in_buffer: Buf::new(),
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    14
            out_buffer: Buf::new(),
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    15
        }
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    16
    }
14260
a239e4243cf9 Start outlining ipc subsystem
unC0Rr
parents:
diff changeset
    17
14265
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    18
    pub fn send_message(&mut self, message: &EngineMessage) {
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    19
        self.out_buffer.write(&message.to_bytes()).unwrap();
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    20
    }
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    21
}
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    22
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    23
impl Write for IPC {
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    24
    fn write(&mut self, buf: &[u8]) -> Result<usize> {
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    25
        self.in_buffer.write(buf)
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    26
    }
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    27
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    28
    fn flush(&mut self) -> Result<()> {
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    29
        self.in_buffer.flush()
14260
a239e4243cf9 Start outlining ipc subsystem
unC0Rr
parents:
diff changeset
    30
    }
a239e4243cf9 Start outlining ipc subsystem
unC0Rr
parents:
diff changeset
    31
}
14265
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    32
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    33
impl Read for IPC {
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    34
    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    35
        let result = self.out_buffer.as_ref().read(buf);
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    36
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    37
        if let Ok(read_bytes) = result {
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    38
            self.out_buffer.consume(read_bytes);
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    39
        }
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    40
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    41
        result
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    42
    }
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    43
}
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    44
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    45
impl Iterator for IPC {
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    46
    type Item = EngineMessage;
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    47
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    48
    fn next(&mut self) -> Option<Self::Item> {
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    49
        let (consumed, message) = extract_message(&self.in_buffer[..])?;
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    50
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    51
        self.in_buffer.consume(consumed);
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    52
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    53
        Some(message)
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    54
    }
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    55
}