diff options
| author | ruki <[email protected]> | 2022-04-06 22:40:37 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-04-06 22:40:37 +0800 |
| commit | 90ad0edfa8645b5b7044db79090ad90ac22a85a2 (patch) | |
| tree | 0f24a012796c0235d4ce7b26376c2483e5f1030d | |
| parent | cc8c6dee305ab37f2ba18bbd4d04cc62ad0a9f2e (diff) | |
improve pipe read
| -rw-r--r-- | tests/modules/pipe/echo_server.lua | 5 | ||||
| -rw-r--r-- | tests/modules/pipe/pipe_pair.lua | 4 | ||||
| -rw-r--r-- | tests/modules/pipe/sched_echo_server.lua | 4 | ||||
| -rw-r--r-- | tests/modules/pipe/sched_pipe_pair.lua | 4 | ||||
| -rw-r--r-- | tests/modules/process/sched_process_pipe.lua | 5 | ||||
| -rw-r--r-- | xmake/core/base/pipe.lua | 31 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/pipe.lua | 4 |
7 files changed, 25 insertions, 32 deletions
diff --git a/tests/modules/pipe/echo_server.lua b/tests/modules/pipe/echo_server.lua index 9bf1cc8e2..aed624c5b 100644 --- a/tests/modules/pipe/echo_server.lua +++ b/tests/modules/pipe/echo_server.lua @@ -1,14 +1,15 @@ import("core.base.pipe") +import("core.base.bytes") function main(name) - + local buff = bytes(8192) local pipefile = pipe.open(name or "test", 'r') if pipefile:connect() > 0 then print("%s: connected", pipefile) local count = 0 local result = nil while count < 10000 do - local read, data = pipefile:read(13, {block = true}) + local read, data = pipefile:read(buff, 13, {block = true}) if read > 0 then result = data count = count + 1 diff --git a/tests/modules/pipe/pipe_pair.lua b/tests/modules/pipe/pipe_pair.lua index de81a3133..f6b2cb5b1 100644 --- a/tests/modules/pipe/pipe_pair.lua +++ b/tests/modules/pipe/pipe_pair.lua @@ -1,9 +1,11 @@ import("core.base.pipe") +import("core.base.bytes") function main() + local buff = bytes(8192) local rpipe, wpipe = pipe.openpair(4096) wpipe:write("hello xmake!", {block = true}) - local read, data = rpipe:read(13) + local read, data = rpipe:read(buff, 13) if read > 0 and data then data:dump() end diff --git a/tests/modules/pipe/sched_echo_server.lua b/tests/modules/pipe/sched_echo_server.lua index 6a89131d0..55d355ece 100644 --- a/tests/modules/pipe/sched_echo_server.lua +++ b/tests/modules/pipe/sched_echo_server.lua @@ -1,4 +1,5 @@ import("core.base.pipe") +import("core.base.bytes") import("core.base.scheduler") function _session(id) @@ -8,8 +9,9 @@ function _session(id) print("%s/%d: connected", pipefile, id) local count = 0 local result = nil + local buff = bytes(8192) while count < 10000 do - local read, data = pipefile:read(13, {block = true}) + local read, data = pipefile:read(buff, 13, {block = true}) if read > 0 then result = data count = count + 1 diff --git a/tests/modules/pipe/sched_pipe_pair.lua b/tests/modules/pipe/sched_pipe_pair.lua index 84a055cd9..be64d0011 100644 --- a/tests/modules/pipe/sched_pipe_pair.lua +++ b/tests/modules/pipe/sched_pipe_pair.lua @@ -1,11 +1,13 @@ import("core.base.pipe") +import("core.base.bytes") import("core.base.scheduler") function _session_read(id, pipefile) print("%s/%d: read ..", pipefile, id) local result = nil + local buff = bytes(8192) for i = 1, 10000 do - local read, data = pipefile:read(12, {block = true}) + local read, data = pipefile:read(buff, 12, {block = true}) if read > 0 and data then result = data:str() end diff --git a/tests/modules/process/sched_process_pipe.lua b/tests/modules/process/sched_process_pipe.lua index 02bf70a93..65bfb6cbe 100644 --- a/tests/modules/process/sched_process_pipe.lua +++ b/tests/modules/process/sched_process_pipe.lua @@ -5,12 +5,13 @@ import("core.base.scheduler") function _session_read_pipe(id, rpipeopt) local results = {} + local buff = bytes(8192) local rpipe = rpipeopt.rpipe print("%s/%d: read ..", rpipe, id) while not rpipeopt.stop do - local real, data = rpipe:read(8192) + local real, data = rpipe:read(buff, 8192) if real > 0 then - table.insert(results, bytes(data, 1, real)) + table.insert(results, data:clone()) -- TODO elseif real == 0 then if rpipe:wait(pipe.EV_READ, -1) < 0 then break diff --git a/xmake/core/base/pipe.lua b/xmake/core/base/pipe.lua index 92ad42bc6..bb6908cde 100644 --- a/xmake/core/base/pipe.lua +++ b/xmake/core/base/pipe.lua @@ -118,7 +118,7 @@ function _instance:write(data, opt) end -- read data from pipe -function _instance:read(size, opt) +function _instance:read(buff, size, opt) -- ensure opened local ok, errors = self:_ensure_opened() @@ -126,6 +126,11 @@ function _instance:read(size, opt) return -1, errors end + -- check buffer + if not buff and buff:size() < size then + return -1, string.format("%s: too small buffer!", self) + end + -- check size if size == 0 then return 0 @@ -141,12 +146,9 @@ function _instance:read(size, opt) if opt.block then local results = {} while read < size do - local buff = self:_readbuff() - real, data_or_errors = io.pipe_read(self:cdata(), buff:caddr(), math.min(buff:size(), size - read)) + real, data_or_errors = io.pipe_read(self:cdata(), buff:caddr() + read, math.min(buff:size() - read, size - read)) if real > 0 then read = read + real - table.insert(results, bytes(buff, 1, real)) - self:_readbuff_clear() elseif real == 0 then local events, waiterrs = _instance.wait(self, pipe.EV_READ, opt.timeout or -1) if events ~= pipe.EV_READ then @@ -158,16 +160,14 @@ function _instance:read(size, opt) end end if read == size then - data_or_errors = bytes(results) + data_or_errors = bytes(buff, 1, read) else read = -1 end else - local buff = self:_readbuff() read, data_or_errors = io.pipe_read(self:cdata(), buff:caddr(), math.min(buff:size(), size)) if read > 0 then data_or_errors = bytes(buff, 1, read) - self:_readbuff_clear() end end if read < 0 and data_or_errors then @@ -255,21 +255,6 @@ function _instance:close() return ok end --- get the read buffer -function _instance:_readbuff() - local readbuff = self._READBUFF - if not readbuff then - readbuff = bytes(8192) - self._READBUFF = readbuff - end - return readbuff -end - --- clear the read buffer -function _instance:_readbuff_clear() - self._READBUFF = nil -end - -- ensure the pipe is opened function _instance:_ensure_opened() if not self:cdata() then diff --git a/xmake/core/sandbox/modules/import/core/base/pipe.lua b/xmake/core/sandbox/modules/import/core/base/pipe.lua index 462c5af0c..b3603d0ad 100644 --- a/xmake/core/sandbox/modules/import/core/base/pipe.lua +++ b/xmake/core/sandbox/modules/import/core/base/pipe.lua @@ -78,8 +78,8 @@ function sandbox_core_base_pipe_instance.write(pipefile, data, opt) end -- read data from pipe file -function sandbox_core_base_pipe_instance.read(pipefile, size, opt) - local real, data_or_errors = pipefile:_read(size, opt) +function sandbox_core_base_pipe_instance.read(pipefile, buff, size, opt) + local real, data_or_errors = pipefile:_read(buff, size, opt) if real < 0 and data_or_errors then raise(data_or_errors) end |
