Struct PacketProcessor
An API representing a packet acquired from a PacketsQueue, and which allows modifying the packet. The packet is represented as a slice inside a fixed-size buffer, which is why APIs like BytesAvailableAtEnd are offered.
Inherited Members
Namespace: Unity.Networking.Transport
Syntax
public struct PacketProcessor
Properties
BytesAvailableAtEnd
Bytes available in the buffer after the end of the packet.
Declaration
public readonly int BytesAvailableAtEnd { get; }
Property Value
Type | Description |
---|---|
Int32 | Size in bytes. |
BytesAvailableAtStart
Bytes available in the buffer before the start of the packet.
Declaration
public readonly int BytesAvailableAtStart { get; }
Property Value
Type | Description |
---|---|
Int32 | Size in bytes. |
Capacity
Size of the buffer containing the packet.
Declaration
public readonly int Capacity { get; }
Property Value
Type | Description |
---|---|
Int32 | Size in bytes. |
EndpointRef
A reference to the endpoint of the packet. For packets in the receive queue, this is the endpoint from which the packet was received. For packets in the send queue, this is the endpoint the packet is destined to. This must be set appropriately for newly-enqueued packets.
Declaration
public readonly ref NetworkEndpoint EndpointRef { get; }
Property Value
Type | Description |
---|---|
NetworkEndpoint | Endpoint associated with the packet. |
IsCreated
Whether the packet processor was obtained from a valid PacketsQueue.
Declaration
public readonly bool IsCreated { get; }
Property Value
Type | Description |
---|---|
Boolean | True if obtained from a valid queue, false otherwise. |
Length
Size of the packet's data inside the buffer.
Declaration
public readonly int Length { get; }
Property Value
Type | Description |
---|---|
Int32 | Size in bytes. |
Offset
Offset of the packet's first byte inside the buffer.
Declaration
public readonly int Offset { get; }
Property Value
Type | Description |
---|---|
Int32 | Offset in bytes. |
Methods
AppendToPayload(Void*, Int32)
Copy the provided bytes at the end of the packet and increases its size accordingly.
Declaration
public void AppendToPayload(void *dataPtr, int size)
Parameters
Type | Name | Description |
---|---|---|
Void* | dataPtr | Pointer to the data to copy. |
Int32 | size | Size in bytes to copy. |
Exceptions
Type | Condition |
---|---|
ArgumentException | If there are not enough bytes available at the end of the packet. Only thrown when collections checks are enabled (i.e. in the editor). Otherwise an error is logged and nothing is copied. |
AppendToPayload(PacketProcessor)
Append the content of the given packet at the end of this one.
Declaration
public void AppendToPayload(PacketProcessor processor)
Parameters
Type | Name | Description |
---|---|---|
PacketProcessor | processor | Packet processor to copy the data from. |
Exceptions
Type | Condition |
---|---|
ArgumentException | If there are not enough bytes available at the end of the packet. Only thrown when collections checks are enabled (i.e. in the editor). Otherwise an error is logged and nothing is copied. |
AppendToPayload<T>(T)
Copy the provided value at the end of the packet and increase its size accordingly.
Declaration
public void AppendToPayload<T>(T value)
where T : struct
Parameters
Type | Name | Description |
---|---|---|
T | value | Value to copy. |
Type Parameters
Name | Description |
---|---|
T | Type of the data to copy. |
Exceptions
Type | Condition |
---|---|
ArgumentException | If there are not enough bytes available at the end of the packet. Only thrown when collections checks are enabled (i.e. in the editor). Otherwise an error is logged and nothing is copied. |
CopyPayload(Void*, Int32)
Fill the provided buffer with the data at the start of the payload. The copied data will remain in the packet (compare to RemoveFromPayloadStart(Void*, Int32) which removes the data from the packet).
Declaration
public int CopyPayload(void *destinationPtr, int size)
Parameters
Type | Name | Description |
---|---|---|
Void* | destinationPtr | Pointer to the buffer data will be copied to. |
Int32 | size |
Returns
Type | Description |
---|---|
Int32 | Ammount of bytes copied. |
Exceptions
Type | Condition |
---|---|
ArgumentException | If the buffer is larger than the packet. Only thrown when collections checks are enabled (i.e. in the editor). Otherwise an error is logged and nothing is copied. |
Drop()
Drop the packet from its queue by setting its length to 0. Packets with a length of 0 are considered to be dropped from the queue and will be batch-recycled at the end of the update cycle. This is more performant than properly releasing packets one at a time.
Declaration
public void Drop()
GetPayloadDataRef<T>(Int32)
Get a reference to the payload data reinterpreted to the type T.
Declaration
public ref T GetPayloadDataRef<T>(int offset = 0)
where T : struct
Parameters
Type | Name | Description |
---|---|---|
Int32 | offset | Offset from the start of the payload. |
Returns
Type | Description |
---|---|
T | Returns a reference to the payload data |
Type Parameters
Name | Description |
---|---|
T | Type of the data. |
Exceptions
Type | Condition |
---|---|
ArgumentException | If there is not enough bytes in the payload for the specified type. Only thrown when collections checks are enabled (i.e. in the editor). Otherwise the obtained reference may be partially corrupted. |
GetUnsafePayloadPtr()
Get a raw pointer to the packet data.
Declaration
public void *GetUnsafePayloadPtr()
Returns
Type | Description |
---|---|
Void* | A pointer to the packet data. |
PrependToPayload<T>(T)
Copy the provided value at the start of the packet and increase its size accordingly.
Declaration
public void PrependToPayload<T>(T value)
where T : struct
Parameters
Type | Name | Description |
---|---|---|
T | value | Value to copy. |
Type Parameters
Name | Description |
---|---|
T | Type of the data to copy. |
Exceptions
Type | Condition |
---|---|
ArgumentException | If there are not enough bytes available at the start of the packet. Only thrown when collections checks are enabled (i.e. in the editor). Otherwise an error is logged and nothing is copied. |
RemoveFromPayloadStart(Void*, Int32)
Fill the provided buffer with the data at the start of the payload, and remove that data from the packet, decreasing its size accordingly.
Declaration
public void RemoveFromPayloadStart(void *ptr, int size)
Parameters
Type | Name | Description |
---|---|---|
Void* | ptr | Pointer to the start of the buffer to fill. |
Int32 | size | Size of the buffer to fill. |
Exceptions
Type | Condition |
---|---|
ArgumentException | If the buffer is larger than the packet. Only thrown when collections checks are enabled (i.e. in the editor). Otherwise an error is logged and nothing is copied. |
RemoveFromPayloadStart<T>()
Get and remove data at the start of the payload reinterpreted to the type T.
Declaration
public T RemoveFromPayloadStart<T>()
where T : struct
Returns
Type | Description |
---|---|
T | Extracted data value. |
Type Parameters
Name | Description |
---|---|
T | Type of the data. |
Exceptions
Type | Condition |
---|---|
ArgumentException | If there are not enough bytes available at the start of the packet. Only thrown when collections checks are enabled (i.e. in the editor). Otherwise an error is logged and a default value is returned. |