about summary refs log tree commit diff stats
path: root/src/io
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-02-09 18:33:32 +0100
committerbptato <nincsnevem662@gmail.com>2024-02-09 18:33:32 +0100
commit7cb7ea1db2b4566fde492c1e0cc4c42f245dea63 (patch)
treec3561b3b2cebb704c87c32514c0abb2a734129cb /src/io
parentf46b0b363825d4ac5f95bc824c1312370d958cdd (diff)
downloadchawan-7cb7ea1db2b4566fde492c1e0cc4c42f245dea63.tar.gz
loader: use recvData instead of readData
recvData is a new method for PosixStream that does less weird magic than
readData.

Also, allow duplicates in unregWrite/unregRead; it's simpler to live
with them than to prevent them.
Diffstat (limited to 'src/io')
-rw-r--r--src/io/posixstream.nim10
-rw-r--r--src/io/socketstream.nim10
2 files changed, 20 insertions, 0 deletions
diff --git a/src/io/posixstream.nim b/src/io/posixstream.nim
index 04fe0e5c..66b2d0d9 100644
--- a/src/io/posixstream.nim
+++ b/src/io/posixstream.nim
@@ -62,6 +62,16 @@ proc psReadData(s: Stream, buffer: pointer, len: int): int =
   if result == -1:
     raisePosixIOError()
 
+method recvData*(s: PosixStream, buffer: pointer, len: int): int {.base.} =
+  let n = read(s.fd, buffer, len)
+  if n < 0:
+    raisePosixIOError()
+  if n == 0:
+    if unlikely(s.isend):
+      raise newException(EOFError, "eof")
+    s.isend = true
+  return n
+
 method sendData*(s: PosixStream, buffer: pointer, len: int): int {.base.} =
   #TODO use sendData instead
   let n = write(s.fd, buffer, len)
diff --git a/src/io/socketstream.nim b/src/io/socketstream.nim
index fb378083..38c43a84 100644
--- a/src/io/socketstream.nim
+++ b/src/io/socketstream.nim
@@ -49,6 +49,16 @@ proc sockWriteData(s: Stream, buffer: pointer, len: int) =
       raisePosixIOError()
     i += n
 
+method recvData*(s: SocketStream, buffer: pointer, len: int): int =
+  let n = s.source.recv(buffer, len)
+  if n < 0:
+    raisePosixIOError()
+  if n == 0:
+    if unlikely(s.isend):
+      raise newException(EOFError, "eof")
+    s.isend = true
+  return n
+
 method sendData*(s: SocketStream, buffer: pointer, len: int): int =
   let n = s.source.send(buffer, len)
   if n < 0: