Struct PacketMemory
- Namespace
- VpnHood.Net.Packets
- Assembly
- VpnHood.Net.Packets.dll
The memory a packet is made on, and who takes it back when the packet is disposed.
public readonly struct PacketMemory
- Inherited Members
Remarks
Made from a Memory<T>, an array or an ArraySegment<T>, which convert to it, the memory stays the caller's: the packet reads and writes it, and leaves it when disposed. Made from an IMemoryOwner<T>, or from an array rented from an ArrayPool<T> (FromRentedArray(byte[], int, ArrayPool<byte>?)), the memory is given to the packet made on it, which disposes the owner or returns the array once, when the packet is disposed; when making the packet fails, the memory stays the caller's. Give such memory to one packet only: a copy of the struct is the same memory.
A rented array needs no object to own it, so code that makes a packet from each read makes no object besides the packet.
Constructors
PacketMemory(IMemoryOwner<byte>)
Initializes a new instance of the PacketMemory struct over an owner's memory, which the packet made on it disposes.
public PacketMemory(IMemoryOwner<byte> memoryOwner)
Parameters
memoryOwnerIMemoryOwner<byte>The owner of the memory.
Exceptions
- ArgumentException
memoryOwneris also an ArrayPool<T>.
PacketMemory(Memory<byte>)
Initializes a new instance of the PacketMemory struct over memory the caller keeps.
public PacketMemory(Memory<byte> memory)
Parameters
Properties
Memory
Gets the memory.
public Memory<byte> Memory { get; }
Property Value
Methods
FromRentedArray(byte[], int, ArrayPool<byte>?)
Makes a PacketMemory over the first bytes of an array rented from an ArrayPool<T>, which the packet made on it returns to that pool.
public static PacketMemory FromRentedArray(byte[] rentedArray, int length, ArrayPool<byte>? arrayPool = null)
Parameters
rentedArraybyte[]The array.
lengthintHow many of the array's first bytes the memory takes, such as the bytes read into it: a pool lends an array longer than asked for, and past them it holds what was written to it before.
arrayPoolArrayPool<byte>
Returns
- PacketMemory
The memory, over the array's first
lengthbytes.
Exceptions
- ArgumentOutOfRangeException
lengthis negative or exceeds the array.
Operators
implicit operator PacketMemory(ArraySegment<byte>)
Converts an array segment the caller keeps.
public static implicit operator PacketMemory(ArraySegment<byte> segment)
Parameters
segmentArraySegment<byte>The array segment.
Returns
implicit operator PacketMemory(byte[])
Converts an array the caller keeps.
public static implicit operator PacketMemory(byte[] array)
Parameters
arraybyte[]The array.
Returns
implicit operator PacketMemory(Memory<byte>)
Converts memory the caller keeps.
public static implicit operator PacketMemory(Memory<byte> memory)