gameServer2/src/protocol/mod.rs
author Wuzzy <Wuzzy2@mail.ru>
Thu, 26 Oct 2017 22:49:39 +0200
changeset 12779 1f8a62d1609d
parent 12136 e25a82ce2374
child 13416 cdf69667593b
permissions -rw-r--r--
ACF5: Fix final animation being stuck when the cyborg's way to the right is blocked Fixed with the new maxMoveTime parameter in AnimMove. If cyborg didn't reach its destination in 7000ms, the move anim is skipped and the sequence just continues.

use netbuf;
use std::io::Read;
use std::io::Result;
use nom::IResult;

pub mod messages;
mod parser;

pub struct ProtocolDecoder {
    buf: netbuf::Buf,
    consumed: usize,
}

impl ProtocolDecoder {
    pub fn new() -> ProtocolDecoder {
        ProtocolDecoder {
            buf: netbuf::Buf::new(),
            consumed: 0,
        }
    }

    pub fn read_from<R: Read>(&mut self, stream: &mut R) -> Result<usize> {
        self.buf.read_from(stream)
    }

    pub fn extract_messages(&mut self) -> Vec<messages::HWProtocolMessage> {
        let parse_result = parser::extract_messages(&self.buf[..]);
        match parse_result {
            IResult::Done(tail, msgs) => {
                self.consumed = self.buf.len() - self.consumed - tail.len();
                msgs
            },
            IResult::Incomplete(_) => unreachable!(),
            IResult::Error(_) => unreachable!(),
        }
    }

    pub fn sweep(&mut self) {
        self.buf.consume(self.consumed);
        self.consumed = 0;
    }
}