diff options
author | bptato <nincsnevem662@gmail.com> | 2024-02-13 16:48:58 +0100 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2024-02-13 16:48:58 +0100 |
commit | aadbaffac2430abf01a849b1f7bbfb154c27d229 (patch) | |
tree | ea88c5a91af1544dddd26d5d41e200dc4907936d /src/loader/loaderhandle.nim | |
parent | 492a9e7cf80ae8e26b03602b5996f471c5edf4be (diff) | |
download | chawan-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.nim | 31 |
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: |