public byte[] ToByteBuffer()
{
UInt16 attributesLength = 0;
foreach (STUNAttribute attribute in Attributes)
{
attributesLength += Convert.ToUInt16(STUNAttribute.STUNATTRIBUTE_HEADER_LENGTH + attribute.Length);
}
int messageLength = STUNHeader.STUN_HEADER_LENGTH + attributesLength;
byte[] buffer = new byte[messageLength];
if (BitConverter.IsLittleEndian)
{
Buffer.BlockCopy(BitConverter.GetBytes(Utility.ReverseEndian((UInt16)Header.MessageType)), 0, buffer, 0, 2);
Buffer.BlockCopy(BitConverter.GetBytes(Utility.ReverseEndian(attributesLength)), 0, buffer, 2, 2);
}
else
{
Buffer.BlockCopy(BitConverter.GetBytes((UInt16)Header.MessageType), 0, buffer, 0, 2);
Buffer.BlockCopy(BitConverter.GetBytes(attributesLength), 0, buffer, 2, 2);
}
Buffer.BlockCopy(Header.TransactionId, 0, buffer, 4, STUNHeader.TRANSACTION_ID_LENGTH);
int attributeIndex = 20;
foreach (STUNAttribute attr in Attributes)
{
attributeIndex += attr.ToByteBuffer(buffer, attributeIndex);
}
return buffer;
}