author | unC0Rr |
Tue, 10 Sep 2024 13:56:51 +0200 | |
branch | transitional_engine |
changeset 16032 | 31cc1e450273 |
parent 15265 | 07e909ba4203 |
permissions | -rw-r--r-- |
14260 | 1 |
use hedgewars_engine_messages::{messages::*, parser::extract_message}; |
14255 | 2 |
use netbuf::*; |
14260 | 3 |
use std::io::*; |
14255 | 4 |
|
15265
07e909ba4203
Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents:
14294
diff
changeset
|
5 |
pub struct Channel { |
14255 | 6 |
in_buffer: Buf, |
7 |
out_buffer: Buf, |
|
8 |
} |
|
9 |
||
15265
07e909ba4203
Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents:
14294
diff
changeset
|
10 |
impl Channel { |
14255 | 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 |
} |
|
14272 | 21 |
|
14294
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14272
diff
changeset
|
22 |
pub fn iter(&mut self) -> IPCMessagesIterator { |
14272 | 23 |
IPCMessagesIterator::new(self) |
24 |
} |
|
14260 | 25 |
} |
26 |
||
15265
07e909ba4203
Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents:
14294
diff
changeset
|
27 |
impl Write for Channel { |
14260 | 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() |
|
14255 | 34 |
} |
35 |
} |
|
14260 | 36 |
|
15265
07e909ba4203
Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents:
14294
diff
changeset
|
37 |
impl Read for Channel { |
14260 | 38 |
fn read(&mut self, buf: &mut [u8]) -> Result<usize> { |
14261 | 39 |
let read_bytes = self.out_buffer.as_ref().read(buf)?; |
14260 | 40 |
|
14261 | 41 |
self.out_buffer.consume(read_bytes); |
14260 | 42 |
|
14261 | 43 |
Ok(read_bytes) |
14260 | 44 |
} |
45 |
} |
|
46 |
||
14272 | 47 |
pub struct IPCMessagesIterator<'a> { |
15265
07e909ba4203
Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents:
14294
diff
changeset
|
48 |
ipc: &'a mut Channel, |
14272 | 49 |
} |
50 |
||
51 |
impl<'a> IPCMessagesIterator<'a> { |
|
15265
07e909ba4203
Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents:
14294
diff
changeset
|
52 |
pub fn new(ipc: &'a mut Channel) -> Self { |
14294
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14272
diff
changeset
|
53 |
Self { ipc } |
14272 | 54 |
} |
55 |
} |
|
56 |
||
57 |
impl<'a> Iterator for IPCMessagesIterator<'a> { |
|
14260 | 58 |
type Item = EngineMessage; |
59 |
||
60 |
fn next(&mut self) -> Option<Self::Item> { |
|
14272 | 61 |
let (consumed, message) = extract_message(&self.ipc.in_buffer[..])?; |
14260 | 62 |
|
14272 | 63 |
self.ipc.in_buffer.consume(consumed); |
14260 | 64 |
|
65 |
Some(message) |
|
66 |
} |
|
67 |
} |