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