Created
January 6, 2017 01:23
-
-
Save cablehead/3fd52af289180f9bb9d6e8443c9ab9c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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