为了配合服务器测试大端通信协议,自己结合网上资料写了个简易的供自己测试使用,特此记录。
socket 需要安装luasocket后才能使用。
今天发现在skynet中有相关案例,可以直接用,比自己瞎折腾的东西好多了。
不过这么折腾一回对自己掌握这一块内容是有帮助的。
local socket = require("socket")
local DataType = {
CHAR = 1,
SHORT = 2,
INT = 3,
INT64 = 4,
FLOAT = 5,
OTHER = 6,
ARRAY = 16,
}
local DataSize = {
SIZE_SHORT = 3,
SIZE_INT = 5,
SIZE_INT64 = 9,
SIZE_FLOAT = 5,
}
function readChar(buf,pos)
if pos+2 > string.len(buf)+1 then
return nil,pos
end
local t = string.unpack(">b",buf,pos)
pos = pos + 1
if t == DataType.CHAR then
local v = string.unpack(">b",buf,pos)
pos = pos + 1
return v,pos
end
return nil,pos
end
function readShort(buf,pos)
if pos+3 > string.len(buf)+1 then
return nil,pos
end
local t = string.unpack(">b",buf,pos)
pos = pos + 1
if t == DataType.SHORT then
local v = string.unpack(">h",buf,pos)
pos = pos + 2
return v,pos
end
return nil,pos
end
function readInt(buf,pos)
print(pos,"len = ",string.len(buf));
if pos+5 > string.len(buf)+1 then
return nil,pos
end
local t = string.unpack(">b",buf,pos)
pos = pos + 1
if t == DataType.INT then
local v = string.unpack(">i4",buf,pos)
pos = pos + 4
return v,pos
end
return nil,pos
end
function readInt64(buf,pos)
if pos+9 > string.len(buf)+1 then
return nil,pos
end
local t = string.unpack(">b",buf,pos)
pos = pos + 1
if t == DataType.INT64 then
local v = string.unpack(">l",buf,pos)
pos = pos + 8
return v,pos
end
return nil,pos
end
function readFloat(buf,pos)
if pos+5 > string.len(buf)+1 then
return nil,pos
end
local t = string.unpack(">b",buf,pos)
pos = pos + 1
if t == DataType.FLOAT then
local v = string.unpack(">f",buf,pos)
pos = pos + 4
return v,pos
end
return nil,pos
end
function readString(buf,pos)
if pos+3 > string.len(buf)+1 then
return nil,pos
end
local t = string.unpack(">b",buf,pos)
pos = pos + 1
if t == (DataType.ARRAY + DataType.CHAR) then
local v = string.unpack(">s2",buf,pos)
pos = pos + 2 + string.len(v)
return v,pos
end
return nil,pos
end
function readHead(buf,pos)
local head = {}
if string.len(buf) >= 6 then
head.cmd,head.seq,head.ret = string.unpack(">hhh",buf,pos)
pos = pos + 6
return head,pos
end
return nil,pos
end
function writeObjectArrayHeader(pkg,f,array_len)
local ret = string.pack(">bh",f,array_len)
pkg = pkg..ret
return pkg
end
function writeChar(pkg,c)
local ret = string.pack(">bb",DataType.CHAR,c)
pkg = pkg..ret
return pkg
end
function writeShort(pkg,i16)
local ret = string.pack(">bh",DataType.SHORT,i16)
pkg = pkg..ret
return pkg
end
function writeInt(pkg,i)
local ret = string.pack(">bi4",DataType.INT,i)
pkg = pkg..ret
return pkg
end
function writeInt64(pkg,i64)
local ret = string.pack(">bl",DataType.INT64,i64)
pkg = pkg..ret
return pkg
end
function writeFloat(pkg,f)
local ret = string.pack(">bi4",DataType.FLOAT,f)
pkg = pkg..ret
return pkg
end
function writeString(pkg,s)
local ret = string.pack(">bs2",(DataType.ARRAY + DataType.CHAR),s)
pkg = pkg..ret
return pkg
end
function writeHead(cmd,seq,ret)
local ret = string.pack(">hhh",cmd,seq,ret)
return ret
end
function unpack(buf_src)
local pos = 1
local head
local body = {}
local buf = crypt.xor_str(buf_src, "aCD390&6")
head,pos = readHead(buf,pos)
return head,body
end
local host = "127.0.0.1"
local port = 5000
local sock = assert(socket.connect(host, port))
sock:settimeout(0)
print("Press enter after input something:")
local input, recvt, sendt, status
while true do
local whtsend = io.read()
print("屏幕输入的内容:",whtsend)
local pkg = writeHead(whtsend,2,0)
pkg = writeString(pkg,"330374657362523456")
pkg = writeString(pkg,"张三")
-- pkg = writeString(pkg,"EFGH")
-- pkg = writeString(pkg,"9CBF8A4DCB8E30682B927F352D6559A0") --密码123456a
-- pkg = writeString(pkg,"E10ADC3949BA59ABBE56E057F20F883E") --密码:123456
local enc_pkg = crypt.xor_str(pkg,PKG_PWD)
local input = string.pack(">s2",enc_pkg)
print("需要发送的数据值:",input)
print("传送的数据的长度",string.len(input))
if #input > 0 then
assert(sock:send(input .. "\n"))
end
recvt, sendt, status = socket.select({sock}, nil, 1)
while #recvt > 0 do
local response, receive_status = sock:receive()
if receive_status ~= "closed" then
if response then
print(response)
recvt, sendt, status = socket.select({sock}, nil, 1)
end
else
break
end
end
end