diff options
Diffstat (limited to 'jnet/README.md')
-rw-r--r-- | jnet/README.md | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/jnet/README.md b/jnet/README.md new file mode 100644 index 0000000..28e53dd --- /dev/null +++ b/jnet/README.md @@ -0,0 +1,57 @@ + +socket通信的数据类型 +* INT 32 +* FLOAT 32 +* BOOL 32 +* STRING -- +STRING判定右界,以0结尾即可 +一个协议定义的例子(最好把协议统一放在一个单独的lua文件里) +-- s2c_package.lua +local INT = jin.net.dataType.INT +local FLOAT = jin.net.dataType.FLOAT +local BOOL = jin.net.dataType.BOOL +local STRING = jin.net.dataType.STRING +Skill = { + id = INT, + damage = FLOAT, + range = FLOAT, + description = STRING +} +发的时候 +local data, size = jin.net.serialize(Message.Skill, message) +Socket:send(data, size) +收的时候 +local data, size = Socket:receive() +local message = jin.net.deserialize(Message.Skill, data, size) + +jin.net.dataType = { + INT = 1, + FLOAT = 2, + BOOL = 3, + STRING = 4 +} + +jin.net.deserialize = function(prototype, data, size) + local message = {} + local i = 1 + for k, t in pairs(prototype) do + message[k] = data[i] + i = i + 1 + end + return message +end + +jin.net.serialize = function(prototype, message) + local data = "" + local size = 0 + for i, v in pairs(message) do + data, size = jin.bit.write(data, size, v) + end + return data, size +end + +-- 解密协议 +jin.net.decode = function() + local s = jin.bit.grabstring(buffer, size) +end +]]
\ No newline at end of file |