Skip to content

Instantly share code, notes, and snippets.

@cablehead
Created January 6, 2017 17:14
Show Gist options
  • Save cablehead/9b5d0704cc82febc18c1c6e5a10d48ad to your computer and use it in GitHub Desktop.
Save cablehead/9b5d0704cc82febc18c1c6e5a10d48ad to your computer and use it in GitHub Desktop.
local ffi = require("ffi")
local levee = require("levee")
ffi.cdef([[
typedef struct { int len; } myproto_prefix_t;
]])
local SIZEOF_PREFIX = ffi.sizeof("myproto_prefix_t")
local h = levee.Hub()
local r, w = h.io:pipe()
local function writer()
local prefix = ffi.new("myproto_prefix_t")
prefix.len = 3
w:write(ffi.cast("char *", prefix), SIZEOF_PREFIX)
w:write("foo")
end
writer()
-- reader
local stream = r:stream()
stream:readin(SIZEOF_PREFIX)
local prefix = ffi.cast("myproto_prefix_t *", stream:value())
stream:readin(prefix.len)
-- local s = ffi.string(stream:value() + SIZEOF_PREFIX, prefix.len) vs -->
local s = ffi.string(stream:value(SIZEOF_PREFIX, prefix.len))
stream:trim(SIZEOF_PREFIX + prefix.len)
print(s)
assert(#stream == 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment