rust/hedgewars-server/src/protocol.rs
author unC0Rr
Thu, 25 Jul 2019 15:18:00 +0200
changeset 15270 07e909ba4203
parent 15128 1aa3b44c0441
child 15444 a158ff8f84ef
permissions -rw-r--r--
Implement ipc queue which takes care of message ordering and timestamps
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15079
c5a6e8566425 shuffle server files
alfadur
parents: 14801
diff changeset
     1
use self::parser::message;
14800
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
     2
use log::*;
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
     3
use netbuf;
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
     4
use std::io::{Read, Result};
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
     5
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
     6
pub mod messages;
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
     7
mod parser;
13801
59ea2403f62d move everything test related into test cfg
alfadur
parents: 13443
diff changeset
     8
#[cfg(test)]
13421
cdf69667593b partial room implementation
alfadur
parents: 12141
diff changeset
     9
pub mod test;
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    10
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    11
pub struct ProtocolDecoder {
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    12
    buf: netbuf::Buf,
14800
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    13
    is_recovering: bool,
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    14
}
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    15
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    16
impl ProtocolDecoder {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    17
    pub fn new() -> ProtocolDecoder {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    18
        ProtocolDecoder {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    19
            buf: netbuf::Buf::new(),
14800
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    20
            is_recovering: false,
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    21
        }
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    22
    }
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    23
14800
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    24
    fn recover(&mut self) -> bool {
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    25
        self.is_recovering = match parser::malformed_message(&self.buf[..]) {
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    26
            Ok((tail, ())) => {
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    27
                self.buf.consume(self.buf.len() - tail.len());
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    28
                false
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    29
            }
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    30
            _ => {
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    31
                self.buf.consume(self.buf.len());
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    32
                true
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    33
            }
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    34
        };
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    35
        !self.is_recovering
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    36
    }
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    37
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    38
    pub fn read_from<R: Read>(&mut self, stream: &mut R) -> Result<usize> {
14800
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    39
        let count = self.buf.read_from(stream)?;
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    40
        if count > 0 && self.is_recovering {
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    41
            self.recover();
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    42
        }
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    43
        Ok(count)
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    44
    }
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    45
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    46
    pub fn extract_messages(&mut self) -> Vec<messages::HwProtocolMessage> {
14800
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    47
        let mut messages = vec![];
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    48
        if !self.is_recovering {
15128
1aa3b44c0441 fix parser getting stuck
alfadur
parents: 15119
diff changeset
    49
            while !self.buf.is_empty() {
14801
f5d43f007970 start by actually handling client messages
alfadur
parents: 14800
diff changeset
    50
                match parser::message(&self.buf[..]) {
14800
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    51
                    Ok((tail, message)) => {
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    52
                        messages.push(message);
14801
f5d43f007970 start by actually handling client messages
alfadur
parents: 14800
diff changeset
    53
                        self.buf.consume(self.buf.len() - tail.len());
14800
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    54
                    }
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    55
                    Err(nom::Err::Incomplete(_)) => break,
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    56
                    Err(nom::Err::Failure(e)) | Err(nom::Err::Error(e)) => {
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    57
                        debug!("Invalid message: {:?}", e);
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    58
                        if !self.recover() || self.buf.is_empty() {
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    59
                            break;
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    60
                        }
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    61
                    }
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    62
                }
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    63
            }
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    64
        }
14800
add191d825f4 add parser error handling
alfadur
parents: 14780
diff changeset
    65
        messages
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    66
    }
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    67
}