Something went wrong. Try again.
Modular scanner/radio client and multi-broadcast server. uniscan.dexrn.me
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061using System.Text;using DotNetty.Buffers;using DotNetty.Codecs;using DotNetty.Transport.Channels;using MessagePack;using Serilog;using Shiki.Common.Identity;using UniScan.Core;using UniScan.Core.Extensions;using UniScan.Network.Registry;
namespace UniScan.Network.Protocol;
public class PacketEncoder(PacketRegistry packetRegistry, MessagePackSerializerOptions? options = null, CancellationToken ct = default) : MessageToByteEncoder<IPacket>{ private readonly ILogger _logger = Log.ForContext<PacketEncoder>();
protected override void Encode(IChannelHandlerContext context, IPacket message, IByteBuffer output) { Type type = message.GetType(); Identifier? id = packetRegistry.GetIdentifier(type); if (id == null) { _logger.Error("Attempted to encode unregistered packet of type '{Type}'", type.FullName); context.CloseAsync(); return; } byte[] str = Encoding.UTF8.GetBytes(id.ToString()); if (str.Length > ushort.MaxValue) { _logger.Error("Packet identifier too long ({Length}/{MaxLength}): {Identifier}", str.Length, ushort.MaxValue, id.ToString()); context.CloseAsync(); return; } output.WriteUnsignedShortLE((ushort)str.Length); output.WriteBytes(str);
try { byte[] msg = MessagePackSerializer.Serialize(type, message, options, ct);
#if DEBUG Log.Debug("Sent {Type} ({Size}): {lines}", type.FullName, msg.Length, msg.AsSpan().ToHexViewString(HexViewStringArgs.DefaultConsole)); #endif
output.WriteBytes(msg); } catch (FormatterNotRegisteredException ex) { _logger.Error(ex, "Attempted to serialize '{Type}' but no formatter was registered to handle it", message.GetType().FullName); context.CloseAsync(); } catch (MessagePackSerializationException ex) { _logger.Error(ex, "Failed to serialize packet '{Type}'", message.GetType().FullName); context.CloseAsync(); } }}