summaryrefslogtreecommitdiff
path: root/jnet/init.lua
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-08-12 21:51:38 +0800
committerchai <chaifix@163.com>2018-08-12 21:51:38 +0800
commitac0de268b46b136a6cdd6bbf096d93c1ff126329 (patch)
tree0e4f4305b3aded2c9ae59da3e33e141178d3e9c1 /jnet/init.lua
parentdbb4af0ea10640d062e1bc9e170be4a657c93d9d (diff)
update
Diffstat (limited to 'jnet/init.lua')
-rw-r--r--jnet/init.lua51
1 files changed, 51 insertions, 0 deletions
diff --git a/jnet/init.lua b/jnet/init.lua
new file mode 100644
index 0000000..76a023f
--- /dev/null
+++ b/jnet/init.lua
@@ -0,0 +1,51 @@
+local jnet = {}
+
+jnet.DataType = {
+ INT = 1,
+ FLOAT = 2,
+ BOOL = 3,
+ STRING = 4
+}
+
+local INT = jnet.DataType.INT
+local FLOAT = jnet.DataType.FLOAT
+local BOOL = jnet.DataType.BOOL
+local STRING = jnet.DataType.STRING
+
+local grab = function(buffer, t, sp)
+ if t == INT then
+ return buffer:grabInteger(sp)
+ elseif t == FLOAT then
+ return buffer:grabFloat(sp)
+ elseif t == BOOL then
+ return buffer:grabBoolean(sp)
+ elseif t == STRING then
+ return buffer:grabString(sp)
+ end
+end
+
+-- return jin.net.Buffer
+jnet.serialize = function(message, buffer)
+ if buffer == nil then
+ return
+ end
+ for k, v in pairs(message) do
+ buffer:append(v)
+ end
+ return buffer
+end
+
+-- input jin.net.Buffer, startpoint
+-- return messege table
+jnet.deserialize = function(msg_type, buffer, startpoint)
+ local message = {}
+ local p = startpoint or 0
+ for k, t in pairs(msg_type) do
+ local v, l = grab(buffer, t, p)
+ p = l + p
+ message[k] = v
+ end
+ return message
+end
+
+return jnet \ No newline at end of file