diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-18 21:41:44 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-18 21:41:44 +0000 |
commit | 7ed89c97f760600df238f9853ee453570935870f (patch) | |
tree | a17573a43815071e35f85557519fefca739ef7ef /src/tp.lua | |
parent | ac4aac0909da26befaaeb6b415f66cf35b6980e0 (diff) | |
download | luasocket-7ed89c97f760600df238f9853ee453570935870f.tar.gz luasocket-7ed89c97f760600df238f9853ee453570935870f.tar.bz2 luasocket-7ed89c97f760600df238f9853ee453570935870f.zip |
2.0 alpha RELEASED!
Diffstat (limited to 'src/tp.lua')
-rw-r--r-- | src/tp.lua | 49 |
1 files changed, 27 insertions, 22 deletions
@@ -20,16 +20,16 @@ TIMEOUT = 60 | |||
20 | -- Implementation | 20 | -- Implementation |
21 | ----------------------------------------------------------------------------- | 21 | ----------------------------------------------------------------------------- |
22 | -- gets server reply (works for SMTP and FTP) | 22 | -- gets server reply (works for SMTP and FTP) |
23 | local function get_reply(control) | 23 | local function get_reply(c) |
24 | local code, current, sep | 24 | local code, current, sep |
25 | local line, err = control:receive() | 25 | local line, err = c:receive() |
26 | local reply = line | 26 | local reply = line |
27 | if err then return nil, err end | 27 | if err then return nil, err end |
28 | code, sep = socket.skip(2, string.find(line, "^(%d%d%d)(.?)")) | 28 | code, sep = socket.skip(2, string.find(line, "^(%d%d%d)(.?)")) |
29 | if not code then return nil, "invalid server reply" end | 29 | if not code then return nil, "invalid server reply" end |
30 | if sep == "-" then -- reply is multiline | 30 | if sep == "-" then -- reply is multiline |
31 | repeat | 31 | repeat |
32 | line, err = control:receive() | 32 | line, err = c:receive() |
33 | if err then return nil, err end | 33 | if err then return nil, err end |
34 | current, sep = socket.skip(2, string.find(line, "^(%d%d%d)(.?)")) | 34 | current, sep = socket.skip(2, string.find(line, "^(%d%d%d)(.?)")) |
35 | reply = reply .. "\n" .. line | 35 | reply = reply .. "\n" .. line |
@@ -43,7 +43,7 @@ end | |||
43 | local metat = { __index = {} } | 43 | local metat = { __index = {} } |
44 | 44 | ||
45 | function metat.__index:check(ok) | 45 | function metat.__index:check(ok) |
46 | local code, reply = get_reply(self.control) | 46 | local code, reply = get_reply(self.c) |
47 | if not code then return nil, reply end | 47 | if not code then return nil, reply end |
48 | if type(ok) ~= "function" then | 48 | if type(ok) ~= "function" then |
49 | if type(ok) == "table" then | 49 | if type(ok) == "table" then |
@@ -59,50 +59,55 @@ function metat.__index:check(ok) | |||
59 | end | 59 | end |
60 | 60 | ||
61 | function metat.__index:command(cmd, arg) | 61 | function metat.__index:command(cmd, arg) |
62 | if arg then return self.control:send(cmd .. " " .. arg.. "\r\n") | 62 | if arg then return self.c:send(cmd .. " " .. arg.. "\r\n") |
63 | else return self.control:send(cmd .. "\r\n") end | 63 | else return self.c:send(cmd .. "\r\n") end |
64 | end | 64 | end |
65 | 65 | ||
66 | function metat.__index:sink(snk, pat) | 66 | function metat.__index:sink(snk, pat) |
67 | local chunk, err = control:receive(pat) | 67 | local chunk, err = c:receive(pat) |
68 | return snk(chunk, err) | 68 | return snk(chunk, err) |
69 | end | 69 | end |
70 | 70 | ||
71 | function metat.__index:send(data) | 71 | function metat.__index:send(data) |
72 | return self.control:send(data) | 72 | return self.c:send(data) |
73 | end | 73 | end |
74 | 74 | ||
75 | function metat.__index:receive(pat) | 75 | function metat.__index:receive(pat) |
76 | return self.control:receive(pat) | 76 | return self.c:receive(pat) |
77 | end | 77 | end |
78 | 78 | ||
79 | function metat.__index:getfd() | 79 | function metat.__index:getfd() |
80 | return self.control:getfd() | 80 | return self.c:getfd() |
81 | end | 81 | end |
82 | 82 | ||
83 | function metat.__index:dirty() | 83 | function metat.__index:dirty() |
84 | return self.control:dirty() | 84 | return self.c:dirty() |
85 | end | 85 | end |
86 | 86 | ||
87 | function metat.__index:getcontrol() | 87 | function metat.__index:getcontrol() |
88 | return self.control | 88 | return self.c |
89 | end | 89 | end |
90 | 90 | ||
91 | function metat.__index:source(source, step) | 91 | function metat.__index:source(source, step) |
92 | local sink = socket.sink("keep-open", self.control) | 92 | local sink = socket.sink("keep-open", self.c) |
93 | return ltn12.pump.all(source, sink, step or ltn12.pump.step) | 93 | return ltn12.pump.all(source, sink, step or ltn12.pump.step) |
94 | end | 94 | end |
95 | 95 | ||
96 | -- closes the underlying control | 96 | -- closes the underlying c |
97 | function metat.__index:close() | 97 | function metat.__index:close() |
98 | self.control:close() | 98 | self.c:close() |
99 | return 1 | 99 | return 1 |
100 | end | 100 | end |
101 | 101 | ||
102 | -- connect with server and return control object | 102 | -- connect with server and return c object |
103 | connect = socket.protect(function(host, port, timeout) | 103 | function connect(host, port, timeout) |
104 | local control = socket.try(socket.tcp()) | 104 | local c, e = socket.tcp() |
105 | socket.try(control:settimeout(timeout or TIMEOUT)) | 105 | if not c then return nil, e end |
106 | socket.try(control:connect(host, port)) | 106 | c:settimeout(timeout or TIMEOUT) |
107 | return setmetatable({control = control}, metat) | 107 | local r, e = c:connect(host, port) |
108 | end) | 108 | if not r then |
109 | c:close() | ||
110 | return nil, e | ||
111 | end | ||
112 | return setmetatable({c = c}, metat) | ||
113 | end | ||