about summary refs log tree commit diff stats
path: root/src/io
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-03-11 19:15:24 +0100
committerbptato <nincsnevem662@gmail.com>2024-03-11 21:09:52 +0100
commitb789b0b076ef7aba3f5f6bbb4f6b7cadf596994c (patch)
tree388a265fd778cd8fc3e9fad9d734628aca0bd287 /src/io
parent8bf82ddbfb84b5ca32824a466380dae4df4ff31a (diff)
downloadchawan-b789b0b076ef7aba3f5f6bbb4f6b7cadf596994c.tar.gz
loader: rework process model
Originally we had several loader processes so that the loader did not
need asynchronity for loading several buffers at once. Since then, the
scope of what loader does has been reduced significantly, and with
that loader has become mostly asynchronous.

This patch finishes the above work as follows:

* We only fork a single loader process for the browser. It is a waste of
  resources to do otherwise, and would have made future work on a
  download manager very difficult.

* loader becomes (almost) fully async. Now the only sync part is a)
  processing commands and b) waiting for clients to consume responses.
  b) is a bit more problematic than a), but should not cause problems
  unless some other horrible bug exists in a client. (TODO: make it
  fully async.)
  This gives us a noticable improvement in CSS loading speed, since all
  resources can now be queried at once (even before the previous ones
  are connected).

* Buffers now only get processes when the *connection* is finished. So
  headers, status code, etc. are handled by the client, and the buffer
  is forked when the loader starts streaming the response body.
  As a result, mailcap entries can simply dup2 the first UNIX domain
  socket connection as their stdin. This allows us to remove the ugly
  (and slow) `canredir' hack, which required us to send file handles on
  a tour accross the entire codebase.

* The "cache" has been reworked somewhat:
	- Since canredir is gone, buffer-level requests usually start
	  in a suspended state, and are explicitly resumed only after
	  the client could decide whether it wants to cache the response.
	- Instead of a flag on Request and the URL as the cache key,
	  we now use a global counter and the special `cache:' scheme.

* misc fixes: referer_from is now actually respected by buffers (not
  just the pager), load info display should work slightly better, etc.
Diffstat (limited to 'src/io')
-rw-r--r--src/io/posixstream.nim6
-rw-r--r--src/io/serialize.nim34
-rw-r--r--src/io/serversocket.nim7
-rw-r--r--src/io/socketstream.nim8
4 files changed, 38 insertions, 17 deletions
diff --git a/src/io/posixstream.nim b/src/io/posixstream.nim
index 6d2f145d..dfb6f85b 100644
--- a/src/io/posixstream.nim
+++ b/src/io/posixstream.nim
@@ -59,6 +59,9 @@ method sendData*(s: PosixStream, buffer: pointer, len: int): int {.base.} =
 proc sendData*(s: PosixStream, buffer: openArray[char]): int {.inline.} =
   return s.sendData(unsafeAddr buffer[0], buffer.len)
 
+proc sendData*(s: PosixStream, buffer: openArray[uint8]): int {.inline.} =
+  return s.sendData(unsafeAddr buffer[0], buffer.len)
+
 method setBlocking*(s: PosixStream, blocking: bool) {.base.} =
   s.blocking = blocking
   let ofl = fcntl(s.fd, F_GETFL, 0)
@@ -67,6 +70,9 @@ method setBlocking*(s: PosixStream, blocking: bool) {.base.} =
   else:
     discard fcntl(s.fd, F_SETFL, ofl or O_NONBLOCK)
 
+method seek*(s: PosixStream; off: int) {.base.} =
+  discard lseek(s.fd, Off(off), SEEK_SET)
+
 method sclose*(s: PosixStream) {.base.} =
   discard close(s.fd)
 
diff --git a/src/io/serialize.nim b/src/io/serialize.nim
index 2e2480e6..4dcb79f0 100644
--- a/src/io/serialize.nim
+++ b/src/io/serialize.nim
@@ -38,8 +38,12 @@ proc swrite*(stream: Stream, tup: tuple)
 proc sread*(stream: Stream, tup: var tuple)
 func slen*(tup: tuple): int
 
