当前位置:网站首页>Lua third-party byte stream serialization and deserialization module --lpack
Lua third-party byte stream serialization and deserialization module --lpack
2022-07-29 01:54:00 【wufeng_ asia】
Recent research luasocket, Ready to use all lua To implement a lightweight framework , Used to make some tools 、 Simple game server , And as a network library used in cocos2dx in .
A perfect network library will inevitably encounter sticky packets 、 Half a package of questions ,luasocket No exception , Because the network part is lua, Formulation of the agreement and buff There is no suitable scheme for the analysis of , I don't want to be in C++ To encapsulate the interface , Later, I checked some information on the Internet , Find out lua There is also a binary packaged extension library ——lpack, After understanding, I found that it is still very easy to use , I decided to use it to do buff analysis , To solve sticking 、 Half a package of questions .
First you need to download lpack Source code , The address is lpack Download address , Then decompress , There are lpack.c and makefile file .
Then there's compilation lpack, There are two ways to compile and initialize lpack
- take lpack.c Add it to the source code of the host program , And then you initialize it lua Call the initialization function :
luaopen_pack(lua_state);
- take lpack Translate it into dll(so), And then in lua It calls :
require("lpack")
And then say lpack The interface of ,lpack The extension library provides two interfaces ,pack and unpack. But there are two ways to call , Use macros (USE_GLOBALS) To control , The first is to use the global interface (pack/unpack), The other is to insert these two interfaces into string In my space (string.pack/string.unpack).
lpack Specific usage of
- Packaging interface pack Use , The global name is easy to confuse lua Self function unpack, Use string.pack many , You can also modify the source code and function name .
--luapack The interface name modified for me
local _ss = luapack(">P", " China asd")
local _ss2 = luapack(">h", 500)
_ss = _ss.._ss2
print(_ss, #_ss)
-- The results
China asd 9
China asd 11
- Unpacking interface unpack Use
--luaunpack The interface name modified for me
-- Usage mode 1
local ne, value = luaunpack(_ss, ">P")
print(ne, value)
_ss = string.sub(_ss, ne, #_ss)
local ne, value = luaunpack(_ss, ">h")
print(ne, value)
-- The results
10 China asd
3 500
-- Usage mode 2
local ne, value1, value2 = luaunpack(_ss, ">Ph")
print(ne, value, value2)
-- The results
12 China asd 500
Package variable type definitions
#define OP_ZSTRING 'z' // An empty string
#define OP_BSTRING 'p' // The length is less than 2^8 String
#define OP_WSTRING 'P' // The length is less than 2^16 String
#define OP_SSTRING 'a' // The length is less than 2^32/64 String */
#define OP_STRING 'A' // Specify the length string
#define OP_FLOAT 'f' /* float */
#define OP_DOUBLE 'd' /* double */
#define OP_NUMBER 'n' /* Lua number */
#define OP_CHAR 'c' /* char */
#define OP_BYTE 'b' /* byte = unsigned char */
#define OP_SHORT 'h' /* short */
#define OP_USHORT 'H' /* unsigned short */
#define OP_INT 'i' /* int */
#define OP_UINT 'I' /* unsigned int */
#define OP_LONG 'l' /* long */
#define OP_ULONG 'L' /* unsigned long */
Packed byte order , Divided into the following 3 Kind of
#define OP_LITTLEENDIAN '<' /* little endian */
#define OP_BIGENDIAN '>' /* big endian */
#define OP_NATIVE '=' /* native endian */
Project examples
function parcel_method:read_ussd_buff()
local buf = string.sub(self.buffer, self.pos,#self.buffer)
local pos,hex_value
local str_buffer = ""
for index =1,#buf,2 do
pos, hex_value = string.unpack(buf,"<h")
-- log.debug("ussd hex_value=%d",hex_value)
if hex_value > 0 and hex_value < 128 then -- Input parameter must be ASCII code
local str_chr = string.char(hex_value)
str_buffer = str_buffer .. str_chr
end
buf = string.sub(buf,pos)
-- print(string.unpack(str,">h"))
end
self.pos = self.pos + pos - 1
-- log.debug("ussd " .. str_buffer)
return str_buffer
end
Okay , How to use it here lpack It's already clear , Then you can use lpack Characteristic design of buff To parse the network byte stream .
边栏推荐
- Focus on differentiated product design, intelligent technology efficiency improvement and literacy education around new citizen Finance
- Reinforcement learning (II): SARS, with code rewriting
- OpenGL development with QT (II) drawing cube
- We summarized the three recommendations for the use of Nacos and first published the Nacos 3.0 plan for the 4th anniversary of the open source of Nacos
- Super scientific and technological data leakage prevention system, control illegal Internet behaviors, and ensure enterprise information security
- Where will Jinan win in hosting the first computing power conference?
- PCL 点云转强度图像
- ASCII code table
- Anaconda environment installation problem
- MPEG音频编码三十年
猜你喜欢

Autoware reports an error: can't generate global path for start solution

Timer of BOM series

规划数学期末模拟考试一
![[search] - iteration deepening / bidirectional dfs/ida*](/img/88/d460276a657cfc49eff10015c29e5d.png)
[search] - iteration deepening / bidirectional dfs/ida*
![[observation] ranked first in SaaS of pure public cloud in three years, and yonsuite's](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[observation] ranked first in SaaS of pure public cloud in three years, and yonsuite's "flywheel effect"

mysql的执行顺序

Where will Jinan win in hosting the first computing power conference?

golang启动报错【已解决】

Planning mathematics final exam simulation II

Lua log implementation -- print table
随机推荐
Use of packet capturing tool Charles
internship:用于类型判断的工具类编写
Autoware reports an error: can't generate global path for start solution
Regular filtering data learning notes (①)
【流放之路-第七章】
Golang run times undefined error [resolved]
正则过滤数据学习笔记(①)
Practical experience of Google cloud spanner
5G 商用第三年:无人驾驶的“上山”与“下海”
LeetCode 112:路径总和
【10点公开课】:快手GPU/FPGA/ASIC异构平台的应用探索
TypeError: can only concatenate str (not “int“) to str
PCL 点云转强度图像
剑指offer专项突击版第13天
Anaconda environment installation problem
JVM learning minutes
Yocto project download and compilation
关于df[‘某一列名’][序号]
What are the common cyber threats faced by manufacturers and how do they protect themselves
golang启动报错【已解决】