rust/hedgewars-server/src/protocol.rs
author alfadur
Mon, 08 Apr 2019 22:16:44 +0300
changeset 14796 09d46ab83361
parent 14478 98ef2913ec73
child 14816 add191d825f4
permissions -rw-r--r--
port server parser to nom 5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
     1
use netbuf;
14796
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
     2
use nom::{Err, ErrorKind, IResult};
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
     3
use std::io::{Read, Result};
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
     4
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
     5
pub mod messages;
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
     6
mod parser;
13713
59ea2403f62d move everything test related into test cfg
alfadur
parents: 13438
diff changeset
     7
#[cfg(test)]
13416
cdf69667593b partial room implementation
alfadur
parents: 12136
diff changeset
     8
pub mod test;
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
     9
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
    10
pub struct ProtocolDecoder {
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    11
    buf: netbuf::Buf,
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
    12
    consumed: usize,
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    13
}
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    14
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
    15
impl ProtocolDecoder {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
    16
    pub fn new() -> ProtocolDecoder {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
    17
        ProtocolDecoder {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
    18
            buf: netbuf::Buf::new(),
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
    19
            consumed: 0,
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    20
        }
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
    pub fn read_from<R: Read>(&mut self, stream: &mut R) -> Result<usize> {
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    24
        self.buf.read_from(stream)
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    25
    }
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    26
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
    27
    pub fn extract_messages(&mut self) -> Vec<messages::HWProtocolMessage> {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
    28
        let parse_result = parser::extract_messages(&self.buf[..]);
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
    29
        match parse_result {
13438
da71e0d88a1c update nom
alfadur
parents: 13416
diff changeset
    30
            Ok((tail, msgs)) => {
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
    31
                self.consumed = self.buf.len() - self.consumed - tail.len();
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
    32
                msgs
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    33
            }
14796
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    34
            _ => unreachable!(),
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
    35
        }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
    36
    }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
    37
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
    38
    pub fn sweep(&mut self) {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
    39
        self.buf.consume(self.consumed);
e25a82ce2374 - Render messages to string
unc0rr
parents: 12133
diff changeset
    40
        self.consumed = 0;
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    41
    }
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    42
}