gameServer2/src/protocol/mod.rs
author Wuzzy <Wuzzy2@mail.ru>
Tue, 27 Nov 2018 19:48:55 +0100
changeset 14325 f081a984da28
parent 13801 59ea2403f62d
permissions -rw-r--r--
Fix gameServer dependency documentation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
     1
use netbuf;
13443
da71e0d88a1c update nom
alfadur
parents: 13421
diff changeset
     2
use std::{
da71e0d88a1c update nom
alfadur
parents: 13421
diff changeset
     3
    io::{Read, Result}
da71e0d88a1c update nom
alfadur
parents: 13421
diff changeset
     4
};
da71e0d88a1c update nom
alfadur
parents: 13421
diff changeset
     5
use nom::{
da71e0d88a1c update nom
alfadur
parents: 13421
diff changeset
     6
    IResult, Err
da71e0d88a1c update nom
alfadur
parents: 13421
diff changeset
     7
};
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
     8
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
     9
pub mod messages;
13801
59ea2403f62d move everything test related into test cfg
alfadur
parents: 13443
diff changeset
    10
#[cfg(test)]
13421
cdf69667593b partial room implementation
alfadur
parents: 12141
diff changeset
    11
pub mod test;
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12137
diff changeset
    12
mod parser;
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    13
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    14
pub struct ProtocolDecoder {
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    15
    buf: netbuf::Buf,
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    16
    consumed: usize,
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    17
}
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    18
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    19
impl ProtocolDecoder {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    20
    pub fn new() -> ProtocolDecoder {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    21
        ProtocolDecoder {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    22
            buf: netbuf::Buf::new(),
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    23
            consumed: 0,
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    24
        }
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    25
    }
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    26
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    27
    pub fn read_from<R: Read>(&mut self, stream: &mut R) -> Result<usize> {
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    28
        self.buf.read_from(stream)
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    29
    }
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    30
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    31
    pub fn extract_messages(&mut self) -> Vec<messages::HWProtocolMessage> {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    32
        let parse_result = parser::extract_messages(&self.buf[..]);
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    33
        match parse_result {
13443
da71e0d88a1c update nom
alfadur
parents: 13421
diff changeset
    34
            Ok((tail, msgs)) => {
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    35
                self.consumed = self.buf.len() - self.consumed - tail.len();
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    36
                msgs
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    37
            },
13443
da71e0d88a1c update nom
alfadur
parents: 13421
diff changeset
    38
            Err(Err::Incomplete(_)) => unreachable!(),
da71e0d88a1c update nom
alfadur
parents: 13421
diff changeset
    39
            Err(Err::Error(_)) | Err(Err::Failure(_)) => unreachable!(),
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    40
        }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    41
    }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    42
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    43
    pub fn sweep(&mut self) {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    44
        self.buf.consume(self.consumed);
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    45
        self.consumed = 0;
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    46
    }
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    47
}