diff options
| author | ruki <[email protected]> | 2022-04-07 23:02:06 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-04-07 23:02:06 +0800 |
| commit | e930a2db6840d9a6f501d4231889e045ba09d7d3 (patch) | |
| tree | f1c6e1a79d13cdb090954d238aa59e6b241dd4d2 /xmake/modules/private/service/stream.lua | |
| parent | 3c562917883423cbf50c94aecd2dc790b6bbf988 (diff) | |
fix bytes
Diffstat (limited to 'xmake/modules/private/service/stream.lua')
| -rw-r--r-- | xmake/modules/private/service/stream.lua | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/xmake/modules/private/service/stream.lua b/xmake/modules/private/service/stream.lua index 1025a6d87..5460a7cb9 100644 --- a/xmake/modules/private/service/stream.lua +++ b/xmake/modules/private/service/stream.lua @@ -20,6 +20,7 @@ -- imports import("core.base.object") +import("core.base.socket") import("core.base.bytes") -- define module @@ -35,6 +36,22 @@ function stream:init(sock) self._WCACHE_SIZE = 0 end +-- flush data +function stream:flush() + local cache = self._WCACHE + local cache_size = self._WCACHE_SIZE + if cache_size > 0 then + local sock = self._SOCK + local real = sock:send(cache, {block = true, last = cache_size}) + if real > 0 then + self._WCACHE_SIZE = 0 + return true + end + else + return true + end +end + -- send the given bytes function stream:send(data, start, last) start = start or 1 @@ -61,6 +78,7 @@ function stream:send(data, start, last) assert(cache_size == cache_maxn) -- send data to socket + local sock = self._SOCK local real = sock:send(cache, {block = true}) if real > 0 then -- copy left data to cache @@ -110,16 +128,17 @@ function stream:recv(buff, size) local real = 0 local data = nil local wait = false + local sock = self._SOCK while buffsize < size do real, data = sock:recv(cache) if real > 0 then -- append data to buffer local leftsize = size - buffsize if real < leftsize then - buff:copy2(buffsize, data) + buff:copy2(buffsize + 1, data) buffsize = buffsize + real else - buff:copy2(buffsize, data, 1, leftsize) + buff:copy2(buffsize + 1, data, 1, leftsize) buffsize = buffsize + leftsize -- move left cache to head @@ -147,7 +166,7 @@ end function stream:recv_u16be() local data = self:recv(self._BUFF, 2) if data then - return data:u16be() + return data:u16be(1) end end |
