diff options
Diffstat (limited to '3rdparty/luv/examples')
-rw-r--r-- | 3rdparty/luv/examples/cqueues-main.lua | 31 | ||||
-rw-r--r-- | 3rdparty/luv/examples/cqueues-slave.lua | 55 | ||||
-rw-r--r-- | 3rdparty/luv/examples/echo-server-client.lua | 68 | ||||
-rw-r--r-- | 3rdparty/luv/examples/killing-children.lua | 24 | ||||
-rw-r--r-- | 3rdparty/luv/examples/lots-o-dns.lua | 49 | ||||
-rw-r--r-- | 3rdparty/luv/examples/repl.lua | 89 | ||||
-rw-r--r-- | 3rdparty/luv/examples/talking-to-children.lua | 47 | ||||
-rw-r--r-- | 3rdparty/luv/examples/tcp-cluster.lua | 84 | ||||
-rw-r--r-- | 3rdparty/luv/examples/timers.lua | 68 | ||||
-rw-r--r-- | 3rdparty/luv/examples/uvbook/helloworld.lua | 5 | ||||
-rw-r--r-- | 3rdparty/luv/examples/uvbook/idle-basic.lua | 14 | ||||
-rw-r--r-- | 3rdparty/luv/examples/uvbook/onchange.lua | 30 | ||||
-rw-r--r-- | 3rdparty/luv/examples/uvbook/queue-work.lua | 19 | ||||
-rw-r--r-- | 3rdparty/luv/examples/uvbook/tcp-echo-client.lua | 21 | ||||
-rw-r--r-- | 3rdparty/luv/examples/uvbook/tcp-echo-server.lua | 22 | ||||
-rw-r--r-- | 3rdparty/luv/examples/uvbook/thread-create.lua | 38 | ||||
-rw-r--r-- | 3rdparty/luv/examples/uvbook/uvcat.lua | 37 | ||||
-rw-r--r-- | 3rdparty/luv/examples/uvbook/uvtee.lua | 35 |
18 files changed, 736 insertions, 0 deletions
diff --git a/3rdparty/luv/examples/cqueues-main.lua b/3rdparty/luv/examples/cqueues-main.lua new file mode 100644 index 00000000000..ff60ec2b1c9 --- /dev/null +++ b/3rdparty/luv/examples/cqueues-main.lua @@ -0,0 +1,31 @@ +--[[ +Demonstrates using luv with a cqueues mainloop +]] + +local cqueues = require "cqueues" +local uv = require "luv" + +local cq = cqueues.new() + +cq:wrap(function() + while cqueues.poll({ + pollfd = uv.backend_fd(); + timeout = uv.backend_timeout() / 1000; + events = "r"; + }) do + uv.run("nowait") + end +end) + +cq:wrap(function() + while true do + cqueues.sleep(1) + print("HELLO FROM CQUEUES") + end +end) + +uv.new_timer():start(1000, 1000, function() + print("HELLO FROM LUV") +end) + +assert(cq:loop()) diff --git a/3rdparty/luv/examples/cqueues-slave.lua b/3rdparty/luv/examples/cqueues-slave.lua new file mode 100644 index 00000000000..599e7c6ded7 --- /dev/null +++ b/3rdparty/luv/examples/cqueues-slave.lua @@ -0,0 +1,55 @@ +--[[ +Demonstrates using cqueues with a luv mainloop + +Starts a simple sleep+print loop using each library's native form. +They should print intertwined. +]] + +local cqueues = require "cqueues" +local uv = require "luv" + +local cq = cqueues.new() + +do + local timer = uv.new_timer() + local function reset_timer() + local timeout = cq:timeout() + if timeout then + -- libuv takes milliseconds as an integer, + -- while cqueues gives timeouts as a floating point number + -- use `math.ceil` as we'd rather wake up late than early + timer:set_repeat(math.ceil(timeout * 1000)) + timer:again() + else + -- stop timer for now; it may be restarted later. + timer:stop() + end + end + local function onready() + -- Step the cqueues loop once (sleeping for max 0 seconds) + assert(cq:step(0)) + reset_timer() + end + -- Need to call `start` on libuv timer now + -- to provide callback and so that `again` works + timer:start(0, 0, onready) + -- Ask libuv to watch the cqueue pollfd + uv.new_poll(cq:pollfd()):start(cq:events(), onready) +end + +-- Adds a new function to the scheduler `cq` +-- The functions is an infinite loop that sleeps for 1 second and prints +cq:wrap(function() + while true do + cqueues.sleep(1) + print("HELLO FROM CQUEUES") + end +end) + +-- Start a luv timer that fires every 1 second +uv.new_timer():start(1000, 1000, function() + print("HELLO FROM LUV") +end) + +-- Run luv mainloop +uv.run() diff --git a/3rdparty/luv/examples/echo-server-client.lua b/3rdparty/luv/examples/echo-server-client.lua new file mode 100644 index 00000000000..ea4e6d2132d --- /dev/null +++ b/3rdparty/luv/examples/echo-server-client.lua @@ -0,0 +1,68 @@ +local p = require('lib/utils').prettyPrint +local uv = require('luv') + +local function create_server(host, port, on_connection) + + local server = uv.new_tcp() + p(1, server) + uv.tcp_bind(server, host, port) + + uv.listen(server, 128, function(err) + assert(not err, err) + local client = uv.new_tcp() + uv.accept(server, client) + on_connection(client) + end) + + return server +end + +local server = create_server("0.0.0.0", 0, function (client) + p("new client", client, uv.tcp_getsockname(client), uv.tcp_getpeername(client)) + uv.read_start(client, function (err, chunk) + p("onread", {err=err,chunk=chunk}) + + -- Crash on errors + assert(not err, err) + + if chunk then + -- Echo anything heard + uv.write(client, chunk) + else + -- When the stream ends, close the socket + uv.close(client) + end + end) +end) + +local address = uv.tcp_getsockname(server) +p("server", server, address) + +local client = uv.new_tcp() +uv.tcp_connect(client, "127.0.0.1", address.port, function (err) + assert(not err, err) + + uv.read_start(client, function (err, chunk) + p("received at client", {err=err,chunk=chunk}) + assert(not err, err) + if chunk then + uv.shutdown(client) + p("client done shutting down") + else + uv.close(client) + uv.close(server) + end + end) + + p("writing from client") + uv.write(client, "Hello") + uv.write(client, "World") + +end) + +-- Start the main event loop +uv.run() +-- Close any stray handles when done +uv.walk(uv.close) +uv.run() +uv.loop_close() diff --git a/3rdparty/luv/examples/killing-children.lua b/3rdparty/luv/examples/killing-children.lua new file mode 100644 index 00000000000..6aab693d0c1 --- /dev/null +++ b/3rdparty/luv/examples/killing-children.lua @@ -0,0 +1,24 @@ +local p = require('lib/utils').prettyPrint +local uv = require('luv') + + + +local child, pid +child, pid = uv.spawn("sleep", { + args = {"100"} +}, function (code, signal) + p("EXIT", {code=code,signal=signal}) + uv.close(child) +end) + +p{child=child, pid=pid} + +-- uv.kill(pid, "SIGTERM") +uv.process_kill(child, "SIGTERM") + +repeat + print("\ntick.") +until uv.run('once') == 0 + +print("done") + diff --git a/3rdparty/luv/examples/lots-o-dns.lua b/3rdparty/luv/examples/lots-o-dns.lua new file mode 100644 index 00000000000..59a1b0fe534 --- /dev/null +++ b/3rdparty/luv/examples/lots-o-dns.lua @@ -0,0 +1,49 @@ +local p = require('lib/utils').prettyPrint +local uv = require('luv') + +uv.getaddrinfo(nil, 80, nil, p) + +local domains = { + "facebook.com", + "google.com", + "mail.google.com", + "maps.google.com", + "plus.google.com", + "play.google.com", + "apple.com", + "hp.com", + "yahoo.com", + "mozilla.com", + "developer.mozilla.com", + "luvit.io", + "creationix.com", + "howtonode.org", + "github.com", + "gist.github.com" +} + +local i = 1 +local function next() + uv.getaddrinfo(domains[i], nil, { + v4mapped = true, + all = true, + addrconfig = true, + canonname = true, + numericserv = true, + socktype = "STREAM" + }, function (err, data) + assert(not err, err) + p(data) + i = i + 1 + if i <= #domains then + next() + end + end) +end +next(); + +repeat + print("\nTick..") +until uv.run('once') == 0 + +print("done") diff --git a/3rdparty/luv/examples/repl.lua b/3rdparty/luv/examples/repl.lua new file mode 100644 index 00000000000..92be0f17d87 --- /dev/null +++ b/3rdparty/luv/examples/repl.lua @@ -0,0 +1,89 @@ +local uv = require('luv') +local utils = require('lib/utils') + +if uv.guess_handle(0) ~= "tty" or + uv.guess_handle(1) ~= "tty" then + error "stdio must be a tty" +end +local stdin = uv.new_tty(0, true) +local stdout = require('lib/utils').stdout + +local debug = require('debug') +local c = utils.color + +local function gatherResults(success, ...) + local n = select('#', ...) + return success, { n = n, ... } +end + +local function printResults(results) + for i = 1, results.n do + results[i] = utils.dump(results[i]) + end + print(table.concat(results, '\t')) +end + +local buffer = '' + +local function evaluateLine(line) + if line == "<3\n" then + print("I " .. c("Bred") .. "♥" .. c() .. " you too!") + return '>' + end + local chunk = buffer .. line + local f, err = loadstring('return ' .. chunk, 'REPL') -- first we prefix return + + if not f then + f, err = loadstring(chunk, 'REPL') -- try again without return + end + + if f then + buffer = '' + local success, results = gatherResults(xpcall(f, debug.traceback)) + + if success then + -- successful call + if results.n > 0 then + printResults(results) + end + else + -- error + print(results[1]) + end + else + + if err:match "'<eof>'$" then + -- Lua expects some more input; stow it away for next time + buffer = chunk .. '\n' + return '>>' + else + print(err) + buffer = '' + end + end + + return '>' +end + +local function displayPrompt(prompt) + uv.write(stdout, prompt .. ' ') +end + +local function onread(err, line) + if err then error(err) end + if line then + local prompt = evaluateLine(line) + displayPrompt(prompt) + else + uv.close(stdin) + end +end + +coroutine.wrap(function() + displayPrompt '>' + uv.read_start(stdin, onread) +end)() + +uv.run() + +print("") diff --git a/3rdparty/luv/examples/talking-to-children.lua b/3rdparty/luv/examples/talking-to-children.lua new file mode 100644 index 00000000000..10a53ef8c88 --- /dev/null +++ b/3rdparty/luv/examples/talking-to-children.lua @@ -0,0 +1,47 @@ +local p = require('lib/utils').prettyPrint +local uv = require('luv') + +local stdout = uv.new_pipe(false) +local stderr = uv.new_pipe( false) +local stdin = uv.new_pipe(false) + +local handle, pid + +local function onexit(code, signal) + p("exit", {code=code,signal=signal}) +end + +local function onclose() + p("close") +end + +local function onread(err, chunk) + assert(not err, err) + if (chunk) then + p("data", {data=chunk}) + else + p("end") + end +end + +local function onshutdown() + uv.close(handle, onclose) +end + +handle, pid = uv.spawn("cat", { + stdio = {stdin, stdout, stderr} +}, onexit) + +p{ + handle=handle, + pid=pid +} + +uv.read_start(stdout, onread) +uv.read_start(stderr, onread) +uv.write(stdin, "Hello World") +uv.shutdown(stdin, onshutdown) + +uv.run() +uv.walk(uv.close) +uv.run() diff --git a/3rdparty/luv/examples/tcp-cluster.lua b/3rdparty/luv/examples/tcp-cluster.lua new file mode 100644 index 00000000000..e69ceffc62d --- /dev/null +++ b/3rdparty/luv/examples/tcp-cluster.lua @@ -0,0 +1,84 @@ + +-- This function will be run in a child process +local child_code = string.dump(function () + local p = require('lib/utils').prettyPrint + local uv = require('luv') + + -- The parent is going to pass us the server handle over a pipe + -- This will be our local file descriptor at PIPE_FD + local pipe = uv.new_pipe(true) + local pipe_fd = tonumber(os.getenv("PIPE_FD")) + assert(uv.pipe_open(pipe, pipe_fd)) + + -- Configure the server handle + local server = uv.new_tcp() + local function onconnection() + local client = uv.new_tcp() + uv.accept(server, client) + p("New TCP", client, "on", server) + p{client=client} + uv.write(client, "BYE!\n"); + uv.shutdown(client, function () + uv.close(client) + uv.close(server) + end) + end + + -- Read the server handle from the parent + local function onread(err, data) + p("onread", {err=err,data=data}) + assert(not err, err) + if uv.pipe_pending_count(pipe) > 0 then + local pending_type = uv.pipe_pending_type(pipe) + p("pending_type", pending_type) + assert(pending_type == "tcp") + assert(uv.accept(pipe, server)) + assert(uv.listen(server, 128, onconnection)) + p("Received server handle from parent process", server) + elseif data then + p("ondata", data) + else + p("onend", data) + end + end + uv.read_start(pipe, onread) + + -- Start the event loop! + uv.run() +end) + +local p = require('lib/utils').prettyPrint +local uv = require('luv') + +local exepath = assert(uv.exepath()) +local cpu_count = # assert(uv.cpu_info()) + +local server = uv.new_tcp() +assert(uv.tcp_bind(server, "::1", 1337)) +print("Master process bound to TCP port 1337 on ::1") + + +local function onexit(status, signal) + p("Child exited", {status=status,signal=signal}) +end + +local function spawnChild() + local pipe = uv.new_pipe(true) + local input = uv.new_pipe(false) + local _, pid = assert(uv.spawn(exepath, { + stdio = {input,1,2,pipe}, + env= {"PIPE_FD=3"} + }, onexit)) + uv.write(input, child_code) + uv.shutdown(input) + p("Spawned child", pid, "and sending handle", server) + assert(uv.write2(pipe, "123", server)) + assert(uv.shutdown(pipe)) +end + +-- Spawn a child process for each CPU core +for _ = 1, cpu_count do + spawnChild() +end + +uv.run() diff --git a/3rdparty/luv/examples/timers.lua b/3rdparty/luv/examples/timers.lua new file mode 100644 index 00000000000..049235e6fb1 --- /dev/null +++ b/3rdparty/luv/examples/timers.lua @@ -0,0 +1,68 @@ +local p = require('lib/utils').prettyPrint +local uv = require('luv') + +local function set_timeout(timeout, callback) + local timer = uv.new_timer() + local function ontimeout() + p("ontimeout", timer) + uv.timer_stop(timer) + uv.close(timer) + callback(timer) + end + uv.timer_start(timer, timeout, 0, ontimeout) + return timer +end + +local function clear_timeout(timer) + uv.timer_stop(timer) + uv.close(timer) +end + +local function set_interval(interval, callback) + local timer = uv.new_timer() + local function ontimeout() + p("interval", timer) + callback(timer) + end + uv.timer_start(timer, interval, interval, ontimeout) + return timer +end + +local clear_interval = clear_timeout + +local i = set_interval(300, function() + print("interval...") +end) + +set_timeout(1000, function() + clear_interval(i) +end) + + +local handle = uv.new_timer() +local delay = 1024 +local function ontimeout() + p("tick", delay) + delay = delay / 2 + if delay >= 1 then + uv.timer_set_repeat(handle, delay) + uv.timer_again(handle) + else + uv.timer_stop(handle) + uv.close(handle) + p("done") + end +end +uv.timer_start(handle, delay, 0, ontimeout) + + +repeat + print("\ntick.") +until uv.run('once') == 0 + +print("done") + +uv.walk(uv.close) +uv.run() +uv.loop_close() + diff --git a/3rdparty/luv/examples/uvbook/helloworld.lua b/3rdparty/luv/examples/uvbook/helloworld.lua new file mode 100644 index 00000000000..2c77d0c51bc --- /dev/null +++ b/3rdparty/luv/examples/uvbook/helloworld.lua @@ -0,0 +1,5 @@ +local uv = require('luv') + +print('Now quitting.') +uv.run('default') +uv.loop_close() diff --git a/3rdparty/luv/examples/uvbook/idle-basic.lua b/3rdparty/luv/examples/uvbook/idle-basic.lua new file mode 100644 index 00000000000..dc2a47b3c30 --- /dev/null +++ b/3rdparty/luv/examples/uvbook/idle-basic.lua @@ -0,0 +1,14 @@ +local uv = require('luv') + +local counter = 0 +local idle = uv.new_idle() +idle:start(function() + counter = counter + 1 + if counter >= 10e6 then + idle:stop() + end +end) + +print("Idling...") +uv.run('default') +uv.loop_close()
\ No newline at end of file diff --git a/3rdparty/luv/examples/uvbook/onchange.lua b/3rdparty/luv/examples/uvbook/onchange.lua new file mode 100644 index 00000000000..07b3f9b1d78 --- /dev/null +++ b/3rdparty/luv/examples/uvbook/onchange.lua @@ -0,0 +1,30 @@ +local uv = require('luv') + +if #arg==0 then + print(string.format("Usage: %s <command> <file1> [file2 ...]",arg[0])); + return +end + +for i=1,#arg do + local fse = uv.new_fs_event() + assert(uv.fs_event_start(fse,arg[i],{ + --"watch_entry"=true,"stat"=true, + recursive=true + },function (err,fname,status) + if(err) then + print("Error "..err) + else + print(string.format('Change detected in %s', + uv.fs_event_getpath(fse))) + for k,v in pairs(status) do + print(k,v) + end + print('file changed:'..(fname and fname or '')) + end + end)) + +end + +uv.run('default') +uv.loop_close() + diff --git a/3rdparty/luv/examples/uvbook/queue-work.lua b/3rdparty/luv/examples/uvbook/queue-work.lua new file mode 100644 index 00000000000..cf52abfb216 --- /dev/null +++ b/3rdparty/luv/examples/uvbook/queue-work.lua @@ -0,0 +1,19 @@ +local uv = require('luv') + +local ctx = uv.new_work( + function(n) --work,in threadpool + local uv = require('luv') + local t = uv.thread_self() + uv.sleep(100) + return n*n,n + end, + function(r,n) print(string.format('%d => %d',n,r)) end --after work, in loop thread +) +uv.queue_work(ctx,2) +uv.queue_work(ctx,4) +uv.queue_work(ctx,6) +uv.queue_work(ctx,8) +uv.queue_work(ctx,10) + +uv.run('default') +uv.loop_close() diff --git a/3rdparty/luv/examples/uvbook/tcp-echo-client.lua b/3rdparty/luv/examples/uvbook/tcp-echo-client.lua new file mode 100644 index 00000000000..40dd22a311f --- /dev/null +++ b/3rdparty/luv/examples/uvbook/tcp-echo-client.lua @@ -0,0 +1,21 @@ +local uv = require('luv') + + +local client = uv.new_tcp() +uv.tcp_connect(client, "127.0.0.1", 1337, function (err) + assert(not err, err) + uv.read_start(client, function (err, chunk) + assert(not err, err) + if chunk then + print(chunk) + else + uv.close(client) + end + end) + + uv.write(client, "Hello") + uv.write(client, "World") +end) +print('CTRL-C to break') +uv.run('default') +uv.loop_close() diff --git a/3rdparty/luv/examples/uvbook/tcp-echo-server.lua b/3rdparty/luv/examples/uvbook/tcp-echo-server.lua new file mode 100644 index 00000000000..269c49114cf --- /dev/null +++ b/3rdparty/luv/examples/uvbook/tcp-echo-server.lua @@ -0,0 +1,22 @@ +local uv = require('luv') + + +local server = uv.new_tcp() +server:bind("127.0.0.1", 1337) +server:listen(128, function (err) + assert(not err, err) + local client = uv.new_tcp() + server:accept(client) + client:read_start(function (err, chunk) + assert(not err, err) + if chunk then + client:write(chunk) + else + client:shutdown() + client:close() + end + end) +end) + +uv.run('default') +uv.loop_close() diff --git a/3rdparty/luv/examples/uvbook/thread-create.lua b/3rdparty/luv/examples/uvbook/thread-create.lua new file mode 100644 index 00000000000..4b42587adbf --- /dev/null +++ b/3rdparty/luv/examples/uvbook/thread-create.lua @@ -0,0 +1,38 @@ +local uv = require('luv') + +local step = 10 + +local hare_id = uv.new_thread(function(step,...) + local ffi = require'ffi' + local uv = require('luv') + local sleep + if ffi.os=='Windows' then + ffi.cdef "void Sleep(int ms);" + sleep = ffi.C.Sleep + else + ffi.cdef "unsigned int usleep(unsigned int seconds);" + sleep = ffi.C.usleep + end + while (step>0) do + step = step - 1 + uv.sleep(math.random(1000)) + print("Hare ran another step") + end + print("Hare done running!") +end, step,true,'abcd','false') + +local tortoise_id = uv.new_thread(function(step,...) + local uv = require('luv') + while (step>0) do + step = step - 1 + uv.sleep(math.random(100)) + print("Tortoise ran another step") + end + print("Tortoise done running!") +end,step,'abcd','false') + +print(hare_id==hare_id,uv.thread_equal(hare_id,hare_id)) +print(tortoise_id==hare_id,uv.thread_equal(tortoise_id,hare_id)) + +uv.thread_join(hare_id) +uv.thread_join(tortoise_id) diff --git a/3rdparty/luv/examples/uvbook/uvcat.lua b/3rdparty/luv/examples/uvbook/uvcat.lua new file mode 100644 index 00000000000..99fdd68000b --- /dev/null +++ b/3rdparty/luv/examples/uvbook/uvcat.lua @@ -0,0 +1,37 @@ +local uv = require('luv') + + +local fname = arg[1] and arg[1] or arg[0] + +uv.fs_open(fname, 'r', tonumber('644', 8), function(err,fd) + if err then + print("error opening file:"..err) + else + local stat = uv.fs_fstat(fd) + local off = 0 + local block = 10 + + local function on_read(err,chunk) + if(err) then + print("Read error: "..err); + elseif #chunk==0 then + uv.fs_close(fd) + else + off = block + off + uv.fs_write(1,chunk,-1,function(err,chunk) + if err then + print("Write error: "..err) + else + uv.fs_read(fd, block, off, on_read) + end + end) + end + end + uv.fs_read(fd, block, off, on_read) + end +end) + + + +uv.run('default') +uv.loop_close() diff --git a/3rdparty/luv/examples/uvbook/uvtee.lua b/3rdparty/luv/examples/uvbook/uvtee.lua new file mode 100644 index 00000000000..c91b066ae21 --- /dev/null +++ b/3rdparty/luv/examples/uvbook/uvtee.lua @@ -0,0 +1,35 @@ +local uv = require('luv') + +if not arg[1] then + print(string.format("please run %s filename",arg[0])) + return +end + + +local stdin = uv.new_tty(0, true) +local stdout = uv.new_tty(1, true) +--local stdin_pipe = uv.new_pipe(false) +--uv.pipe_open(stdin_pipe,0) + +local fname = arg[1] + +uv.fs_open(fname, 'w+', tonumber('644', 8), function(err,fd) + if err then + print("error opening file:"..err) + else + local fpipe = uv.new_pipe(false) + uv.pipe_open(fpipe, fd) + + uv.read_start(stdin, function(err,chunk) + if err then + print('Read error: '..err) + else + uv.write(stdout,chunk) + uv.write(fpipe,chunk) + end + end); + end +end) + +uv.run('default') +uv.loop_close() |