-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuv.lua
More file actions
69 lines (59 loc) · 1.36 KB
/
Copy pathuv.lua
File metadata and controls
69 lines (59 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
l3c = require 'go'
uv = require 'luv'
local function log (fmt, ...)
print (string.format(fmt, ...))
end
local run = l3c.run(function () uv.run "once" end)
local function sleep(n)
local t = uv.new_timer()
t:start(n, 0, l3c.async_handler(t))
coroutine.yield('async', t)
end
local function signal(sig)
local h = uv.new_signal()
uv.signal_start(h, sig, l3c.async_handler(h))
coroutine.yield('async', h)
end
local function server_create(addr, port)
local ch = l3c.chan()
local srv = uv.new_tcp()
uv.tcp_bind(srv, addr, port)
local cc = function (err)
if err then return end
local cli = uv.new_tcp()
uv.accept(srv, cli)
ch:send(cli)
end
uv.listen(srv, 128, cc)
return ch
end
local function read(cli)
local err = nil
local chunk = nil
uv.read_start(cli, function (err_, chunk_)
err = err_
chunk = chunk_
l3c.async_handler(cli)()
end)
coroutine.yield('async', cli)
return err, chunk
end
local function reader(cli, n)
while true do
local err, msg = read(cli)
if err then log ("#%d: <<error>>", n)
else log ("#%d: %s", n, msg)
end
end
end
local function main()
local sch = server_create("127.0.0.1", 8110)
local n = 0
while true do
n = n + 1
local cli = sch:recv()
l3c.go (reader, cli, n)
end
end
run(main)
--server(33)