当前位置:网站首页>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 .
边栏推荐
- LeetCode 112:路径总和
- 关于df[‘某一列名’][序号]
- 【Golang】- runtime.Goexit()
- Process -- user address space and kernel address space
- Minimalist thrift+consumer
- Plato launched the LAAS protocol elephant swap, which allows users to earn premium income
- Merkel Studio - harmonyos implementation list to do
- mysql的执行顺序
- numpy. Where() usage and np.argsort() usage
- [unity project practice] synthetic watermelon
猜你喜欢
【公开课预告】:快手GPU/FPGA/ASIC异构平台的应用探索
Tomorrow infinite plan, 2022 conceptual planning scheme for a company's yuanuniverse product launch
把逻辑做在Sigma-DSP中的优化实例-数据分配器
[unity project practice] synthetic watermelon
我们总结了 3 大Nacos使用建议,并首次公开 Nacos 3.0 规划图 Nacos 开源 4 周年
[the road of Exile - Chapter 8]
StoneDB 邀请您参与开源社区月会!
OpenGL development with QT (II) drawing cube
【GoLang】网络连接 net.Dial
Anaconda environment installation problem
随机推荐
[the road of Exile - Chapter 7]
PCL 点云转强度图像
JS event introduction
Cross modal alignment 20220728
采用QT进行OpenGL开发(二)绘制立方体
规划数学期末考试模拟二
StoneDB 邀请您参与开源社区月会!
Planning mathematics final exam simulation II
[7.21-26] code source - [good sequence] [social circle] [namonamo]
Data security is a competitive advantage. How can companies give priority to information security and compliance
Timer of BOM series
【流放之路-第七章】
JS 定时器setInterval clearInterval 延时器setTimeOut 异步 动画
Window object of BOM series
How to choose professional, safe and high-performance remote control software
Slow storage scheme
我们总结了 3 大Nacos使用建议,并首次公开 Nacos 3.0 规划图 Nacos 开源 4 周年
T-sne dimensionality reduction
Tomorrow infinite plan, 2022 conceptual planning scheme for a company's yuanuniverse product launch
[golang] network connection net.dial