util-soc-imp-tp.lua /size: 3116 b    last modification: 2020-07-01 14:35
1
-- original file : tp.lua
2
-- for more into : see util-soc.lua
3 4
local
setmetatable
,
next
,
type
,
tonumber
=
setmetatable
,
next
,
type
,
tonumber
5
local
find
,
upper
=
string
.
find
,
string
.
upper
6 7
local
socket
=
socket
or
require
(
"
socket
"
)
8
local
ltn12
=
ltn12
or
require
(
"
ltn12
"
)
9 10
local
skipsocket
=
socket
.
skip
11
local
sinksocket
=
socket
.
sink
12
local
tcpsocket
=
socket
.
tcp
13 14
local
ltn12pump
=
ltn12
.
pump
15
local
pumpall
=
ltn12pump
.
all
16
local
pumpstep
=
ltn12pump
.
step
17 18
local
tp
=
{
19
TIMEOUT
=
60
,
20
}
21 22
socket
.
tp
=
tp
23 24
local
function
get_reply
(
c
)
25
local
line
,
err
=
c
:
receive
(
)
26
local
reply
=
line
27
if
err
then
return
28
nil
,
err
29
end
30
local
code
,
sep
=
skipsocket
(
2
,
find
(
line
,
"
^(%d%d%d)(.?)
"
)
)
31
if
not
code
then
32
return
nil
,
"
invalid server reply
"
33
end
34
if
sep
=
=
"
-
"
then
35
local
current
36
repeat
37
line
,
err
=
c
:
receive
(
)
38
if
err
then
39
return
nil
,
err
40
end
41
current
,
sep
=
skipsocket
(
2
,
find
(
line
,
"
^(%d%d%d)(.?)
"
)
)
42
reply
=
reply
.
.
"
\n
"
.
.
line
43
until
code
=
=
current
and
sep
=
=
"
"
44
end
45
return
code
,
reply
46
end
47 48
local
methods
=
{
}
49
local
mt
=
{
__index
=
methods
}
50 51
function
methods
.
getpeername
(
self
)
52
return
self
.
c
:
getpeername
(
)
53
end
54 55
function
methods
.
getsockname
(
self
)
56
return
self
.
c
:
getpeername
(
)
57
end
58 59
function
methods
.
check
(
self
,
ok
)
60
local
code
,
reply
=
get_reply
(
self
.
c
)
61
if
not
code
then
62
return
nil
,
reply
63
end
64
local
c
=
tonumber
(
code
)
65
local
t
=
type
(
ok
)
66
if
t
=
=
"
function
"
then
67
return
ok
(
c
,
reply
)
68
elseif
t
=
=
"
table
"
then
69
for
i
=
1
,
#
ok
do
70
if
find
(
code
,
ok
[
i
]
)
then
71
return
c
,
reply
72
end
73
end
74
return
nil
,
reply
75
elseif
find
(
code
,
ok
)
then
76
return
c
,
reply
77
else
78
return
nil
,
reply
79
end
80
end
81 82
function
methods
.
command
(
self
,
cmd
,
arg
)
83
cmd
=
upper
(
cmd
)
84
if
arg
then
85
cmd
=
cmd
.
.
"
"
.
.
arg
.
.
"
\r\n
"
86
else
87
cmd
=
cmd
.
.
"
\r\n
"
88
end
89
return
self
.
c
:
send
(
cmd
)
90
end
91 92
function
methods
.
sink
(
self
,
snk
,
pat
)
93
local
chunk
,
err
=
self
.
c
:
receive
(
pat
)
94
return
snk
(
chunk
,
err
)
95
end
96 97
function
methods
.
send
(
self
,
data
)
98
return
self
.
c
:
send
(
data
)
99
end
100 101
function
methods
.
receive
(
self
,
pat
)
102
return
self
.
c
:
receive
(
pat
)
103
end
104 105
function
methods
.
getfd
(
self
)
106
return
self
.
c
:
getfd
(
)
107
end
108 109
function
methods
.
dirty
(
self
)
110
return
self
.
c
:
dirty
(
)
111
end
112 113
function
methods
.
getcontrol
(
self
)
114
return
self
.
c
115
end
116 117
function
methods
.
source
(
self
,
source
,
step
)
118
local
sink
=
sinksocket
(
"
keep-open
"
,
self
.
c
)
119
local
ret
,
err
=
pumpall
(
source
,
sink
,
step
or
pumpstep
)
120
return
ret
,
err
121
end
122 123
function
methods
.
close
(
self
)
124
self
.
c
:
close
(
)
125
return
1
126
end
127 128
function
tp
.
connect
(
host
,
port
,
timeout
,
create
)
129
local
c
,
e
=
(
create
or
tcpsocket
)
(
)
130
if
not
c
then
131
return
nil
,
e
132
end
133
c
:
settimeout
(
timeout
or
tp
.
TIMEOUT
)
134
local
r
,
e
=
c
:
connect
(
host
,
port
)
135
if
not
r
then
136
c
:
close
(
)
137
return
nil
,
e
138
end
139
return
setmetatable
(
{
c
=
c
}
,
mt
)
140
end
141 142
package
.
loaded
[
"
socket.tp
"
]
=
tp
143 144
return
tp
145