| 1 |
#!/usr/bin/env lua |
| 2 |
|
| 3 |
local socket = require"socket" |
| 4 |
|
| 5 |
port = 8765 |
| 6 |
|
| 7 |
function pcalltest(msg, o, opt) |
| 8 |
local a = { pcall(o.getoption, o, opt) } |
| 9 |
if a[1] then |
| 10 |
print(msg, opt, unpack(a)) |
| 11 |
else |
| 12 |
print(msg, opt, 'fail: ' .. a[2]) |
| 13 |
end |
| 14 |
end |
| 15 |
|
| 16 |
function options(o) |
| 17 |
print("options for", o) |
| 18 |
|
| 19 |
for _, opt in ipairs{ |
| 20 |
"keepalive", "reuseaddr", |
| 21 |
"tcp-nodelay", "tcp-keepidle", "tcp-keepcnt", "tcp-keepintvl"} do |
| 22 |
pcalltest("getoption", o, opt) |
| 23 |
end |
| 24 |
|
| 25 |
r = o:getoption'linger' |
| 26 |
if r then |
| 27 |
print("getoption", "linger", |
| 28 |
"on", r.on, |
| 29 |
"timeout", r.timeout) |
| 30 |
else |
| 31 |
print("getoption", "linger", "no result") |
| 32 |
end |
| 33 |
end |
| 34 |
|
| 35 |
local m = socket.tcp() |
| 36 |
|
| 37 |
options(m) |
| 38 |
|
| 39 |
assert(m:bind("*", port)) |
| 40 |
assert(m:listen()) |
| 41 |
|
| 42 |
options(m) |
| 43 |
|
| 44 |
m:close() |
| 45 |
|
| 46 |
local m = socket.bind("*", port) |
| 47 |
|
| 48 |
options(m) |
| 49 |
|
| 50 |
local c = socket.connect("localhost", port) |
| 51 |
|
| 52 |
options(c) |
| 53 |
|
| 54 |
local s = m:accept() |
| 55 |
|
| 56 |
options(s) |
| 57 |
|