about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-03-24 12:04:46 +0100
committerbptato <nincsnevem662@gmail.com>2024-03-24 12:04:46 +0100
commit91150588036fc3d8611f659fad063138e25b2cd8 (patch)
tree49f158f9c8ce60631577cb08851d96e139d43b3e /src
parent64d80e44c3c2f0cdd51632757bab8158b314a413 (diff)
downloadchawan-91150588036fc3d8611f659fad063138e25b2cd8.tar.gz
bufreader: add initPacketReader
Diffstat (limited to 'src')
-rw-r--r--src/io/bufreader.nim10
-rw-r--r--src/io/dynstream.nim7
-rw-r--r--src/server/buffer.nim4
3 files changed, 14 insertions, 7 deletions
diff --git a/src/io/bufreader.nim b/src/io/bufreader.nim
index a8d30fb0..eca6b585 100644
--- a/src/io/bufreader.nim
+++ b/src/io/bufreader.nim
@@ -27,12 +27,14 @@ proc initReader*(stream: DynStream; len: int): BufferedReader =
       break
   return reader
 
+proc initPacketReader*(stream: DynStream): BufferedReader =
+  var len: int
+  stream.recvDataLoop(addr len, sizeof(len))
+  return stream.initReader(len)
+
 template withPacketReader*(stream: DynStream; r, body: untyped) =
   block:
-    var len: int
-    # note: this must be readData
-    doAssert stream.readData(addr len, sizeof(len)) == sizeof(len)
-    var r = stream.initReader(len)
+    var r = stream.initPacketReader()
     body
 
 proc sread*(reader: var BufferedReader; n: var SomeNumber)
diff --git a/src/io/dynstream.nim b/src/io/dynstream.nim
index ac1269f5..10db4c64 100644
--- a/src/io/dynstream.nim
+++ b/src/io/dynstream.nim
@@ -46,6 +46,13 @@ proc sendDataLoop*(s: DynStream; buffer: pointer; len: int) =
     if n == len:
       break
 
+proc recvDataLoop*(s: DynStream; buffer: pointer; len: int) =
+  var n = 0
+  while true:
+    n += s.recvData(addr cast[ptr UncheckedArray[uint8]](buffer)[n], len - n)
+    if n == len:
+      break
+
 proc dsClose(s: Stream) =
   DynStream(s).sclose()
 
diff --git a/src/server/buffer.nim b/src/server/buffer.nim
index bcc39aa2..e952d9aa 100644
--- a/src/server/buffer.nim
+++ b/src/server/buffer.nim
@@ -1791,11 +1791,9 @@ macro bufferDispatcher(funs: static ProxyMap; buffer: Buffer;
   return switch
 
 proc readCommand(buffer: Buffer) =
+  var r = buffer.pstream.initPacketReader()
   var cmd: BufferCommand
-  var len: int
   var packetid: int
-  buffer.pstream.sread(len)
-  var r = buffer.pstream.initReader(len)
   r.sread(cmd)
   r.sread(packetid)
   bufferDispatcher(ProxyFunctions, buffer, cmd, packetid, r)