gameServer2/src/protocol/mod.rs
author Wuzzy <Wuzzy2@mail.ru>
Wed, 07 Mar 2018 15:09:31 +0100
changeset 13094 c9cdbf630447
parent 12141 e25a82ce2374
child 13421 cdf69667593b
permissions -rw-r--r--
Stop SplitByChar also lowercasing the entire string. Fixes bug #581. It's weird that a function with this name would lowercase the whole string. Nemo and I have checked the history and code for any justifications of the lowercasing but we found none. I have checked in the code if anything actually depends on SplitByChar also lowercasing the string but I found nothing. It would surprise me since it's not obvious from the name IMO is bad coding practice anyway. Bug 581 is fixed by this because cLocale was (incorrectly) lowercased, which broke locale names like pt_BR to pt_br.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
     1
use netbuf;
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
     2
use std::io::Read;
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
     3
use std::io::Result;
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
     4
use nom::IResult;
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;
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12137
diff changeset
     7
mod parser;
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
     8
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
     9
pub struct ProtocolDecoder {
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    10
    buf: netbuf::Buf,
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    11
    consumed: usize,
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    12
}
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    13
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    14
impl ProtocolDecoder {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    15
    pub fn new() -> ProtocolDecoder {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    16
        ProtocolDecoder {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    17
            buf: netbuf::Buf::new(),
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    18
            consumed: 0,
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    19
        }
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
    pub fn read_from<R: Read>(&mut self, stream: &mut R) -> Result<usize> {
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    23
        self.buf.read_from(stream)
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    24
    }
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    25
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    26
    pub fn extract_messages(&mut self) -> Vec<messages::HWProtocolMessage> {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    27
        let parse_result = parser::extract_messages(&self.buf[..]);
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    28
        match parse_result {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    29
            IResult::Done(tail, msgs) => {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    30
                self.consumed = self.buf.len() - self.consumed - tail.len();
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    31
                msgs
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    32
            },
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    33
            IResult::Incomplete(_) => unreachable!(),
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    34
            IResult::Error(_) => unreachable!(),
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    35
        }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    36
    }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    37
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    38
    pub fn sweep(&mut self) {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    39
        self.buf.consume(self.consumed);
e25a82ce2374 - Render messages to string
unc0rr
parents: 12138
diff changeset
    40
        self.consumed = 0;
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    41
    }
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    42
}