about summary refs log tree commit diff stats
path: root/src/loader/loaderhandle.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-02-13 16:48:58 +0100
committerbptato <nincsnevem662@gmail.com>2024-02-13 16:48:58 +0100
commitaadbaffac2430abf01a849b1f7bbfb154c27d229 (patch)
treeea88c5a91af1544dddd26d5d41e200dc4907936d /src/loader/loaderhandle.nim
parent492a9e7cf80ae8e26b03602b5996f471c5edf4be (diff)
downloadchawan-aadbaffac2430abf01a849b1f7bbfb154c27d229.tar.gz
loader: fixes & improvements
* factor out pushBuffer to make loadFromCache async
* fix incorrect cache path
* replace rewind with loadFromCache (it does the same thing except
  actually works)
* remove rewindImpl callback, rewind in buffer instead
Diffstat (limited to 'src/loader/loaderhandle.nim')
-rw-r--r--src/loader/loaderhandle.nim31
1 files changed, 18 insertions, 13 deletions
diff --git a/src/loader/loaderhandle.nim b/src/loader/loaderhandle.nim
index fe8c6435..b0c5747e 100644
--- a/src/loader/loaderhandle.nim
+++ b/src/loader/loaderhandle.nim
@@ -17,7 +17,7 @@ const LoaderBufferPageSize = 4064 # 4096 - 32
 type
   LoaderBufferObj = object
     page: ptr UncheckedArray[uint8]
-    len: int
+    len*: int
 
   LoaderBuffer* = ref LoaderBufferObj
 
@@ -41,7 +41,7 @@ type
     canredir: bool
     outputs*: seq[OutputHandle]
     cached*: bool
-    cachepath*: string
+    cacheUrl*: string
     when defined(debug):
       url*: URL
 
@@ -70,18 +70,9 @@ proc findOutputHandle*(handle: LoaderHandle, fd: int): OutputHandle =
       return output
   return nil
 
-func `[]`*(buffer: LoaderBuffer, i: int): var uint8 {.inline.} =
-  return buffer[].page[i]
-
 func cap*(buffer: LoaderBuffer): int {.inline.} =
   return LoaderBufferPageSize
 
-func len*(buffer: LoaderBuffer): var int {.inline.} =
-  return buffer[].len
-
-proc `len=`*(buffer: LoaderBuffer, i: int) {.inline.} =
-  buffer[].len = i
-
 proc newLoaderBuffer*(): LoaderBuffer =
   return LoaderBuffer(
     page: cast[ptr UncheckedArray[uint8]](alloc(LoaderBufferPageSize)),
@@ -102,6 +93,14 @@ proc bufferCleared*(output: OutputHandle) =
   else:
     output.currentBuffer = nil
 
+proc clearBuffers*(output: OutputHandle) =
+  if output.currentBuffer != nil:
+    output.currentBuffer = nil
+    output.currentBufferIdx = 0
+    output.buffers.clear()
+  else:
+    assert output.buffers.len == 0
+
 proc tee*(outputIn: OutputHandle, ostream: PosixStream, clientId: StreamId) =
   outputIn.parent.outputs.add(OutputHandle(
     parent: outputIn.parent,
@@ -139,8 +138,14 @@ proc sendHeaders*(handle: LoaderHandle, headers: Headers) =
       output.sostream = sostream
       output.ostream = newPosixStream(fd)
 
-proc sendData*(output: OutputHandle, p: pointer, nmemb: int): int =
-  return output.ostream.sendData(p, nmemb)
+proc recvData*(ps: PosixStream, buffer: LoaderBuffer): int {.inline.} =
+  let n = ps.recvData(addr buffer.page[0], buffer.cap)
+  buffer.len = n
+  return n
+
+proc sendData*(ps: PosixStream, buffer: LoaderBuffer, si = 0): int {.inline.} =
+  assert buffer.len - si > 0
+  return ps.sendData(addr buffer.page[si], buffer.len - si)
 
 proc close*(handle: LoaderHandle) =
   for output in handle.outputs:
7f1 ^
5b59ba3 ^




9ee17f1 ^


05649a5 ^

9ee17f1 ^

07511f9 ^












1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
              
             
              
 

                                       

                 



                       

 
                
                 
                                   

 

                                       

                                         
                                                         
                    







                                                              
                                                          







                                           




                                                      


            

                                   

     












                                                                    
>
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69