45 lines
1.3 KiB
C#
Raw Normal View History

2024-08-17 14:27:18 +08:00
using System;
using System.Linq;
using System.Reflection;
using Google.Protobuf;
using Plugins.JNGame.Network.Entity;
using Plugins.JNGame.Util;
namespace Plugins.JNGame.Network.Util
{
// 网络数据工具类 [请求Id*4,处理Id*4,...参数数据*N]
public static class NDataUtil
{
// 解析
public static JNetParam Parse(byte[] data)
{
return JNetParam.Build(
ToUtil.Byte4ToInt(data.Skip(0).Take(4).ToArray()),
ToUtil.Byte4ToInt(data.Skip(4).Take(4).ToArray()))
.SetByte(data.Skip(8).Take(data.Length - 8).ToArray());
}
// 编码
public static byte[] Encrypt(JNetParam param)
{
byte[] id = ToUtil.IntToByte4(param.ID);
byte[] hId = ToUtil.IntToByte4(param.HId);
byte[] data = Array.Empty<byte>();
if(param.Data != null)
data = param.Data.ToByteArray();
byte[] datas = new byte[hId.Length+id.Length+data.Length];
Array.Copy(id, 0, datas, 0, id.Length);
Array.Copy(hId, 0, datas, id.Length, hId.Length);
Array.Copy(data, 0, datas, id.Length + hId.Length, data.Length);
return datas;
}
}
}