Skip to content

Instantly share code, notes, and snippets.

@brimworks
Created August 18, 2015 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brimworks/c129c707044359d30ed7 to your computer and use it in GitHub Desktop.
Save brimworks/c129c707044359d30ed7 to your computer and use it in GitHub Desktop.
local uv = require("uv")
local ffi = require("ffi")
local PIPE_NAME = "Windows" == ffi.os and "\\\\.\\pipe\\luv-bug" or "luv-bug"
local function mkresume()
local thread = coroutine.running()
return function(...)
coroutine.resume(thread, ...)
end
end
local function mkspawn(fn)
-- protected function so we can get error messages:
local function pfn(...)
local ok, err = xpcall(fn, debug.traceback, ...)
if not ok then
print("ERROR:", err)
end
end
return function(...)
coroutine.resume(coroutine.create(pfn), ...)
end
end
local function check(err, ...)
if err then
error(err)
end
return ...
end
local function main()
local spipe = assert(uv.new_pipe(false))
local function on_listen()
local cpipe = assert(uv.new_pipe(false))
assert(spipe:accept(cpipe))
assert(cpipe:read_start(mkresume()))
local chunk = check(coroutine.yield())
assert(cpipe:read_stop())
print(chunk)
cpipe:close(mkresume())
check(coroutine.yield())
end
assert(spipe:bind(PIPE_NAME))
assert(spipe:listen(10, mkspawn(on_listen)))
for idx=1,2 do
local cpipe = assert(uv.new_pipe(false))
assert(cpipe:connect(PIPE_NAME))
cpipe:write("Hello World "..idx, mkresume())
check(coroutine.yield())
cpipe:close(mkresume())
check(coroutine.yield())
end
spipe:close(mkresume())
check(coroutine.yield())
print("DONE")
end
mkspawn(main)()
uv.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment