rust/hedgewars-server/src/protocol.rs
author Wuzzy <Wuzzy2@mail.ru>
Sat, 18 May 2019 19:33:49 +0200
changeset 15007 9d00a873bbcc
parent 14796 f5d43f007970
child 15074 c5a6e8566425
permissions -rw-r--r--
Don't abort ready phase for moving camera with keys Rationale: The keys are equivalent to moving the mouse, but boving the mouse doesn't abort the ready phase. So this is only fair: Players should not be punished for using different controls.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14795
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
     1
use crate::protocol::parser::message;
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
     2
use log::*;
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
     3
use netbuf;
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
     4
use nom::{Err, ErrorKind, IResult};
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
     5
use std::io::{Read, Result};
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
     6
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
     7
pub mod messages;
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
     8
mod parser;
13796
59ea2403f62d move everything test related into test cfg
alfadur
parents: 13438
diff changeset
     9
#[cfg(test)]
13416
cdf69667593b partial room implementation
alfadur
parents: 12136
diff changeset
    10
pub mod test;
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    11
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
    12
pub struct ProtocolDecoder {
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    13
    buf: netbuf::Buf,
14795
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    14
    is_recovering: bool,
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    15
}
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    16
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
    17
impl ProtocolDecoder {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
    18
    pub fn new() -> ProtocolDecoder {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
    19
        ProtocolDecoder {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
    20
            buf: netbuf::Buf::new(),
14795
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    21
            is_recovering: false,
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    22
        }
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    23
    }
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    24
14795
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    25
    fn recover(&mut self) -> bool {
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    26
        self.is_recovering = match parser::malformed_message(&self.buf[..]) {
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    27
            Ok((tail, ())) => {
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    28
                self.buf.consume(self.buf.len() - tail.len());
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    29
                false
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    30
            }
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    31
            _ => {
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    32
                self.buf.consume(self.buf.len());
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    33
                true
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    34
            }
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    35
        };
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    36
        !self.is_recovering
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    37
    }
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    38
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    39
    pub fn read_from<R: Read>(&mut self, stream: &mut R) -> Result<usize> {
14795
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    40
        let count = self.buf.read_from(stream)?;
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    41
        if count > 0 && self.is_recovering {
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    42
            self.recover();
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    43
        }
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    44
        Ok(count)
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    45
    }
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    46
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
    47
    pub fn extract_messages(&mut self) -> Vec<messages::HWProtocolMessage> {
14795
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    48
        let mut messages = vec![];
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    49
        if !self.is_recovering {
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    50
            loop {
14796
f5d43f007970 start by actually handling client messages
alfadur
parents: 14795
diff changeset
    51
                match parser::message(&self.buf[..]) {
14795
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    52
                    Ok((tail, message)) => {
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    53
                        messages.push(message);
14796
f5d43f007970 start by actually handling client messages
alfadur
parents: 14795
diff changeset
    54
                        self.buf.consume(self.buf.len() - tail.len());
14795
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    55
                    }
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    56
                    Err(nom::Err::Incomplete(_)) => break,
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    57
                    Err(nom::Err::Failure(e)) | Err(nom::Err::Error(e)) => {
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    58
                        debug!("Invalid message: {:?}", e);
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    59
                        if !self.recover() || self.buf.is_empty() {
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    60
                            break;
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    61
                        }
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    62
                    }
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    63
                }
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    64
            }
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
    65
        }
14795
add191d825f4 add parser error handling
alfadur
parents: 14775
diff changeset
    66
        messages
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    67
    }
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    68
}