【德州扑克开发记录】客户端网络连接数据片段拼接,lua代码示例
http://www.cnblogs.com/litian/articles/2004470.html 这里说明了为什么要做数据片段拼接
示例代码:
--处理接收到的数据缓存
netDataProcess._onRecvData = function()
if #netDataProcess._recvDataCash < 1 then
return
end
local szDataCash = table.concat(netDataProcess._recvDataCash)
if string.len(szDataCash) < PACK_HEAD_LEN then---- print('不夠包頭長度')
return
end
local nPackLen = getNtohs(string.sub(szDataCash, 1, PACK_HEAD_LEN))
if string.len(szDataCash) - 4 < nPackLen then--还没有接受完
return
end
local nPackTypeLen = getNtohs(string.sub(szDataCash, 5, 6))
local szPackType = string.sub(szDataCash, 6 + 1, 6 + nPackTypeLen)
local szPackBody = string.sub(szDataCash, 6 + nPackTypeLen + 1, nPackLen + 4)
-- -- print('szPackType', szPackType)
if tbProtocols[szPackType] then
--移除遮罩
G_LoadingMgr.getInstance():removeLoadingLayer(szPackType)
tbProtocols[szPackType].callFunc(szPackType, szPackBody)
end
local szNextPack = string.sub(szDataCash, nPackLen + 5, string.len(szDataCash))
netDataProcess._recvDataCash = {}
if szNextPack and szNextPack ~= "" then
table.insert(netDataProcess._recvDataCash, szNextPack)
end
-- -- print('strPackBody', strPackBody)
--
--检查包里是否还有数据
if #netDataProcess._recvDataCash > 0 then
netDataProcess._onRecvData();
end
end