rust/lib-hedgewars-engine/src/ipc/channel.rs
author Wuzzy <Wuzzy2@mail.ru>
Mon, 07 Oct 2019 14:18:31 +0200
changeset 15447 6031c0cfec89
parent 15270 07e909ba4203
permissions -rw-r--r--
Make sure AI doesn't skip after collecting ammo. Fixes bug #796
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
15270
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14299
diff changeset
     5
pub struct Channel {
14260
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
15270
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14299
diff changeset
    10
impl Channel {
14260
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
    }
14277
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14266
diff changeset
    21
14299
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14277
diff changeset
    22
    pub fn iter(&mut self) -> IPCMessagesIterator {
14277
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14266
diff changeset
    23
        IPCMessagesIterator::new(self)
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14266
diff changeset
    24
    }
14265
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    25
}
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    26
15270
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14299
diff changeset
    27
impl Write for Channel {
14265
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    28
    fn write(&mut self, buf: &[u8]) -> Result<usize> {
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    29
        self.in_buffer.write(buf)
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    30
    }
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    31
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    32
    fn flush(&mut self) -> Result<()> {
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    33
        self.in_buffer.flush()
14260
a239e4243cf9 Start outlining ipc subsystem
unC0Rr
parents:
diff changeset
    34
    }
a239e4243cf9 Start outlining ipc subsystem
unC0Rr
parents:
diff changeset
    35
}
14265
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    36
15270
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14299
diff changeset
    37
impl Read for Channel {
14265
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    38
    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
14266
1b8c889027a3 Use question mark approach also in IPC::read
unC0Rr
parents: 14265
diff changeset
    39
        let read_bytes = self.out_buffer.as_ref().read(buf)?;
14265
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    40
14266
1b8c889027a3 Use question mark approach also in IPC::read
unC0Rr
parents: 14265
diff changeset
    41
        self.out_buffer.consume(read_bytes);
14265
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    42
14266
1b8c889027a3 Use question mark approach also in IPC::read
unC0Rr
parents: 14265
diff changeset
    43
        Ok(read_bytes)
14265
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    44
    }
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    45
}
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    46
14277
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14266
diff changeset
    47
pub struct IPCMessagesIterator<'a> {
15270
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14299
diff changeset
    48
    ipc: &'a mut Channel,
14277
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14266
diff changeset
    49
}
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14266
diff changeset
    50
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14266
diff changeset
    51
impl<'a> IPCMessagesIterator<'a> {
15270
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14299
diff changeset
    52
    pub fn new(ipc: &'a mut Channel) -> Self {
14299
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14277
diff changeset
    53
        Self { ipc }
14277
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14266
diff changeset
    54
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14266
diff changeset
    55
}
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14266
diff changeset
    56
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14266
diff changeset
    57
impl<'a> Iterator for IPCMessagesIterator<'a> {
14265
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    58
    type Item = EngineMessage;
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    59
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    60
    fn next(&mut self) -> Option<Self::Item> {
14277
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14266
diff changeset
    61
        let (consumed, message) = extract_message(&self.ipc.in_buffer[..])?;
14265
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    62
14277
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14266
diff changeset
    63
        self.ipc.in_buffer.consume(consumed);
14265
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    64
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    65
        Some(message)
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    66
    }
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14260
diff changeset
    67
}