14260
|
1 |
use hedgewars_engine_messages::{messages::*, parser::extract_message};
|
14255
|
2 |
use netbuf::*;
|
14260
|
3 |
use std::io::*;
|
14255
|
4 |
|
|
5 |
pub struct IPC {
|
|
6 |
in_buffer: Buf,
|
|
7 |
out_buffer: Buf,
|
|
8 |
}
|
|
9 |
|
|
10 |
impl IPC {
|
|
11 |
pub fn new() -> Self {
|
14260
|
12 |
Self {
|
|
13 |
in_buffer: Buf::new(),
|
|
14 |
out_buffer: Buf::new(),
|
|
15 |
}
|
|
16 |
}
|
14255
|
17 |
|
14260
|
18 |
pub fn send_message(&mut self, message: &EngineMessage) {
|
|
19 |
self.out_buffer.write(&message.to_bytes()).unwrap();
|
|
20 |
}
|
|
21 |
}
|
|
22 |
|
|
23 |
impl Write for IPC {
|
|
24 |
fn write(&mut self, buf: &[u8]) -> Result<usize> {
|
|
25 |
self.in_buffer.write(buf)
|
|
26 |
}
|
|
27 |
|
|
28 |
fn flush(&mut self) -> Result<()> {
|
|
29 |
self.in_buffer.flush()
|
14255
|
30 |
}
|
|
31 |
}
|
14260
|
32 |
|
|
33 |
impl Read for IPC {
|
|
34 |
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
|
14261
|
35 |
let read_bytes = self.out_buffer.as_ref().read(buf)?;
|
14260
|
36 |
|
14261
|
37 |
self.out_buffer.consume(read_bytes);
|
14260
|
38 |
|
14261
|
39 |
Ok(read_bytes)
|
14260
|
40 |
}
|
|
41 |
}
|
|
42 |
|
|
43 |
impl Iterator for IPC {
|
|
44 |
type Item = EngineMessage;
|
|
45 |
|
|
46 |
fn next(&mut self) -> Option<Self::Item> {
|
|
47 |
let (consumed, message) = extract_message(&self.in_buffer[..])?;
|
|
48 |
|
|
49 |
self.in_buffer.consume(consumed);
|
|
50 |
|
|
51 |
Some(message)
|
|
52 |
}
|
|
53 |
}
|