Table of Contents

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

memoryOwner IMemoryOwner<byte>

The owner of the memory.

Exceptions

ArgumentException

memoryOwner is 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

memory Memory<byte>

The memory, which the packet made on it leaves to the caller.

Properties

Memory

Gets the memory.

public Memory<byte> Memory { get; }

Property Value

Memory<byte>

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

rentedArray byte[]

The array.

length int

How 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.

arrayPool ArrayPool<byte>

The pool the array was rented from, or null for Shared.

Returns

PacketMemory

The memory, over the array's first length bytes.

Exceptions

ArgumentOutOfRangeException

length is 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

segment ArraySegment<byte>

The array segment.

Returns

PacketMemory

implicit operator PacketMemory(byte[])

Converts an array the caller keeps.

public static implicit operator PacketMemory(byte[] array)

Parameters

array byte[]

The array.

Returns

PacketMemory

implicit operator PacketMemory(Memory<byte>)

Converts memory the caller keeps.

public static implicit operator PacketMemory(Memory<byte> memory)

Parameters

memory Memory<byte>

The memory.

Returns

PacketMemory