Skip to content

Instantly share code, notes, and snippets.

@cablehead
Created January 6, 2017 01:21
Show Gist options
  • Save cablehead/ef548a2c67123587fdbd90c7ff0163ff to your computer and use it in GitHub Desktop.
Save cablehead/ef548a2c67123587fdbd90c7ff0163ff 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)
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