diff options
author | bptato <nincsnevem662@gmail.com> | 2024-03-20 21:27:47 +0100 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2024-03-20 21:27:47 +0100 |
commit | c8e6d65b7979dc22dee3c51effd321f92ab17cd1 (patch) | |
tree | 00602b322081c8bd341fd552149f6c9d030f824b /src | |
parent | 4d82d078f95a9654d1d8f2d0c95880ed723790fb (diff) | |
download | chawan-c8e6d65b7979dc22dee3c51effd321f92ab17cd1.tar.gz |
buffer: also buffer reads for packets
Since we know the length of packets, we can also read them in in one call. Though I really wish we could do this without the StringStream.
Diffstat (limited to 'src')
-rw-r--r-- | src/io/bufwriter.nim | 9 | ||||
-rw-r--r-- | src/server/buffer.nim | 28 |
2 files changed, 21 insertions, 16 deletions
diff --git a/src/io/bufwriter.nim b/src/io/bufwriter.nim index 99c7ed94..57219100 100644 --- a/src/io/bufwriter.nim +++ b/src/io/bufwriter.nim @@ -48,10 +48,11 @@ proc deinit*(writer: var BufferedWriter) = writer.bufLen = 0 template withWriter*(stream: DynStream; w, body: untyped) = - var w {.inject.} = stream.initWriter() - body - w.flush() - w.deinit() + block: + var w {.inject.} = stream.initWriter() + body + w.flush() + w.deinit() proc swrite*(writer: var BufferedWriter; n: SomeNumber) proc swrite*[T](writer: var BufferedWriter; s: set[T]) diff --git a/src/server/buffer.nim b/src/server/buffer.nim index 43b08b18..c1019518 100644 --- a/src/server/buffer.nim +++ b/src/server/buffer.nim @@ -123,7 +123,8 @@ type outputId: int InterfaceOpaque = ref object - stream: Stream + stream: SocketStream + dummyStream: StringStream len: int BufferInterface* = ref object @@ -144,11 +145,16 @@ type proc getFromOpaque[T](opaque: pointer, res: var T) = let opaque = cast[InterfaceOpaque](opaque) if opaque.len != 0: - opaque.stream.sread(res) + let dummyStream = opaque.dummyStream + opaque.dummyStream.setPosition(0) + dummyStream.data = newString(opaque.len) + let n = opaque.stream.readData(addr dummyStream.data[0], opaque.len) + assert n == opaque.len + dummyStream.sread(res) proc newBufferInterface*(stream: SocketStream, registerFun: proc(fd: int)): BufferInterface = - let opaque = InterfaceOpaque(stream: stream) + let opaque = InterfaceOpaque(stream: stream, dummyStream: newStringStream()) result = BufferInterface( map: newPromiseMap(cast[pointer](opaque)), packetid: 1, # ids below 1 are invalid @@ -1724,19 +1730,17 @@ macro bufferDispatcher(funs: static ProxyMap, buffer: Buffer, if rval == nil: resolve.add(quote do: let len = slen(`packetid`) - block: - buffer.pstream.withWriter w: - w.swrite(len) - w.swrite(`packetid`) + buffer.pstream.withWriter w: + w.swrite(len) + w.swrite(`packetid`) ) else: resolve.add(quote do: let len = slen(`packetid`) + slen(`rval`) - block: - buffer.pstream.withWriter w: - w.swrite(len) - w.swrite(`packetid`) - w.swrite(`rval`) + buffer.pstream.withWriter w: + w.swrite(len) + w.swrite(`packetid`) + w.swrite(`rval`) ) if v.istask: let en = v.ename |