You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.5 KiB

using DotNetty.Buffers;
using DotNetty.Codecs;
using DotNetty.Transport.Channels;
using HybirdFrameworkCore.Utils;
using log4net;
using Service.TBox.Msg;
using Service.TBox.Msg.TBox;
namespace Service.TBox.Codec;
public class Decoder : ByteToMessageDecoder
{
//TODO 实际开发时去掉
public static Queue<BaseMsg> Msgs { get; set; } = new();
//TODO 实际开发时去掉
public static Queue<byte[]> BytesQueue { get; set; } = new();
private static readonly ILog Log = LogManager.GetLogger(typeof(Decoder));
private readonly int _fixedLength = 13;
protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List<object> output)
{
if (_fixedLength <= input.ReadableBytes)
{
byte[] bytes = new byte[_fixedLength-1];
input.SkipBytes(1);
input.ReadBytes(bytes);
//TODO 实际开发时去掉
BytesQueue.Enqueue(bytes);
Log.Info($"receive {BitUtls.BytesToHexStr(bytes)}");
int id = BitConverter.ToInt32(new byte[] { bytes[3], bytes[2], bytes[1], bytes[0] }, 0);
BaseMsg baseMsg = id switch
{
0x1882D0F3 => ModelConvert.Decode<SocMsg>(bytes),
0x18FF48A8 => ModelConvert.Decode<StatusMsg>(bytes),
_ => ModelConvert.Decode<OtherMsg>(bytes),
};
//TODO 实际开发时去掉
Msgs.Enqueue(baseMsg);
output.Add(baseMsg);
}
}
}