rust/hedgewars-server/src/protocol.rs
changeset 14796 f5d43f007970
parent 14795 add191d825f4
child 15074 c5a6e8566425
--- a/rust/hedgewars-server/src/protocol.rs	Fri Apr 12 22:36:54 2019 +0300
+++ b/rust/hedgewars-server/src/protocol.rs	Fri Apr 12 23:16:44 2019 +0300
@@ -11,7 +11,6 @@
 
 pub struct ProtocolDecoder {
     buf: netbuf::Buf,
-    consumed: usize,
     is_recovering: bool,
 }
 
@@ -19,7 +18,6 @@
     pub fn new() -> ProtocolDecoder {
         ProtocolDecoder {
             buf: netbuf::Buf::new(),
-            consumed: 0,
             is_recovering: false,
         }
     }
@@ -48,19 +46,16 @@
 
     pub fn extract_messages(&mut self) -> Vec<messages::HWProtocolMessage> {
         let mut messages = vec![];
-        let mut consumed = 0;
         if !self.is_recovering {
             loop {
-                match parser::message(&self.buf[consumed..]) {
+                match parser::message(&self.buf[..]) {
                     Ok((tail, message)) => {
                         messages.push(message);
-                        consumed += self.buf.len() - tail.len();
+                        self.buf.consume(self.buf.len() - tail.len());
                     }
                     Err(nom::Err::Incomplete(_)) => break,
                     Err(nom::Err::Failure(e)) | Err(nom::Err::Error(e)) => {
                         debug!("Invalid message: {:?}", e);
-                        self.buf.consume(consumed);
-                        consumed = 0;
                         if !self.recover() || self.buf.is_empty() {
                             break;
                         }
@@ -68,7 +63,6 @@
                 }
             }
         }
-        self.buf.consume(consumed);
         messages
     }
 }