示例#1
0
        internal int CountAttribute <T>(RadiusAttributeParser <T> parser)
        {
            // return the number of attribute of the given type
            var offsets = OffsetsFromParser(parser);

            return(offsets.Count);
        }
示例#2
0
        internal void SetAttribute <T>(RadiusAttributeParser <T> parser, int index, T value)
        {
            // check for not read-only
            EnsureWritable();

            // determine offset and old length
            var offset    = OffsetsFromParser(parser)[index];
            var oldLength = buffer[offset + 1];

            // parse value into byte array
            var attribute = parser.Write(value);
            var newLength = attribute.Length + 2;

            if (newLength > byte.MaxValue)
            {
                throw new ArgumentOutOfRangeException("value");
            }

            // prepare buffer if necessary
            if (oldLength != newLength)
            {
                CorrectOffsets(offset + oldLength, newLength - oldLength);
            }

            // write attribute
            buffer[offset]     = parser.Type;
            buffer[offset + 1] = (byte)newLength;
            Array.Copy(attribute, 0, buffer, offset + 2, attribute.Length);
        }
示例#3
0
        internal T GetAttribute <T>(RadiusAttributeParser <T> parser, int index)
        {
            // read attribute
            var offset = OffsetsFromParser(parser)[index];

            return(parser.Read(buffer, offset + 2, buffer[offset + 1] - 2));
        }
示例#4
0
        internal void InsertAttribute <T>(RadiusAttributeParser <T> parser, int index, T item)
        {
            // check for not read-only
            EnsureWritable();

            // determine offset
            var offsets = OffsetsFromParser(parser);
            var offset  = index == offsets.Count ? Length : offsets[index];

            // parse value into a byte array
            var attribute = parser.Write(item);
            var length    = attribute.Length + 2;

            if (length > byte.MaxValue)
            {
                throw new ArgumentOutOfRangeException("item");
            }

            // prepare buffer
            CorrectOffsets(offset, length);

            // write attribute
            buffer[offset]     = parser.Type;
            buffer[offset + 1] = (byte)length;
            Array.Copy(attribute, 0, buffer, offset + 2, length - 2);

            // store offset
            offsets.Insert(index, offset);
        }
示例#5
0
        private List <int> OffsetsFromParser <T>(RadiusAttributeParser <T> parser)
        {
            // return the offsets for a given attribute type
            List <int> offsets;

            if (!attributeOffsets.TryGetValue(parser.Type, out offsets))
            {
                attributeOffsets.Add(parser.Type, offsets = new List <int>());
            }
            return(offsets);
        }
示例#6
0
        internal void RemoveAttribute <T>(RadiusAttributeParser <T> parser, int index)
        {
            // check for not read-only
            EnsureWritable();

            // determine offset and length
            var offsets = OffsetsFromParser(parser);
            var offset  = offsets[index];
            var length  = buffer[offset + 1];

            // remove attribute from buffer
            CorrectOffsets(offset + length, -length);

            // delete offset
            offsets.RemoveAt(index);
        }
示例#7
0
 internal RadiusAttributeList(RadiusPacket packet, RadiusAttributeParser <T> parser)
 {
     this.packet = packet;
     this.parser = parser;
 }
示例#8
0
 /// <summary>
 /// Accesses and modifies attributes within the packet.
 /// </summary>
 /// <typeparam name="T">The value type of <paramref name="parser"/>.</typeparam>
 /// <param name="parser">The attribute parser, either one of <see cref="RadiusAttribute"/> members or a custom one.</param>
 /// <returns>A modifiable list of all attribute values with the same type as <paramref name="parser"/>.</returns>
 public RadiusAttributeList <T> Attribute <T>(RadiusAttributeParser <T> parser)
 {
     return(new RadiusAttributeList <T>(this, parser));
 }