jacket/examples/jacket_ringbuffer.nim

50 lines
1.4 KiB
Nim

import std/[logging, strformat]
import jacket
var log = newConsoleLogger(when defined(release): lvlInfo else: lvlDebug)
proc main() =
addHandler(log)
let size = 32
var read, written: int
var data = [0x64.uint8, 0x65, 0x61, 0x64, 0x62, 0x65, 0x65, 0x66]
var recvBuf: array[8, uint8]
let rb = ringbufferCreate(size.csize_t)
debug fmt"Created ringbuffer of {size} bytes size."
doAssert ringbufferReadSpace(rb) == 0
written = cast[int](ringbufferWrite(rb, cast[cstring](data.addr), 4))
doAssert written == 4
debug fmt"Written {written} bytes to ringbuffer."
doAssert ringbufferReadSpace(rb) == 4
written = cast[int](ringbufferWrite(rb, cast[cstring](data[4].addr), 4))
doAssert written == 4
debug fmt"Written {written} bytes to ringbuffer."
doAssert ringbufferReadSpace(rb) == 8
read = cast[int](ringbufferRead(rb, cast[cstring](recvBuf.addr), 4))
doAssert read == 4
debug fmt"Read {read} bytes from ringbuffer into receive buffer: {recvBuf}"
doAssert ringbufferReadSpace(rb) == 4
read = cast[int](ringbufferRead(rb, cast[cstring](recvBuf[4].addr), 4))
doAssert read == 4
debug fmt"Read {read} bytes from ringbuffer into receive buffer: {recvBuf}"
doAssert ringbufferReadSpace(rb) == 0
debug "Freeing ringbuffer memory."
ringbufferFree(rb)
when(isMainModule):
main()