servicepoint/servicepoint2-binding-cs/src/Packet.cs
Vinzenz Schroeter 1dad113ca1 a bunch of minor changes combined:
- From instead of Into
- unsafe_data_ref for other payloads
- CByteSlice for returning memory spans
- send Packet instead of Into<Packet>
- expose packet layer to C/C#
2024-05-15 20:34:51 +02:00

40 lines
1,013 B
C#

using System.Diagnostics.CodeAnalysis;
using ServicePoint2.BindGen;
namespace ServicePoint2;
public sealed class Packet : Sp2NativeInstance<BindGen.Packet>
{
public static Packet FromCommand(Command command)
{
unsafe
{
return new Packet(NativeMethods.sp2_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.sp2_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 Dealloc()
{
NativeMethods.sp2_packet_dealloc(Instance);
}
}