-proc swrite*[T](stream: Stream, s: seq[T])
-proc sread*[T](stream: Stream, s: var seq[T])
+proc swrite*[I, T](stream: Stream, a: array[I, T])
+proc sread*[I, T](stream: Stream, a: var array[I, T])
+func slen*[I, T](a: array[I, T]): int
+
+proc swrite*(stream: Stream, s: seq)
+proc sread*(stream: Stream, s: var seq)
 func slen*(s: seq): int
 
 proc swrite*[U, V](stream: Stream, t: Table[U, V])
@@ -182,19 +186,29 @@ func slen*(tup: tuple): int =
   for f in tup.fields:
     result += slen(f)
 
-proc swrite*[T](stream: Stream, s: seq[T]) =
+proc swrite*[I, T](stream: Stream; a: array[I, T]) =
+  for x in a:
+    stream.swrite(x)
+
+proc sread*[I, T](stream: Stream; a: var array[I, T]) =
+  for x in a.mitems:
+    stream.sread(x)
+
+func slen*[I, T](a: array[I, T]): int =
+  for x in a:
+    result += slen(x)
+
+proc swrite*(stream: Stream, s: seq) =
   stream.swrite(s.len)
-  var i = 0
-  for m in s:
-    stream.swrite(m)
-    inc i
+  for x in s:
+    stream.swrite(x)
 
-proc sread*[T](stream: Stream, s: var seq[T]) =
+proc sread*(stream: Stream, s: var seq) =
   var len: int
   stream.sread(len)
   s.setLen(len)
-  for i in 0..<len:
-    stream.sread(s[i])
+  for x in s.mitems:
+    stream.sread(x)
 
 func slen*(s: seq): int =
   result = slen(s.len)
diff --git a/src/io/serversocket.nim b/src/io/serversocket.nim
index e8bbf549..020c5ed3 100644
--- a/src/io/serversocket.nim
+++ b/src/io/serversocket.nim
@@ -11,7 +11,7 @@ type ServerSocket* = object
 
 var SocketDirectory* = "/tmp/cha"
 const SocketPathPrefix = "cha_sock_"
-proc getSocketPath*(pid: Pid): string =
+proc getSocketPath*(pid: int): string =
   SocketDirectory / SocketPathPrefix & $pid
 
 # The way stdlib does bindUnix is utterly broken at least on FreeBSD.
@@ -19,12 +19,13 @@ proc getSocketPath*(pid: Pid): string =
 {.compile: "bind_unix.c".}
 proc bind_unix_from_c(fd: cint, path: cstring, pathlen: cint): cint {.importc.}
 
-proc initServerSocket*(buffered = true, blocking = true): ServerSocket =
+proc initServerSocket*(pid: int; buffered = true; blocking = true):
+    ServerSocket =
   createDir(SocketDirectory)
   let sock = newSocket(Domain.AF_UNIX, SockType.SOCK_STREAM, Protocol.IPPROTO_IP, buffered)
   if not blocking:
     sock.getFd().setBlocking(false)
-  let path = getSocketPath(getpid())
+  let path = getSocketPath(pid)
   discard unlink(cstring(path))
   if bind_unix_from_c(cint(sock.getFd()), cstring(path), cint(path.len)) != 0:
     raiseOSError(osLastError())
diff --git a/src/io/socketstream.nim b/src/io/socketstream.nim
index 99f7c2c9..168b7a4f 100644
--- a/src/io/socketstream.nim
+++ b/src/io/socketstream.nim
@@ -2,9 +2,6 @@ import std/nativesockets
 import std/net
 import std/os
 
-when defined(posix):
-  import std/posix
-
 import io/posixstream
 import io/serversocket
 
@@ -52,6 +49,9 @@ method setBlocking*(s: SocketStream, blocking: bool) =
   s.blocking = blocking
   s.source.getFd().setBlocking(blocking)
 
+method seek*(s: PosixStream; off: int) =
+  doAssert false
+
 method sclose*(s: SocketStream) =
   s.source.close()
 
@@ -76,7 +76,7 @@ proc connectSocketStream*(path: string, buffered = true, blocking = true):
   )
   result.addStreamIface()
 
-proc connectSocketStream*(pid: Pid, buffered = true, blocking = true):
+proc connectSocketStream*(pid: int, buffered = true, blocking = true):
     SocketStream =
   try:
     return connectSocketStream(getSocketPath(pid), buffered, blocking)