1
- require (" crc" )
2
- local pb = require " pb"
3
- local protoc = require " protoc"
4
- m_PacketCreateMap = {}
5
- m_PacketMap = {}
6
-
7
-
8
- -- 加载pb
9
- assert (pb .loadfile " ./../../message/pb/message.pb" )
10
- assert (pb .loadfile " ./../../message/pb/game.pb" )
11
- assert (pb .loadfile " ./../../message/pb/client.pb" )
12
-
13
- -- 创建包头
14
- function BuildPacketHead (id , destservertype )
15
- return {
16
- Stx = 0x27 ,
17
- DestServerType = destservertype ,
18
- Ckx = 0x72 ,
19
- Id = id
20
- }
21
- end
22
-
23
- -- 包处理回调函数
24
- function RegisterPacket (packetName , func )
25
- name = string.lower (packetName )
26
- id = CRC32 .hash (name )
27
- m_PacketCreateMap [id ] = " message." .. packetName
28
- m_PacketMap [id ] = func
29
- end
30
-
31
- -- 处理包函数
32
- function HandlePacket (dat )
33
- id = bytes_to_int (string.sub (dat , 0 , 4 ))
34
- buff = string.sub (dat , 5 )
35
- packetName = m_PacketCreateMap [id ]
36
- if packetName ~= nil then
37
- local packet = pb .decode (packetName , buff )
38
- m_PacketMap [id ](packet )
39
- end
40
- end
41
-
42
- -- 发送包函数
43
- function SendPacket (name , packet )
44
- name = string.lower (name )
45
- id = CRC32 .hash (name )
46
- packetName = m_PacketCreateMap [id ]
47
- if packetName ~= nil then
48
- local bytes = pb .encode (packetName , packet )
49
- bytes = int_to_bytes (id ) .. bytes .. TCP_END
50
- CLIENTSOCKET :Send (bytes )
51
- end
52
- end
53
-
54
- function bytes_to_int (str ,endian ,signed ) -- use length of string to determine 8,16,32,64 bits
55
- local t = {str :byte (1 ,- 1 )}
56
- if endian == " big" then -- reverse bytes
57
- local tt = {}
58
- for k = 1 ,# t do
59
- tt [# t - k + 1 ]= t [k ]
60
- end
61
- t = tt
62
- end
63
- local n = 0
64
- for k = 1 ,# t do
65
- n = n + t [k ]* 2 ^ ((k - 1 )* 8 )
66
- end
67
- if signed then
68
- n = (n > 2 ^ (# t - 1 ) - 1 ) and (n - 2 ^# t ) or n -- if last bit set, negative.
69
- end
70
- return n
71
- end
72
-
73
- function int_to_bytes (num ,endian ,signed )
74
- if num < 0 and not signed then num =- num print " warning, dropping sign from number converting to unsigned" end
75
- local res = {}
76
- local n = math.ceil (select (2 ,math.frexp (num ))/ 8 ) -- number of bytes to be used.
77
- if signed and num < 0 then
78
- num = num + 2 ^ n
79
- end
80
- for k = n ,1 ,- 1 do -- 256 = 2^8 bits per char.
81
- local mul = 2 ^ (8 * (k - 1 ))
82
- res [k ]= math.floor (num / mul )
83
- num = num - res [k ]* mul
84
- end
85
- assert (num == 0 )
86
- if endian == " big" then
87
- local t = {}
88
- for k = 1 ,n do
89
- t [k ]= res [n - k + 1 ]
90
- end
91
- res = t
92
- end
93
- return string.char (table.unpack (res ))
94
- end
95
-
96
- -- lua table data
97
- local data = {
98
- PacketHead = {
99
- Stx = 0x27 ,
100
- DestServerType = 2 ,
101
- Ckx = 0x72 ,
102
- Id = 0
103
- },
104
- Sender = 1000 ,
105
- SenderName = " test"
106
- }
107
-
108
- -- local bytes = assert(pb.encode("message.W_C_ChatMessage", data))
109
- -- print(pb.type("message.W_C_ChatMessage"))
110
- -- encode lua table data into binary format in lua string and return
111
- -- local data2 = assert(pb.decode("message.W_C_ChatMessage", bytes))
1
+ require (" crc" )
2
+ local pb = require " pb"
3
+ local protoc = require " protoc"
4
+ m_PacketCreateMap = {}
5
+ m_PacketMap = {}
6
+
7
+
8
+ -- 加载pb
9
+ assert (pb .loadfile " ./../../message/pb/message.pb" )
10
+ assert (pb .loadfile " ./../../message/pb/game.pb" )
11
+ assert (pb .loadfile " ./../../message/pb/client.pb" )
12
+
13
+ -- 创建包头
14
+ function BuildPacketHead (id , destservertype )
15
+ return {
16
+ Stx = 0x27 ,
17
+ DestServerType = destservertype ,
18
+ Ckx = 0x72 ,
19
+ Id = id
20
+ }
21
+ end
22
+
23
+ -- 包处理回调函数
24
+ function RegisterPacket (packetName , func )
25
+ name = string.lower (packetName )
26
+ id = CRC32 .hash (name )
27
+ m_PacketCreateMap [id ] = " message." .. packetName
28
+ m_PacketMap [id ] = func
29
+ end
30
+
31
+ -- 处理包函数
32
+ function HandlePacket (dat )
33
+ id = bytes_to_int (string.sub (dat , 0 , 4 ))
34
+ buff = string.sub (dat , 5 )
35
+ packetName = m_PacketCreateMap [id ]
36
+ if packetName ~= nil then
37
+ local packet = pb .decode (packetName , buff )
38
+ m_PacketMap [id ](packet )
39
+ end
40
+ end
41
+
42
+ -- 发送包函数
43
+ function SendPacket (name , packet
44
+ id = crc32 .Gen (string .lower (name ))
45
+ packetName = " message." .. name
46
+ if packetName ~= nil then
47
+ local bytes = pb .encode (packetName , packet )
48
+ bytes = int_to_bytes (id ) .. bytes .. TCP_END
49
+ CLIENTSOCKET :Send (bytes )
50
+ end
51
+ end
52
+
53
+ function bytes_to_int (str ,endian ,signed ) -- use length of string to determine 8,16,32,64 bits
54
+ local t = {str :byte (1 ,- 1 )}
55
+ if endian == " big" then -- reverse bytes
56
+ local tt = {}
57
+ for k = 1 ,# t do
58
+ tt [# t - k + 1 ]= t [k ]
59
+ end
60
+ t = tt
61
+ end
62
+ local n = 0
63
+ for k = 1 ,# t do
64
+ n = n + t [k ]* 2 ^ ((k - 1 )* 8 )
65
+ end
66
+ if signed then
67
+ n = (n > 2 ^ (# t - 1 ) - 1 ) and (n - 2 ^# t ) or n -- if last bit set, negative.
68
+ end
69
+ return n
70
+ end
71
+
72
+ function int_to_bytes (num ,endian ,signed )
73
+ if num < 0 and not signed then num =- num print " warning, dropping sign from number converting to unsigned" end
74
+ local res = {}
75
+ local n = math.ceil (select (2 ,math.frexp (num ))/ 8 ) -- number of bytes to be used.
76
+ if signed and num < 0 then
77
+ num = num + 2 ^ n
78
+ end
79
+ for k = n ,1 ,- 1 do -- 256 = 2^8 bits per char.
80
+ local mul = 2 ^ (8 * (k - 1 ))
81
+ res [k ]= math.floor (num / mul )
82
+ num = num - res [k ]* mul
83
+ end
84
+ assert (num == 0 )
85
+ if endian == " big" then
86
+ local t = {}
87
+ for k = 1 ,n do
88
+ t [k ]= res [n - k + 1 ]
89
+ end
90
+ res = t
91
+ end
92
+ return string.char (table.unpack (res ))
93
+ end
94
+
95
+ -- lua table data
96
+ local data = {
97
+ PacketHead = {
98
+ Stx = 0x27 ,
99
+ DestServerType = 2 ,
100
+ Ckx = 0x72 ,
101
+ Id = 0
102
+ },
103
+ Sender = 1000 ,
104
+ SenderName = " test"
105
+ }
106
+
107
+ -- local bytes = assert(pb.encode("message.W_C_ChatMessage", data))
108
+ -- print(pb.type("message.W_C_ChatMessage"))
109
+ -- encode lua table data into binary format in lua string and return
110
+ -- local data2 = assert(pb.decode("message.W_C_ChatMessage", bytes))
112
111
-- print(require "serpent".block(data2))
0 commit comments