aboutsummaryrefslogtreecommitdiff
path: root/src/lua/embed/net.lua.h
blob: 3db9264362033dafca02138cdbbb6ec2f6c3177c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* net.lua */
static const char* net_lua = R"(
jin.net = jin.net or {} 

--[[
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.dataSize = {
    INT = 4, 
    FLOAT = 4, 
    BOOL = 4, 
    STRING = -1, 
}

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 

)";