diff options
| author | ruki <[email protected]> | 2022-04-07 00:53:39 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-04-06 23:22:21 +0800 |
| commit | c48a7d959049ad14b6ea09baca682dcbaf88639c (patch) | |
| tree | b023420a92e8c74aeea35b3ce02ba55f98395729 /xmake/modules | |
| parent | 79e14625b3977bf7953c2150e584332a0ea8dd16 (diff) | |
improve bytes
Diffstat (limited to 'xmake/modules')
| -rw-r--r-- | xmake/modules/private/service/stream.lua | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/xmake/modules/private/service/stream.lua b/xmake/modules/private/service/stream.lua index 437abed4a..8aa31386e 100644 --- a/xmake/modules/private/service/stream.lua +++ b/xmake/modules/private/service/stream.lua @@ -28,6 +28,8 @@ local stream = stream or object() -- init stream function stream:init(sock) self._SOCK = sock + self._RCACHE = bytes(8192) + self._RCACHE_SIZE = 0 end -- is empty? @@ -47,7 +49,47 @@ function stream:send_string(str) end -- recv bytes -function stream:recv_bytes(size) +function stream:recv_bytes(buff, size) + + -- read data from cache first + local buffsize = 0 + local cache = self._RCACHE + local cache_size = self._RCACHE_SIZE + local cache_maxn = cache:size() + if size <= cache_size then + buff:copy(cache:slice(1, size)) + cache_size = cache_size - size + self._RCACHE_SIZE = cache_size + return buff:slice(1, size) + elseif cache_size > 0 then + buff:copy(cache:slice(1, cache_size)) + buffsize = cache_size + cache_size = 0 + end + assert(cache_size == 0) + + -- recv data from socket + local real = 0 + local data = nil + local wait = false + while buffsize < size do + real, data = sock:recv(cache) + if real > 0 then + --buff:append(data, 1, leftbuff) + -- TODO move left cache to head + buffsize = buffsize + real + wait = false + elseif real == 0 and not wait then + if sock:wait(socket.EV_RECV, -1) == socket.EV_RECV then + wait = true + else + break + end + else + -- TODO + break + end + end end -- recv table |
