2024-05-15 20:34:51 +02:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2024-05-25 11:16:37 +02:00
|
|
|
using ServicePoint.BindGen;
|
2024-05-15 20:34:51 +02:00
|
|
|
|
2024-05-25 11:16:37 +02:00
|
|
|
namespace ServicePoint;
|
2024-05-15 20:34:51 +02:00
|
|
|
|
2024-10-16 21:59:35 +02:00
|
|
|
public sealed class Packet : SpNativeInstance<SPPacket>
|
2024-05-15 20:34:51 +02:00
|
|
|
{
|
|
|
|
public static Packet FromCommand(Command command)
|
|
|
|
{
|
|
|
|
unsafe
|
|
|
|
{
|
2024-10-16 21:59:35 +02:00
|
|
|
return new Packet(PacketNative.sp_packet_from_command(command.Into()));
|
2024-05-15 20:34:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool TryFromBytes(Span<byte> bytes, [MaybeNullWhen(false)] out Packet packet)
|
|
|
|
{
|
|
|
|
unsafe
|
|
|
|
{
|
|
|
|
fixed (byte* bytesPtr = bytes)
|
|
|
|
{
|
2024-10-16 21:59:35 +02:00
|
|
|
var instance = PacketNative.sp_packet_try_load(bytesPtr, (nuint)bytes.Length);
|
2024-05-15 20:34:51 +02:00
|
|
|
packet = instance == null
|
|
|
|
? null
|
|
|
|
: new Packet(instance);
|
|
|
|
return packet != null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-16 21:59:35 +02:00
|
|
|
private unsafe Packet(SPPacket* instance) : base(instance)
|
2024-05-15 20:34:51 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-10-16 21:59:35 +02:00
|
|
|
private protected override unsafe void Free() => PacketNative.sp_packet_free(Instance);
|
2024-05-15 20:34:51 +02:00
|
|
|
}
|