about summary refs log tree commit diff stats
path: root/src/ips
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-12-13 17:03:17 +0100
committerbptato <nincsnevem662@gmail.com>2022-12-13 17:03:17 +0100
commit4b482418c22ea31729ca94583ffd2a6aa167d811 (patch)
tree9c456555f1b3cfd45d90f28d61d7369ab96e1c5d /src/ips
parent672ab553c4a2b10a703ea40e049eda52db149a93 (diff)
downloadchawan-4b482418c22ea31729ca94583ffd2a6aa167d811.tar.gz
Fix stream error handling confusion, title display
Also probably other fixes.
Diffstat (limited to 'src/ips')
-rw-r--r--src/ips/forkserver.nim7
-rw-r--r--src/ips/serialize.nim6
-rw-r--r--src/ips/socketstream.nim24
3 files changed, 24 insertions, 13 deletions
diff --git a/src/ips/forkserver.nim b/src/ips/forkserver.nim
index e8904354..29463248 100644
--- a/src/ips/forkserver.nim
+++ b/src/ips/forkserver.nim
@@ -6,6 +6,7 @@ when defined(posix):
 import buffer/buffer
 import config/config
 import io/loader
+import io/posixstream
 import io/request
 import io/urlfilter
 import io/window
@@ -92,8 +93,8 @@ proc forkBuffer(ctx: var ForkServerContext): Pid =
 
 proc runForkServer() =
   var ctx: ForkServerContext
-  ctx.istream = newFileStream(stdin)
-  ctx.ostream = newFileStream(stdout)
+  ctx.istream = newPosixStream(stdin.getFileHandle())
+  ctx.ostream = newPosixStream(stdout.getFileHandle())
   while true:
     try:
       var cmd: ForkCommand
@@ -124,7 +125,7 @@ proc runForkServer() =
         width_table = makewidthtable(config.ambiguous_double)
         SocketDirectory = config.tmpdir
       ctx.ostream.flush()
-    except IOError:
+    except EOFError:
       # EOF
       break
   ctx.istream.close()
diff --git a/src/ips/serialize.nim b/src/ips/serialize.nim
index ed35371a..05f05ac9 100644
--- a/src/ips/serialize.nim
+++ b/src/ips/serialize.nim
@@ -116,7 +116,11 @@ proc swrite*(stream: Stream, s: string) =
 proc sread*(stream: Stream, s: var string) =
   var len: int
   stream.sread(len)
-  stream.readStr(len, s)
+  if len > 0:
+    stream.readStr(len, s)
+  else:
+    s = ""
+
 func slen*(s: string): int =
   slen(s.len) + s.len
 
diff --git a/src/ips/socketstream.nim b/src/ips/socketstream.nim
index 4f98517c..6d3e5cd9 100644
--- a/src/ips/socketstream.nim
+++ b/src/ips/socketstream.nim
@@ -15,26 +15,32 @@ type SocketStream* = ref object of Stream
   isend: bool
 
 proc sockReadData(s: Stream, buffer: pointer, len: int): int =
+  assert len != 0
   let s = SocketStream(s)
   if s.blk:
     while result < len:
       let n = s.source.recv(cast[pointer](cast[int](buffer) + result), len - result)
-      result += n
-      if n == 0:
-        raise newException(EOFError, "")
       if n < 0:
-        result = n
+        if result == 0:
+          result = n
+        break
+      elif n == 0:
+        s.isend = true
         break
+      result += n
   else:
     result = s.source.recv(buffer, len)
+  if result == 0:
+    s.isend = true
+    raise newException(EOFError, "eof")
   if result < 0:
     if errno == EAGAIN:
-      raise newException(ErrorAgain, "")
+      raise newException(ErrorAgain, "eagain")
     case errno
-    of EWOULDBLOCK: raise newException(ErrorWouldBlock, "")
-    of EBADF: raise newException(ErrorBadFD, "")
-    of EFAULT: raise newException(ErrorFault, "")
-    of EINVAL: raise newException(ErrorInvalid, "")
+    of EWOULDBLOCK: raise newException(ErrorWouldBlock, "would block")
+    of EBADF: raise newException(ErrorBadFD, "bad fd")
+    of EFAULT: raise newException(ErrorFault, "fault")
+    of EINVAL: raise newException(ErrorInvalid, "invalid")
     else: raise newException(IOError, $strerror(errno))
   elif result == 0:
     s.isend = true