gameServer2/src/protocol/mod.rs
author unc0rr
Fri, 06 Jan 2017 01:00:21 +0300
changeset 12131 a4d22f197bd2
parent 12130 6273f89ab13d
child 12132 1525923cd7e3
permissions -rw-r--r--
Still trying to make parser work
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12129
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;
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
     4
12130
6273f89ab13d Start on messages parser
unc0rr
parents: 12129
diff changeset
     5
mod messages;
6273f89ab13d Start on messages parser
unc0rr
parents: 12129
diff changeset
     6
mod hwprotocol;
12131
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
     7
mod lexer;
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
     8
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
     9
pub struct FrameDecoder {
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    10
    buf: netbuf::Buf,
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    11
}
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    12
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    13
impl FrameDecoder {
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    14
    pub fn new() -> FrameDecoder {
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    15
        FrameDecoder {
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    16
            buf: netbuf::Buf::new()
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    17
        }
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    18
    }
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    19
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    20
    pub fn read_from<R: Read>(&mut self, stream: &mut R) -> Result<usize> {
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    21
        self.buf.read_from(stream)
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
    pub fn extract_messages(&mut self) -> &[u8] {
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    25
        &self.buf[..]
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    26
    }
07972a8c2433 - Start protocol parser implementation
unc0rr
parents:
diff changeset
    27
}
12131
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
    28
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
    29
#[test]
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
    30
fn testparser() {
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
    31
    assert_eq!(messages::HWProtocolMessage::Nick("hey".to_string()),
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
    32
               hwprotocol::parse_ProtocolMessage("NICK\nhey\n\n").unwrap());
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
    33
}