mirror of
https://github.com/cccb/servicepoint.git
synced 2025-01-19 10:20:12 +01:00
37 lines
984 B
C#
37 lines
984 B
C#
using System.Diagnostics.CodeAnalysis;
|
|
using ServicePoint.BindGen;
|
|
|
|
namespace ServicePoint;
|
|
|
|
public sealed class Packet : SpNativeInstance<BindGen.Packet>
|
|
{
|
|
public static Packet FromCommand(Command command)
|
|
{
|
|
unsafe
|
|
{
|
|
return new Packet(NativeMethods.sp_packet_from_command(command.Into()));
|
|
}
|
|
}
|
|
|
|
public static bool TryFromBytes(Span<byte> bytes, [MaybeNullWhen(false)] out Packet packet)
|
|
{
|
|
unsafe
|
|
{
|
|
fixed (byte* bytesPtr = bytes)
|
|
{
|
|
var instance = NativeMethods.sp_packet_try_load(bytesPtr, (nuint)bytes.Length);
|
|
packet = instance == null
|
|
? null
|
|
: new Packet(instance);
|
|
return packet != null;
|
|
}
|
|
}
|
|
}
|
|
|
|
private unsafe Packet(BindGen.Packet* instance) : base(instance)
|
|
{
|
|
}
|
|
|
|
private protected override unsafe void Free() => NativeMethods.sp_packet_free(Instance);
|
|
}
|