Archived
1
0
Fork 0
forked from Moux23333/FreeSR
This repository has been archived on 2024-03-30. You can view files and clone it, but cannot push or open issues or pull requests.
FreeSR/FreeSR.Gateserver/Network/Handlers/Decoder/StarRailHeaderDecoder.cs

54 lines
1.6 KiB
C#
Raw Normal View History

2024-01-27 13:06:07 +00:00
namespace FreeSR.Gateserver.Network.Handlers.Decoder
{
using DotNetty.Buffers;
using DotNetty.Codecs;
using DotNetty.Transport.Channels;
internal class StarRailHeaderDecoder : ByteToMessageDecoder
{
private IByteBuffer _current;
public StarRailHeaderDecoder()
{
_current = Unpooled.Buffer();
}
protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List<object> output)
{
var allocated = Unpooled.Buffer();
allocated.WriteBytes(_current);
allocated.WriteBytes(input);
_current = allocated;
byte[] lands = new byte[_current.ReadableBytes];
_current.ReadBytes(lands);
_current.ResetReaderIndex();
IByteBuffer packet;
while ((packet = Process()) != null)
output.Add(packet);
}
private IByteBuffer Process()
{
if (_current.ReadableBytes < 12)
return null;
_current.ReadInt(); // headMagic
_current.ReadShort(); // CmdID
int headerLength = _current.ReadShort();
int payloadLength = _current.ReadInt();
int totalLength = 12 + headerLength + payloadLength + 4;
_current.ResetReaderIndex();
if (_current.ReadableBytes < totalLength)
return null;
IByteBuffer result = _current.ReadBytes(totalLength);
_current = _current.ReadBytes(_current.ReadableBytes);
return result;
}
}
}