-
Notifications
You must be signed in to change notification settings - Fork 0
/
eio.lua
268 lines (235 loc) · 5.79 KB
/
eio.lua
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
-- Basic I/O library using effects to wait for I/O
-- Disallow setting global variables in the implementation of this module:
_ENV = setmetatable({}, {
__index = _G,
__newindex = function() error("cannot set global variable", 2) end,
})
-- Table containing all public items of this module:
local _M = {}
local nbio = require "nbio"
local wait_posix = require "wait_posix"
local handle_methods = {}
function handle_methods:close()
local nbio_handle = self.nbio_handle
local fd = nbio_handle.fd
if fd then
wait_posix.deregister_fd(nbio_handle.fd)
end
nbio_handle:close()
end
function handle_methods:shutdown()
return self.nbio_handle:shutdown()
end
_M.handle_metatbl = {
__close = handle_methods.close,
__gc = handle_methods.close,
__index = handle_methods,
}
function handle_methods:read_nonblocking(maxlen)
if maxlen == 0 then
return ""
end
-- EOF is reported as false, no data is reported as ""
return self.nbio_handle:read_unbuffered(maxlen)
end
function handle_methods:read_unbuffered(maxlen)
if maxlen == 0 then
return ""
end
for i = 1, 2 do
local result, errmsg = self.nbio_handle:read_unbuffered(maxlen)
if result == nil then
return nil, errmsg
elseif not result then
return "" -- indicates EOF
elseif result ~= "" then
return result
end
if i == 2 then
break
end
wait_posix.wait_fd_read(self.nbio_handle.fd)
end
error("no data available for reading after waiting")
end
function handle_methods:read(maxlen, terminator)
if maxlen == 0 then
return ""
end
while true do
local result, errmsg = self.nbio_handle:read(maxlen, terminator)
if result == nil then
return nil, errmsg
elseif not result then
return "" -- indicates EOF
elseif result ~= "" then
return result
end
wait_posix.wait_fd_read(self.nbio_handle.fd)
end
end
function handle_methods:write(data, ...)
local arg_count = select("#", data, ...)
if arg_count > 1 then
data = table.concat({data, ...}, nil, 1, arg_count)
end
if data == "" then
return true
end
local start = 1
local total = #data
while true do
local result, errmsg = self.nbio_handle:write(data, start)
if result then
start = start + result
else
return result, errmsg
end
if not (start <= total) then
break
end
wait_posix.wait_fd_write(self.nbio_handle.fd)
end
return true
end
function handle_methods:flush(data, ...)
local arg_count = select("#", data, ...)
if arg_count > 1 then
data = table.concat({data, ...}, nil, 1, arg_count)
end
if data ~= nil and data ~= "" then
-- write_unbuffered also flushes
local start = 1
local total = #data
while true do
local result, errmsg = self.nbio_handle:write_unbuffered(data, start)
if result then
start = start + result
else
return result, errmsg
end
if not (start <= total) then
break
end
wait_posix.wait_fd_write(self.nbio_handle.fd)
end
else
while true do
local result, errmsg = self.nbio_handle:flush()
if result == 0 then
break
elseif not result then
return result, errmsg
end
wait_posix.wait_fd_write(self.nbio_handle.fd)
end
end
return true
end
local function wrap_handle(handle)
return setmetatable({ nbio_handle = handle }, _M.handle_metatbl)
end
function _M.open(...)
local handle, err = nbio.open(...)
if not handle then
return handle, err
end
return wrap_handle(handle)
end
function _M.localconnect(...)
local handle, err = nbio.localconnect(...)
if not handle then
return handle, err
end
return wrap_handle(handle)
end
function _M.tcpconnect(...)
local handle, err = nbio.tcpconnect(...)
if not handle then
return handle, err
end
return wrap_handle(handle)
end
local listener_methods = {}
function listener_methods:close()
local nbio_listener = self.nbio_listener
local fd = nbio_listener.fd
if fd then
wait_posix.deregister_fd(fd)
end
nbio_listener:close()
end
_M.listener_metatbl = {
__close = listener_methods.close,
__gc = listener_methods.close,
__index = listener_methods,
}
function listener_methods:accept()
wait_posix.wait_fd_read(self.nbio_listener.fd)
return wrap_handle(self.nbio_listener:accept())
end
local function wrap_listener(listener)
return setmetatable({ nbio_listener = listener }, _M.listener_metatbl)
end
function _M.locallisten(...)
local listener, err = nbio.locallisten(...)
if not listener then
return listener, err
end
return wrap_listener(listener)
end
function _M.tcplisten(...)
local listener, err = nbio.tcplisten(...)
if not listener then
return listener, err
end
return wrap_listener(listener)
end
local child_methods = {}
function child_methods:close()
return self.nbio_child:close()
end
_M.child_metatbl = {
__close = child_methods.close,
__gc = child_methods.close,
__index = child_methods,
}
function child_methods:kill(sig)
return self.nbio_child:kill(sig)
end
function child_methods:wait()
local pid = self.nbio_child.pid
while true do
local result, errmsg = self.nbio_child:wait()
if result then
return result
end
if result == nil then
error(errmsg)
end
wait_posix.wait_pid(pid)
end
end
local function wrap_child(child)
return setmetatable(
{
nbio_child = child,
stdin = wrap_handle(child.stdin),
stdout = wrap_handle(child.stdout),
stderr = wrap_handle(child.stderr),
},
_M.child_metatbl
)
end
function _M.execute(...)
local child, err = nbio.execute(...)
if not child then
return child, err
end
return wrap_child(child)
end
_M.catch_signal = wait_posix.catch_signal
_M.stdin = wrap_handle(nbio.stdin)
_M.stdout = wrap_handle(nbio.stdout)
_M.stderr = wrap_handle(nbio.stderr)
return _M