summaryrefslogtreecommitdiff
path: root/xmake/core/base/pipe.lua
blob: 4b122ed9e983777686052b0651a98e6153003879 (plain)
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, Xmake Open Source Community.
--
-- @author      ruki
-- @file        pipe.lua
--

-- define module
local pipe      = pipe or {}
local _instance = _instance or {}

-- load modules
local io        = require("base/io")
local bytes     = require("base/bytes")
local table     = require("base/table")
local string    = require("base/string")
local scheduler = require("base/scheduler")

-- the pipe events, @see tbox/platform/pipe.h
pipe.EV_READ  = 1
pipe.EV_WRITE = 2
pipe.EV_CONN  = 2

-- new a pipe file
function _instance.new(cdata, name)
    local pipefile = table.inherit(_instance)
    pipefile._NAME = name
    pipefile._PIPE = cdata
    setmetatable(pipefile, _instance)
    return pipefile
end

-- get the pipe name
function _instance:name()
    return self._NAME
end

-- get poller object type, poller.OT_PIPE
function _instance:otype()
    return 2
end

-- get cdata of pipe file
function _instance:cdata()
    return self._PIPE
end

-- write data to pipe file
function _instance:write(data, opt)

    -- ensure opened
    local ok, errors = self:_ensure_opened()
    if not ok then
        return -1, errors
    end

    -- get data address and size for bytes and string
    if type(data) == "string" then
        data = bytes(data)
    end
    local datasize = data:size()
    local dataaddr = data:caddr()

    -- init start and last
    opt = opt or {}
    local start = opt.start or 1
    local last = opt.last or datasize
    if start < 1 or start > datasize then
        return -1, string.format("%s: invalid start(%d)!", self, start)
    end
    if last < start - 1 or last > datasize + start - 1 then
        return -1, string.format("%s: invalid last(%d)!", self, last)
    end

    -- write it
    local write = 0
    local real = 0
    local errors = nil
    if opt.block then
        local size = last + 1 - start
        while start <= last do
            real, errors = io.pipe_write(self:cdata(), dataaddr + start - 1, last + 1 - start)
            if real > 0 then
                write = write + real
                start = start + real
            elseif real == 0 then
                local events, waiterrs = _instance.wait(self, pipe.EV_WRITE, opt.timeout or -1)
                if events ~= pipe.EV_WRITE then
                    errors = waiterrs
                    break
                end
            else
                break
            end
        end
        if write ~= size then
            write = -1
        end
    else
        write, errors = io.pipe_write(self:cdata(), dataaddr + start - 1, last + 1 - start)
        if write < 0 and errors then
            errors = string.format("%s: %s", self, errors)
        end
    end
    return write, errors
end

-- read data from pipe
function _instance:read(buff, size, opt)
    assert(buff)

    -- ensure opened
    local ok, errors = self:_ensure_opened()
    if not ok then
        return -1, errors
    end

    -- check buffer
    size = size or buff:size()
    if buff:size() < size then
        return -1, string.format("%s: too small buffer!", self)
    end

    -- check size
    if size == 0 then
        return 0
    elseif size == nil or size < 0 then
        return -1, string.format("%s: invalid size(%d)!", self, size)
    end

    -- init start in buffer
    opt = opt or {}
    local start = opt.start or 1
    local pos = start - 1
    if start >= buff:size() or start < 1 then
        return -1, string.format("%s: invalid start(%d)!", self, start)
    end

    -- read it
    local read = 0
    local real = 0
    local data_or_errors = nil
    if opt.block then
        local results = {}
        while read < size do
            real, data_or_errors = io.pipe_read(self:cdata(), buff:caddr() + pos + read, math.min(buff:size() - pos - read, size - read))
            if real > 0 then
                read = read + real
            elseif real == 0 then
                local events, waiterrs = _instance.wait(self, pipe.EV_READ, opt.timeout or -1)
                if events ~= pipe.EV_READ then
                    data_or_errors = waiterrs
                    break
                end
            else
                break
            end
        end
        if read == size then
            data_or_errors = buff:slice(start, read)
        else
            read = -1
        end
    else
        read, data_or_errors = io.pipe_read(self:cdata(), buff:caddr() + pos, math.min(buff:size() - pos, size))
        if read > 0 then
            data_or_errors = buff:slice(start, read)
        end
    end
    if read < 0 and data_or_errors then
        data_or_errors = string.format("%s: %s", self, data_or_errors)
    end
    return read, data_or_errors
