Repository navigation
Expand file tree
/
Copy pathselect.lua
More file actions
285 lines (262 loc) · 5.69 KB
/
Copy pathselect.lua
File metadata and controls
285 lines (262 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
local socket = require 'bee.socket'
local listens = {}
local connects = {}
local wantconnects = {}
local event = {}
local rds = {}
local wds = {}
local read = setmetatable({}, {__index = function() return '' end})
local write = setmetatable({}, {__index = function() return '' end})
local willclose = {}
local shutdown = {}
local function open_read(fd)
for _, r in ipairs(rds) do
if r == fd then
return
end
end
rds[#rds+1] = fd
end
local function open_write(fd)
for _, w in ipairs(wds) do
if w == fd then
return
end
end
wds[#wds+1] = fd
end
local function close_read(fd)
for i, f in ipairs(rds) do
if f == fd then
rds[i] = rds[#rds]
rds[#rds] = nil
return
end
end
end
local function close_write(fd)
for i, f in ipairs(wds) do
if f == fd then
wds[i] = wds[#wds]
wds[#wds] = nil
return
end
end
end
local function close_listen(fd)
for i, f in ipairs(listens) do
if f == fd then
listens[i] = listens[#listens]
listens[#listens] = nil
return
end
end
end
local function close_connect(fd)
for i, f in ipairs(connects) do
if f == fd then
connects[i] = connects[#connects]
connects[#connects] = nil
return
end
end
end
local function close(fd)
if event[fd] then event[fd]('close', fd) end
close_listen(fd)
close_connect(fd)
close_read(fd)
close_write(fd)
fd:close()
read[fd] = nil
write[fd] = nil
event[fd] = nil
shutdown[fd] = true
willclose[fd] = true
end
local function attach(fd)
open_read(fd)
if write[fd] ~= '' then
open_write(fd)
end
end
local m = {}
function m.recv(fd, n)
if n and #read[fd] > n then
local data = read[fd]:sub(1, n)
read[fd] = read[fd]:sub(n+1)
return data
else
local data = read[fd]
read[fd] = ''
return data
end
end
function m.send(fd, data)
if shutdown[fd] then
return
end
if data == '' then
return
end
write[fd] = write[fd] .. data
open_write(fd)
end
function m.close(fd)
willclose[fd] = true
if shutdown[fd] or write[fd] == '' then
close(fd)
end
end
function m.is_closed(fd)
return shutdown[fd] and willclose[fd]
end
function m.cleanup(fd)
shutdown[fd] = nil
willclose[fd] = nil
read[fd] = nil
write[fd] = nil
event[fd] = nil
end
function m.listen(t)
local fd, err = socket(t.protocol)
if not fd then
return nil, err
end
if t.protocol == "unix" then
os.remove(t.address)
end
local ok
ok, err = fd:bind(t.address, t.port)
if not ok then
fd:close()
return nil, err
end
ok, err = fd:listen()
if not ok then
fd:close()
return nil, err
end
listens[#listens+1] = fd
event[fd] = t.event
return fd
end
function m.connect(t)
local fd, err = socket(t.protocol)
if not fd then
return nil, err
end
local ok
ok, err = fd:connect(t.address, t.port)
if ok == nil then
fd:close()
return nil, err
end
connects[#connects+1] = fd
event[fd] = t.event
return fd
end
function m.wantconnect(t)
local i = 1
while true do
if not wantconnects[i] then
wantconnects[i] = t
return i
end
i = i + 1
end
end
function m.dontwantconnect(idx)
wantconnects[idx] = nil
end
local function updateLC()
for idx, wc in pairs(wantconnects) do
local fd = m.connect(wc)
if fd then
m.dontwantconnect(idx)
event[fd]('connect start', fd)
break
end
end
if #listens == 0 and #connects == 0 then
return
end
local rd, wr = socket.select(listens, connects, 0)
if not rd then
return
end
for _, fd in ipairs(rd) do
local newfd = fd:accept()
if newfd:status() then
event[fd]('accept', newfd)
if newfd:status() then
event[newfd] = event[fd]
attach(newfd)
end
end
end
for _, fd in ipairs(wr) do
close_connect(fd)
local ok, err = fd:status()
if ok then
event[fd]('ok', fd)
attach(fd)
else
event[fd]('connect failed', fd, err)
close(fd)
end
end
end
function m.update(timeout)
updateLC()
local rd, wr = socket.select(rds, wds, timeout)
if not rd then
return
end
for _, fd in ipairs(rd) do
local data = fd:recv()
if data == nil then
close(fd)
elseif data == false then
else
read[fd] = read[fd] .. data
end
end
for _, fd in ipairs(wr) do
local n = fd:send(write[fd])
if n == nil then
close_write(fd)
shutdown[fd] = true
if willclose[fd] then
close(fd)
end
else
write[fd] = write[fd]:sub(n + 1)
if write[fd] == '' then
close_write(fd)
if willclose[fd] then
close(fd)
end
end
end
end
end
function m.closeall()
for fd in pairs(write) do
if not m.is_closed(fd) then
m.close(fd)
end
end
local function is_finish()
for fd in pairs(write) do
if not m.is_closed(fd) then
return false
end
end
return true
end
while not is_finish() do
m.update(0)
end
end
return m