grow network buffer periodically
authoralfadur
Mon, 25 Mar 2024 00:23:41 +0300
changeset 16027 14fe5bfe9862
parent 16026 7c8697fa019f
child 16028 d73e6cb37f83
grow network buffer periodically
rust/hedgewars-server/src/protocol.rs
--- a/rust/hedgewars-server/src/protocol.rs	Mon Mar 25 00:04:13 2024 +0300
+++ b/rust/hedgewars-server/src/protocol.rs	Mon Mar 25 00:23:41 2024 +0300
@@ -87,6 +87,7 @@
                 Err(nom::Err::Incomplete(_)) => {}
                 Err(nom::Err::Failure(e) | nom::Err::Error(e)) => {
                     debug!("Invalid message: {:?}", e);
+                    trace!("Buffer content: {:?}", String::from_utf8_lossy(&self.buffer[..]));
                     self.recover();
                 }
             }
@@ -101,7 +102,11 @@
         use ProtocolError::*;
 
         loop {
-            if !self.buffer.has_remaining() {
+            if self.buffer.capacity() < 1024 {
+                self.buffer.reserve(1024 - self.buffer.capacity());
+            }
+
+            if !self.buffer.has_remaining() || self.is_recovering {
                 //todo!("ensure the buffer doesn't grow indefinitely")
                 match timeout(self.read_timeout, stream.read_buf(&mut self.buffer)).await {
                     Err(_) => return Err(Timeout),