end

-- connect pipe, only for named pipe (server-side)
function _instance:connect(opt)

    -- ensure opened
    local ok, errors = self:_ensure_opened()
    if not ok then
        return -1, errors
    end

    -- only for named pipe
    if not self:name() then
        return -1, string.format("%s: cannot connect to anonymous pipe!", self)
    end

    -- connect it
    local ok, errors = io.pipe_connect(self:cdata())
    if ok == 0 then
        opt = opt or {}
        local events, waiterrs = _instance.wait(self, pipe.EV_CONN, opt.timeout or -1)
        if events == pipe.EV_CONN then
            ok, errors = io.pipe_connect(self:cdata())
        else
            errors = waiterrs
        end
    end
    if ok < 0 and errors then
        errors = string.format("%s: %s", self, errors)
    end
    return ok, errors
end

-- wait pipe events
function _instance:wait(events, timeout)

    -- ensure opened
    local ok, errors = self:_ensure_opened()
    if not ok then
        return -1, errors
    end

    -- wait events
    local result = -1
    local errors = nil
    if scheduler:co_running() then
        result, errors = scheduler:poller_wait(self, events, timeout or -1)
    else
        result, errors = io.pipe_wait(self:cdata(), events, timeout or -1)
    end
    if result < 0 and errors then
        errors = string.format("%s: %s", self, errors)
    end
    return result, errors
end

-- close pipe file
function _instance:close()

    -- ensure opened
    local ok, errors = self:_ensure_opened()
    if not ok then
        return false, errors
    end

    -- cancel pipe events from the scheduler
    if scheduler:co_running() then
        ok, errors = scheduler:poller_cancel(self)
        if not ok then
            return false, errors
        end
    end

    -- close it
    ok = io.pipe_close(self:cdata())
    if ok then
        self._PIPE = nil
    end
    return ok
end

-- ensure the pipe is opened
function _instance:_ensure_opened()
    if not self:cdata() then
        return false, string.format("%s: has been closed!", self)
    end
    return true
end

-- tostring(pipe)
function _instance:__tostring()
    return "<pipe: " .. (self:name() or "anonymous") .. ">"
end

-- gc(pipe)
function _instance:__gc()
    if self:cdata() and io.pipe_close(self:cdata()) then
        self._PIPE = nil
    end
end

-- new a pipe
function pipe.new(cdata, name)
    return _instance.new(cdata, name)
end

-- open a named pipe file
--
-- 1. named pipe (server-side):
--
-- local pipe, errors = pipe.open("xxx", 'w')
-- if pipe then
--    if pipe:connect() then
--        pipe:write(...)
--    end
--    pipe:close()
-- end
--
-- 2. named pipe (client-side):
--
-- local pipe, errors = pipe.open("xxx")
-- if pipe then
--    pipe:read(...)
--    pipe:close()
-- end
--
-- mode: "r", "w", "rB" (block), "wB" (block), "rA" (non-block), "wA" (non-block)
--
function pipe.open(name, mode, buffsize)

    -- open named pipe
    local pipefile, errors = io.pipe_open(name, mode, buffsize or 0)
    if pipefile then
        return _instance.new(pipefile, name)
    else
        return nil, string.format("failed to open pipe: %s, %s", name, errors or "unknown reason")
    end
end

-- open anonymous pipe pair
--
-- local rpipe, wpipe, errors = pipe.openpair()
-- rpipe:read(...)
-- wpipe:write(...)
--
-- mode:
--
-- "BB": read block/write block
-- "BA": read block/write non-block
-- "AB": read non-block/write block
-- "AA": read non-block/write non-block (default)
--
function pipe.openpair(mode, buffsize)

    -- open anonymous pipe pair
    local rpipefile, wpipefile, errors = io.pipe_openpair(mode, buffsize or 0)
    if rpipefile and wpipefile then
        return _instance.new(rpipefile), _instance.new(wpipefile)
    else
        return nil, nil, string.format("failed to open anonymous pipe pair, %s", errors or "unknown reason")
    end
end

-- return module
return pipe