/// <summary> /// Generates a binary representation of this <see cref="RadiusPacket"/> object and copies it into the given buffer. /// </summary> /// <param name="buffer">Buffer used to hold generated binary image of the source object.</param> /// <param name="startIndex">0-based starting index in the <paramref name="buffer"/> to start writing.</param> /// <returns>The number of bytes written to the <paramref name="buffer"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="startIndex"/> or <see cref="ISupportBinaryImage.BinaryLength"/> is less than 0 -or- /// <paramref name="startIndex"/> and <see cref="ISupportBinaryImage.BinaryLength"/> will exceed <paramref name="buffer"/> length. /// </exception> public int GenerateBinaryImage(byte[] buffer, int startIndex) { int length = BinaryLength; buffer.ValidateParameters(startIndex, length); // Populate the buffer buffer[startIndex] = Convert.ToByte(m_type); buffer[startIndex + 1] = m_identifier; Buffer.BlockCopy(EndianOrder.GetBytes((ushort)BinaryLength), 0, buffer, startIndex + 2, 2); Buffer.BlockCopy(m_authenticator, 0, buffer, startIndex + 4, m_authenticator.Length); startIndex += 20; foreach (RadiusPacketAttribute attribute in m_attributes) { if (attribute != null) { startIndex += attribute.GenerateBinaryImage(buffer, startIndex); } } return(length); }
/// <summary> /// Adds header containing the <paramref name="marker"/> to the payload in the <paramref name="buffer"/> for "Payload-Aware" transmission. /// </summary> /// <param name="buffer">The buffer containing the payload.</param> /// <param name="offset">The offset in the <paramref name="buffer"/> at which the payload starts.</param> /// <param name="length">The length of the payload in the <paramref name="buffer"/> starting at the <paramref name="offset"/>.</param> /// <param name="marker">The byte sequence used to mark the beginning of the payload in a "Payload-Aware" transmissions.</param> /// <param name="endianOrder">The endian order to apply to payload size encoding.</param> public static void AddHeader(ref byte[] buffer, ref int offset, ref int length, byte[] marker, EndianOrder endianOrder) { // Note that the resulting buffer will be at least 4 bytes bigger than the payload // Resulting buffer = x bytes for payload marker + 4 bytes for the payload size + The payload byte[] result = new byte[length + marker.Length + LengthSegment]; // First, copy the payload marker to the buffer, if any if (marker.Length > 0) { Buffer.BlockCopy(marker, 0, result, 0, marker.Length); } // Then, copy the payload's size to the buffer after the payload marker Buffer.BlockCopy(endianOrder.GetBytes(length), 0, result, marker.Length, LengthSegment); // At last, copy the payload after the payload marker and payload size Buffer.BlockCopy(buffer, offset, result, marker.Length + LengthSegment, length); buffer = result; offset = 0; length = buffer.Length; }