-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_iostream.lua
More file actions
42 lines (34 loc) · 1018 Bytes
/
test_iostream.lua
File metadata and controls
42 lines (34 loc) · 1018 Bytes
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
#!/usr/bin/env luajit
local S = require "syscall"
local ffi = require "ffi"
local t, c = S.t, S.c
local oldassert = assert
function assert(c, s)
return oldassert(c, tostring(s)) -- annoyingly, assert does not call tostring!
end
function printReceived(stream, data)
print(data)
if string.sub(data, #data, #data) == '\n' then
stream:close()
else
stream:read_until('\n', printReceived)
end
end
function start_read(stream)
stream:read_bytes(16, printReceived)
end
function connected(stream)
print('connected')
stream:write('Hi there!\n', start_read)
end
local ioloop = require('everlooping.ioloop').defaultIOLoop()
local IOStream = require('everlooping.iostream').IOStream
local s = assert(S.socket("inet", "stream, nonblock"))
s = IOStream(s)
local sa = assert(t.sockaddr_in(8000, "127.0.0.1"))
s:connect(sa, connected)
local s2 = assert(S.socket("inet", "stream, nonblock"))
s2 = IOStream(s2)
local sa2 = assert(t.sockaddr_in(8001, "127.0.0.1"))
s2:connect(sa2, connected)
ioloop:start()