aboutsummaryrefslogtreecommitdiff
path: root/src/tp.lua
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-18 21:41:44 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-18 21:41:44 +0000
commit7ed89c97f760600df238f9853ee453570935870f (patch)
treea17573a43815071e35f85557519fefca739ef7ef /src/tp.lua
parentac4aac0909da26befaaeb6b415f66cf35b6980e0 (diff)
downloadluasocket-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.lua49
1 files changed, 27 insertions, 22 deletions
diff --git a/src/tp.lua b/src/tp.lua
index 7b581b9..153541a 100644
--- a/src/tp.lua
+++ b/src/tp.lua
@@ -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)
23local function get_reply(control) 23local 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
43local metat = { __index = {} } 43local metat = { __index = {} }
44 44
45function metat.__index:check(ok) 45function 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)
59end 59end
60 60
61function metat.__index:command(cmd, arg) 61function 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
64end 64end
65 65
66function metat.__index:sink(snk, pat) 66function 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)
69end 69end
70 70
71function metat.__index:send(data) 71function metat.__index:send(data)
72 return self.control:send(data) 72 return self.c:send(data)
73end 73end
74 74
75function metat.__index:receive(pat) 75function metat.__index:receive(pat)
76 return self.control:receive(pat) 76 return self.c:receive(pat)
77end 77end
78 78
79function metat.__index:getfd() 79function metat.__index:getfd()
80 return self.control:getfd() 80 return self.c:getfd()
81end 81end
82 82
83function metat.__index:dirty() 83function metat.__index:dirty()
84 return self.control:dirty() 84 return self.c:dirty()
85end 85end
86 86
87function metat.__index:getcontrol() 87function metat.__index:getcontrol()
88 return self.control 88 return self.c
89end 89end
90 90
91function metat.__index:source(source, step) 91function 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)
94end 94end
95 95
96-- closes the underlying control 96-- closes the underlying c
97function metat.__index:close() 97function metat.__index:close()
98 self.control:close() 98 self.c:close()
99 return 1 99 return 1
100end 100end
101 101
102-- connect with server and return control object 102-- connect with server and return c object
103connect = socket.protect(function(host, port, timeout) 103function 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)
108end) 108 if not r then
109 c:close()
110 return nil, e
111 end
112 return setmetatable({c = c}, metat)
113end