summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-04-07 23:02:06 +0800
committerruki <[email protected]>2022-04-07 23:02:06 +0800
commite930a2db6840d9a6f501d4231889e045ba09d7d3 (patch)
treef1c6e1a79d13cdb090954d238aa59e6b241dd4d2
parent3c562917883423cbf50c94aecd2dc790b6bbf988 (diff)
fix bytes
-rw-r--r--xmake/modules/private/service/connect_service.lua5
-rw-r--r--xmake/modules/private/service/server/server.lua2
-rw-r--r--xmake/modules/private/service/stream.lua25
3 files changed, 28 insertions, 4 deletions
diff --git a/xmake/modules/private/service/connect_service.lua b/xmake/modules/private/service/connect_service.lua
index b103e769b..682e9b1f2 100644
--- a/xmake/modules/private/service/connect_service.lua
+++ b/xmake/modules/private/service/connect_service.lua
@@ -23,6 +23,7 @@ import("core.base.option")
import("core.base.socket")
import("core.base.scheduler")
import("private.service.config")
+import("private.service.stream")
import("private.service.client.remote_build_client")
function _get_address()
@@ -47,6 +48,10 @@ function _connect(addr, port)
print("%s: connect %s:%d ..", client, addr, port)
if sock then
print("%s: connected!", client)
+ local wstream = stream(sock)
+ if wstream:send_string("hello xmake!") and wstream:flush() then
+ print("send ok")
+ end
io.save(statusfile, {addr = addr, port = port})
else
print("%s: connect %s:%d failed", client, addr, port)
diff --git a/xmake/modules/private/service/server/server.lua b/xmake/modules/private/service/server/server.lua
index 534a2da15..95db2f5a5 100644
--- a/xmake/modules/private/service/server/server.lua
+++ b/xmake/modules/private/service/server/server.lua
@@ -124,7 +124,7 @@ function server:_handle_session(sock)
while true do
local data = rstream:recv_string()
if data then
- print("%s", data)
+ print("|%s|", data)
else
break
end
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