| 1 |
local base = _G |
| 2 |
local string = require("string") |
| 3 |
local socket = socket or require("socket") |
| 4 |
local ltn12 = ltn12 or require("ltn12") |
| 5 |
socket.tp = {} |
| 6 |
local _M = socket.tp |
| 7 |
module("socket.tp") |
| 8 |
_M.TIMEOUT = 60 |
| 9 |
local function get_reply(c) |
| 10 |
local code, current, sep |
| 11 |
local line, err = c:receive() |
| 12 |
local reply = line |
| 13 |
if err then return nil, err end |
| 14 |
code, sep = socket.skip(2, string.find(line, "^(%d%d%d)(.?)")) |
| 15 |
if not code then return nil, "invalid server reply" end |
| 16 |
if sep == "-" then |
| 17 |
repeat |
| 18 |
line, err = c:receive() |
| 19 |
if err then return nil, err end |
| 20 |
current, sep = socket.skip(2, string.find(line, "^(%d%d%d)(.?)")) |
| 21 |
reply = reply .. "\n" .. line |
| 22 |
until code == current and sep == " " |
| 23 |
end |
| 24 |
return code, reply |
| 25 |
end |
| 26 |
local metat = { __index = {} } |
| 27 |
function metat.__index:getpeername() |
| 28 |
return self.c:getpeername() |
| 29 |
end |
| 30 |
function metat.__index:getsockname() |
| 31 |
return self.c:getpeername() |
| 32 |
end |
| 33 |
function metat.__index:check(ok) |
| 34 |
local code, reply = get_reply(self.c) |
| 35 |
if not code then return nil, reply end |
| 36 |
if base.type(ok) ~= "function" then |
| 37 |
if base.type(ok) == "table" then |
| 38 |
for i, v in base.ipairs(ok) do |
| 39 |
if string.find(code, v) then |
| 40 |
return base.tonumber(code), reply |
| 41 |
end |
| 42 |
end |
| 43 |
return nil, reply |
| 44 |
else |
| 45 |
if string.find(code, ok) then return base.tonumber(code), reply |
| 46 |
else return nil, reply end |
| 47 |
end |
| 48 |
else return ok(base.tonumber(code), reply) end |
| 49 |
end |
| 50 |
function metat.__index:command(cmd, arg) |
| 51 |
cmd = string.upper(cmd) |
| 52 |
if arg then |
| 53 |
return self.c:send(cmd .. " " .. arg.. "\r\n") |
| 54 |
else |
| 55 |
return self.c:send(cmd .. "\r\n") |
| 56 |
end |
| 57 |
end |
| 58 |
function metat.__index:sink(snk, pat) |
| 59 |
local chunk, err = self.c:receive(pat) |
| 60 |
return snk(chunk, err) |
| 61 |
end |
| 62 |
function metat.__index:send(data) |
| 63 |
return self.c:send(data) |
| 64 |
end |
| 65 |
function metat.__index:receive(pat) |
| 66 |
return self.c:receive(pat) |
| 67 |
end |
| 68 |
function metat.__index:getfd() |
| 69 |
return self.c:getfd() |
| 70 |
end |
| 71 |
function metat.__index:dirty() |
| 72 |
return self.c:dirty() |
| 73 |
end |
| 74 |
function metat.__index:getcontrol() |
| 75 |
return self.c |
| 76 |
end |
| 77 |
function metat.__index:source(source, step) |
| 78 |
local sink = socket.sink("keep-open", self.c) |
| 79 |
local ret, err = ltn12.pump.all(source, sink, step or ltn12.pump.step) |
| 80 |
return ret, err |
| 81 |
end |
| 82 |
function metat.__index:close() |
| 83 |
self.c:close() |
| 84 |
return 1 |
| 85 |
end |
| 86 |
function _M.connect(host, port, timeout, create) |
| 87 |
local c, e = (create or socket.tcp)() |
| 88 |
if not c then return nil, e end |
| 89 |
c:settimeout(timeout or _M.TIMEOUT) |
| 90 |
local r, e = c:connect(host, port) |
| 91 |
if not r then |
| 92 |
c:close() |
| 93 |
return nil, e |
| 94 |
end |
| 95 |
return base.setmetatable({c = c}, metat) |
| 96 |
end |
| 97 |
socket.tp = _M |
| 98 |
return _M |