marineparkclient/assets/common/net/protobufUtil.ts
2023-08-15 11:09:12 +08:00

136 lines
4.6 KiB
TypeScript

import * as protobundle from './pb/pbBase'
const g_dwEncryptKey = 0x8F934167;
const RandValueMax = 0x7fffffff;
const DeEnCodeType = 0; //加解密类型
// 定义选项
export interface MsgGameStruct {
mainId:number;
subId:number;
msgBody:Uint8Array;
}
function RandSLocal(dwRandSeed:number):number
{
dwRandSeed = (dwRandSeed * 214013 + 2531011) & RandValueMax;
return dwRandSeed;
}
function EncryptBuffer(cbBuffer:Uint8Array, wDataSize:number):Uint8Array
{
let dwEncryptKey = g_dwEncryptKey + wDataSize;
dwEncryptKey = RandSLocal(dwEncryptKey);
let utf8Array = new Uint8Array(wDataSize)
for (let dwI = 0; dwI < wDataSize; dwI++)
{
utf8Array[dwI] += dwEncryptKey;
}
return utf8Array
}
function DecryptBuffer(cbBuffer:Uint8Array, wDataSize:number):Uint8Array
{
let dwDecryptKey = g_dwEncryptKey + wDataSize;
dwDecryptKey = RandSLocal(dwDecryptKey);
let utf8Array = new Uint8Array(wDataSize)
for (let dwI = 0; dwI < wDataSize; dwI++)
{
utf8Array[dwI] += dwDecryptKey;
}
return utf8Array
}
class ProtoBuffUtil{
GetTokenStr(bodyBuf:Uint8Array)
{
let msgInfo = protobundle.baseproto.Msg_TokenBody.decode(bodyBuf)
return msgInfo
}
// 解析方法
GetMsgHeadBody(bodyBuf:Uint8Array):MsgGameStruct|null { //传入消息
let buffHead = protobundle.baseproto.Msg_Head.decode(bodyBuf)
if(buffHead==null)
return null
return {mainId:buffHead.wMainCmdID,subId:buffHead.wSubCmdID,msgBody:buffHead.msgBody} //
};
//加密
EnCodeBodyMsg(body:Uint8Array){
if (DeEnCodeType)//加密
{
body = EncryptBuffer(body,body.length);//body加密
}
//填充消息包
let msgPack = {cbDataKind:DeEnCodeType,wPacketSize:body.length,msgBuff:body}
let uint8MsgPack = protobundle.baseproto.Msg_Info.encode(msgPack).finish()
return uint8MsgPack
}
//解密
DeCodeBodyMsg = function(body:Uint8Array){
let msgInfo = protobundle.baseproto.Msg_Info.decode(body)
let kind = msgInfo.cbDataKind;
let bodyLen = msgInfo.wPacketSize;
let utf8Array = msgInfo.msgBuff
if (bodyLen == msgInfo.msgBuff.length)//数据长度一致
{
if (kind==DeEnCodeType)
{
if (kind)
{
utf8Array = DecryptBuffer(msgInfo.msgBuff,bodyLen);//数据解密
return {a:1,b:utf8Array};//
}
else{
return {a:1,b:utf8Array};//
}
}
else
return {a:0,b:new Uint8Array()}//-1加解密字段不匹配
}
else
return {a:0,b:utf8Array};//消息长度不匹配
}
//填充方法
PushPbToSendMsg(mId:number,sId:number,bodyBuf:Uint8Array):Uint8Array
{
let headInfo = protobundle.baseproto.Msg_Head.create({wMainCmdID:mId,wSubCmdID:sId,msgBody:bodyBuf})
let uint8Data = protobundle.baseproto.Msg_Head.encode(headInfo).finish()
return uint8Data
}
//发送错误包体
SendMsgErro(codeNum:number,desp:string):Uint8Array
{
let ErroMsgWriter = protobundle.baseproto.MsgBodyErro.create({code:codeNum,disp:desp});
let ErroMsg = protobundle.baseproto.MsgBodyErro.encode(ErroMsgWriter).finish()
let uint8Data = this.PushPbToSendMsg(0,codeNum,ErroMsg);
let uint8MsgPack = this.EnCodeBodyMsg(uint8Data)
return uint8Data
}
//发送到
SendMsg(mId:number,sId:number,bodyBuf:Uint8Array):Uint8Array
{
let headInfo = protobundle.baseproto.Msg_Head.create({wMainCmdID:mId,wSubCmdID:sId,msgBody:bodyBuf})
let uint8Data = protobundle.baseproto.Msg_Head.encode(headInfo).finish()
let uint8MsgPack = this.EnCodeBodyMsg(uint8Data)
return uint8Data
}
//发送
SendMsgWithToken(token:string,mId:number,sId:number,bodyBuf:Uint8Array):Uint8Array
{
let headInfo = protobundle.baseproto.Msg_Head.create({wMainCmdID:mId,wSubCmdID:sId,msgBody:bodyBuf})
let uint8Data = protobundle.baseproto.Msg_Head.encode(headInfo).finish()
let tokenBody = protobundle.baseproto.Msg_TokenBody.create({strToken:token,msgBody:uint8Data})//填充token
let uint8TokenData = protobundle.baseproto.Msg_TokenBody.encode(tokenBody).finish()
let uint8MsgPack = this.EnCodeBodyMsg(uint8TokenData)
return uint8MsgPack
}
}
export default new ProtoBuffUtil()