--
ShunWoPackageHandler = class("ShunWoPackageHandler")
socketListeners = {}
function ShunWoPackageHandler:ctor()
ClassUtil.extends(self, AbstractPackageHandler);
self._packageLenByteNum = ffiMgr:sizeOf("TCP_Info"); --包长度的尺寸,即前几字节是包长度
self._packageHeadByteNum = ffiMgr:sizeOf("TCP_Head"); --包长度的尺寸,即前几字节是包长度
createSetterGetter(self, "dataType", nil)
end
function ShunWoPackageHandler:cookOject2Bytes(bodyStruct, serialId, mainCmdId, subCmdId, truncateLen, notUseFfi)
local bodyStr = ""
if bodyStruct then
if notUseFfi then
bodyStr = bodyStruct;
else
bodyStr = ffiMgr:toLuaString(bodyStruct, truncateLen);
end
end
local bodyLen = #bodyStr;
local bufferStruct = ffiMgr:createCStruct("TCP_Buffer");
bufferStruct.Head.CommandInfo.wMainCmdID = mainCmdId;
bufferStruct.Head.CommandInfo.wSubCmdID = subCmdId;
local packageLen = bodyLen + self._packageHeadByteNum;
bufferStruct.Head.TCPInfo.wPacketSize = packageLen;
bufferStruct.cbBuffer = bodyStr;
local dataStr = ffiMgr:toLuaString(bufferStruct, packageLen)
-- return dataStr
local t = CryptoSwManager_mappedBuffer(dataStr, self._dataType)
return t.buffer;
end
-- 这里要注意一下,旧的服务器是整包中TCP_Info(TCP_Head的前部分)之后的包内容才加密,
-- 所以我们此时读取TCP_HEAD时,其中TCP_COMMAND是加密过的,不能保存为self._tcpHeaderCStruct
function ShunWoPackageHandler:readPackageLen()
local curPos = self._buffer:getPosition();
local headerInfoStr = self._buffer:readBytes2String(self._packageLenByteNum);
self._buffer:setPosition(curPos)
local tempHeaderInfoStruct = ffiMgr:toCStruct(headerInfoStr, "TCP_Info")
return tempHeaderInfoStruct.wPacketSize;
end
function ShunWoPackageHandler:handlePackage(byteArray)
local bytesLen = byteArray:getBytesAvailable();
if bytesLen > 0 then
local byteStr = byteArray:readBytes2String(byteArray:getBytesAvailable());
local resultTable = CryptoSwManager_unmapBuffer(byteStr, self._dataType)
if resultTable then
byteStr = resultTable.buffer;
local tcpHeadStr = string.sub(byteStr, 1, self._packageHeadByteNum);
local bodyStr = string.sub(byteStr, self._packageHeadByteNum + 1);
local tcpHeaderCStruct = ffiMgr:toCStruct(tcpHeadStr, "TCP_Head");
local mainId = tonumber(tcpHeaderCStruct.CommandInfo.wMainCmdID);
local subId = tonumber(tcpHeaderCStruct.CommandInfo.wSubCmdID);
-- print("服务器返回协议", mainId, subId)
local function handleObj()
networkMgr:onSocketDataReceived(mainId, subId, bodyStr, self._dataType)
end
xpcall(handleObj, __G__TRACKBACK__);
end
end
end
--错误返回处理
-- 默认的的服务器请求错误处理
function ShunWoPackageHandler.defaultSocketErrorCallback(data)
-- local resultCode = YiNiuPdkPackageHandler.getResultCode(data);
-- resultCode = parseInt(resultCode);
-- local config = getCSVField("errorMessage")[resultCode]
-- if config then
-- if config.tipType == 1 then
-- popupMgr:showAlert(config.msg);
-- else
-- tweenMsgMgr:showRedMsg(config.msg);
-- end
-- -- errorMgr:handlerServerTips(resultCode, data)
-- else
-- tweenMsgMgr:showRedMsg(I18n.get("c1577", tostring(resultCode)));
-- end
-- trace("服务器报错,错误代码:" .. tostring(resultCode));
end