Noble Connect
Add relays and punchthrough to Mirror or UNet
NobleConnect.Stun.AttributeData Class Reference

Any Attribute that holds an immutable view of a byte array More...

Inherits NobleConnect.Stun.Attribute.

Public Member Functions

 AttributeData (AttributeType attributeType, ArraySegment< byte > payload)
 Construct an AttributeData of the provided AttributeType using the provided ArraySegment of bytes as the payload. More...
 
 AttributeData (AttributeType type, byte[] buffer, ref int offset)
 Construct an AttributeData from the provided byte array More...
 
override void SerializeValues (BufferView buffer, ReadOnlyByteArray transactionID)
 Serialize the payload to the provided buffer. More...
 
void SendTo (Socket socket, EndPoint endPoint)
 Send the payload over the provided socket to the provided end point More...
 
byte[] AsArray ()
 Get a copy of the payload as a byte array More...
 
override ushort GetValueSize ()
 Get the serialized size of the attribute values More...
 
- Public Member Functions inherited from NobleConnect.Stun.Attribute
 Attribute (AttributeType type)
 Construct an Attribute of the provided AttributeType More...
 
 Attribute (AttributeType type, ref int offset)
 
void Serialize (BufferView buffer, ReadOnlyByteArray transactionID)
 Convert this attribute to a bunch of bytes and add them to the provided buffer More...
 
ushort GetSize ()
 

Public Attributes

readonly ImmutableArraySegment< byte > payload
 This is a thread-safe, immutable view into a byte array. More...
 
- Public Attributes inherited from NobleConnect.Stun.Attribute
readonly AttributeType attributeType
 The AttributeType of this Attribute More...
 

Additional Inherited Members

- Static Public Member Functions inherited from NobleConnect.Stun.Attribute
static ushort DeserializeLength (byte[] buffer, ref int offset)
 Read the Attribute length from a byte array. More...
 
static ushort GetNumPaddingBytes (int length)
 Calculate padding based on length More...
 
- Static Public Attributes inherited from NobleConnect.Stun.Attribute
const int HEADER_SIZE = 4
 

Detailed Description

Any Attribute that holds an immutable view of a byte array

This Attribute has been carefully designed to hold an immutable view of a byte array. The backing array can not be accessed directly, as this would make it unsafe to share this Attribute across threads. You can however get a copy of the byte data using AsArray().

Methods are also provided to serialize the backing array to a Buffer or send it out over a socket.

The unsafe ArraySegment is kept around internally for performance reasons. For some applications it is useful to be able to receive a Data message from the Turn server and immediately send it back out over another socket. The design of this class allows for the incoming bytes from one socket to be passed directly out over another with no copying, while still maintaining thread-safety by being immutable.

Constructor & Destructor Documentation

◆ AttributeData() [1/2]

NobleConnect.Stun.AttributeData.AttributeData ( AttributeType  attributeType,
ArraySegment< byte >  payload 
)

Construct an AttributeData of the provided AttributeType using the provided ArraySegment of bytes as the payload.

This is called when constructing an Attribute to send to a Stun server.

Parameters
attributeType
value

◆ AttributeData() [2/2]

NobleConnect.Stun.AttributeData.AttributeData ( AttributeType  type,
byte[]  buffer,
ref int  offset 
)

Construct an AttributeData from the provided byte array

The first 2 bytes of the byte array should contain the length of the payload. This is called when deserializing an AttributeData from a Stun server.

Parameters
type
buffer
offset

Member Function Documentation

◆ AsArray()

byte [] NobleConnect.Stun.AttributeData.AsArray ( )

Get a copy of the payload as a byte array

Returns

◆ GetValueSize()

override ushort NobleConnect.Stun.AttributeData.GetValueSize ( )
virtual

Get the serialized size of the attribute values

Returns
The size of the payload in bytes

Reimplemented from NobleConnect.Stun.Attribute.

◆ SendTo()

void NobleConnect.Stun.AttributeData.SendTo ( Socket  socket,
EndPoint  endPoint 
)

Send the payload over the provided socket to the provided end point

Parameters
socket
endPoint

◆ SerializeValues()

override void NobleConnect.Stun.AttributeData.SerializeValues ( BufferView  buffer,
ReadOnlyByteArray  transactionID 
)
virtual

Serialize the payload to the provided buffer.

This is safe because there is no way to do anything with the data on the buffer other than read it.

Reimplemented from NobleConnect.Stun.Attribute.

Member Data Documentation

◆ payload

readonly ImmutableArraySegment<byte> NobleConnect.Stun.AttributeData.payload

This is a thread-safe, immutable view into a byte array.