summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-04-07 23:08:46 +0800
committerruki <[email protected]>2022-04-07 23:08:46 +0800
commitb6d6a1145f77a1b0d5d0685ba4af1e363b4410cd (patch)
tree5c470ca3143cc9c942f5e7256fadc18a23da9869
parent102b2c2b5d33b31ee7660e77fcbacac9bc5677a4 (diff)
send object
-rw-r--r--xmake/modules/private/service/connect_service.lua2
-rw-r--r--xmake/modules/private/service/server/server.lua8
-rw-r--r--xmake/modules/private/service/stream.lua23
3 files changed, 25 insertions, 8 deletions
diff --git a/xmake/modules/private/service/connect_service.lua b/xmake/modules/private/service/connect_service.lua
index 682e9b1f2..dc98622f7 100644
--- a/xmake/modules/private/service/connect_service.lua
+++ b/xmake/modules/private/service/connect_service.lua
@@ -49,7 +49,7 @@ function _connect(addr, port)
if sock then
print("%s: connected!", client)
local wstream = stream(sock)
- if wstream:send_string("hello xmake!") and wstream:flush() then
+ if wstream:send_object({name = "msg", body = "hello xmake!", func = function () print("hello!") end}) and wstream:flush() then
print("send ok")
end
io.save(statusfile, {addr = addr, port = port})
diff --git a/xmake/modules/private/service/server/server.lua b/xmake/modules/private/service/server/server.lua
index 534a2da15..721af504d 100644
--- a/xmake/modules/private/service/server/server.lua
+++ b/xmake/modules/private/service/server/server.lua
@@ -122,9 +122,11 @@ function server:_handle_session(sock)
print("%s: %s session connected", self, sock)
local rstream = stream(sock)
while true do
- local data = rstream:recv_string()
- if data then
- print("%s", data)
+ local msg = rstream:recv_object()
+ if msg then
+ -- TODO
+ print(msg)
+ msg.func()
else
break
end
diff --git a/xmake/modules/private/service/stream.lua b/xmake/modules/private/service/stream.lua
index 226ee22e0..dd4ff2aec 100644
--- a/xmake/modules/private/service/stream.lua
+++ b/xmake/modules/private/service/stream.lua
@@ -89,8 +89,15 @@ function stream:send(data, start, last)
end
end
--- send table
-function stream:send_table(tbl)
+-- send object
+function stream:send_object(obj)
+ local str, errors = string.serialize(obj, {strip = true, indent = false})
+ if errors then
+ raise(errors)
+ end
+ if str then
+ return self:send_string(str)
+ end
end
-- send string
@@ -170,8 +177,16 @@ function stream:recv_u16be()
end
end
--- recv table
-function stream:recv_table()
+-- recv object
+function stream:recv_object()
+ local str = self:recv_string()
+ if str then
+ local obj, errors = str:deserialize()
+ if errors then
+ raise(errors)
+ end
+ return obj
+ end
end
-- recv string