示例#1
0
        /// <summary>
        /// Convert a StunMethodClass to an host-byte ordered unsigned short
        /// </summary>
        /// <param name="mClass">The StunMethodClass to convert</param>
        /// <returns>
        /// The unsigned short (16bits) matching the StunMethodClass
        /// Return max UInt16 value if the mClass parameter is StunMethodClass.Unmanaged
        /// </returns>
        public static UInt16 MethodClassToUInt16(StunMethodClass mClass)
        {
            foreach (FieldInfo field in typeof(StunMethodClass).GetFields())
            {
                if (field.Name == mClass.ToString())
                {
                    Object[] fieldAttributes = field.GetCustomAttributes(typeof(StunValueAttribute), false);

                    if (fieldAttributes.Length == 1)
                    {
                        StunValueAttribute stunValueAttribute = fieldAttributes.GetValue(0) as StunValueAttribute;

                        return(stunValueAttribute.Value);
                    }
                }
            }
            return(0xFFFF);
        }
示例#2
0
        /// <summary>
        /// Convert an host-byte ordered unsigned short to a StunMethodClass
        /// </summary>
        /// <param name="mClass">An unsigned short representing a method class</param>
        /// <returns>
        /// The StunMethodClass matching the unsigned short (16bits)
        /// Returns StunMethodClass.Unmanaged if the unsigned short doesn't match any StunMethodClass StunValue's
        /// </returns>
        public static StunMethodClass UInt16ToMethodClass(UInt16 mClass)
        {
            foreach (FieldInfo field in typeof(StunMethodClass).GetFields())
            {
                Object[] fieldAttributes = field.GetCustomAttributes(typeof(StunValueAttribute), false);

                if (fieldAttributes.Length == 1)
                {
                    StunValueAttribute stunValueAttribute = fieldAttributes.GetValue(0) as StunValueAttribute;

                    if (stunValueAttribute != null &&
                        stunValueAttribute.Value == mClass)
                    {
                        return((StunMethodClass)Enum.Parse(typeof(StunMethodClass), field.Name));
                    }
                }
            }
            return(StunMethodClass.Unmanaged);
        }
示例#3
0
        /// <summary>
        /// Convert a network-byte ordered array of bytes to a StunAttributeType
        /// </summary>
        /// <param name="bytes">An array of 2 bytes (16bits) representing an attribute type</param>
        /// <returns>
        /// The StunAttributeType matching the array of bytes
        /// Returns StunAttributeType.Unmanaged if the byte array doesn't match any StunAttributeType StunValue's
        /// </returns>
        public static StunAttributeType BytesToAttributeType(byte[] bytes)
        {
            UInt16 type = StunUtilities.ReverseBytes(BitConverter.ToUInt16(bytes, 0));

            foreach (FieldInfo field in typeof(StunAttributeType).GetFields())
            {
                Object[] fieldAttributes = field.GetCustomAttributes(typeof(StunValueAttribute), false);

                if (fieldAttributes.Length == 1)
                {
                    StunValueAttribute stunValueAttribute = fieldAttributes.GetValue(0) as StunValueAttribute;

                    if (stunValueAttribute != null &&
                        stunValueAttribute.Value == type)
                    {
                        return((StunAttributeType)Enum.Parse(typeof(StunAttributeType), field.Name));
                    }
                }
            }
            return(StunAttributeType.Unmanaged);
        }
示例#4
0
        /// <summary>
        /// Convert a StunAttributeType to a network-byte ordered array of bytes
        /// </summary>
        /// <param name="type">The StunAttributeType to convert</param>
        /// <returns>
        /// The array of 2 bytes (16bits) matching the StunAttributeType
        /// Returns max UInt16 value if the type parameter is StunAttributeType.Unmanaged
        /// </returns>
        public static byte[] AttributeTypeToBytes(StunAttributeType type)
        {
            foreach (FieldInfo field in typeof(StunAttributeType).GetFields())
            {
                if (field.Name == type.ToString())
                {
                    Object[] fieldAttributes = field.GetCustomAttributes(typeof(StunValueAttribute), false);

                    if (fieldAttributes.Length == 1)
                    {
                        StunValueAttribute stunValueAttribute = fieldAttributes.GetValue(0) as StunValueAttribute;

                        byte[] typeBytes = BitConverter.GetBytes(stunValueAttribute.Value);
                        Array.Reverse(typeBytes);

                        return(typeBytes);
                    }
                }
            }
            return(BitConverter.GetBytes(0xFFFF));
        }