servicepoint/crates/servicepoint_binding_cs/ServicePoint/Packet.cs

37 lines
960 B
C#
Raw Normal View History

using System.Diagnostics.CodeAnalysis;
using ServicePoint.BindGen;
namespace ServicePoint;
2024-10-16 22:46:34 +02:00
public sealed class Packet : SpNativeInstance<BindGen.Packet>
{
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()));
}
}
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);
packet = instance == null
? null
: new Packet(instance);
return packet != null;
}
}
}
2024-10-16 22:46:34 +02:00
private unsafe Packet(BindGen.Packet* instance) : base(instance)
{
}
2024-10-16 22:46:34 +02:00
private protected override unsafe void Free() => Instance->Free();
}