Ejemplo n.º 1
0
        /// <summary>
        /// Convert this object into a byte stream. The zero-th element contains the token length
        /// Subsequent elements contain the token value in network byte order
        /// </summary>
        /// <param name="reserved">Not used now</param>
        /// <returns>byte array</returns>
        public byte[] ToStream(UInt16 reserved)
        {
            byte[] token = new byte[1 + this.Length];
            token[0] = this.Length;

            if (this.Length > 0)
            {
                byte[] tokenValue = new byte[this.Length];
                Array.Copy(this.Value, tokenValue, this.Length);
                tokenValue = AbstractNetworkUtils.ToNetworkByteOrder(tokenValue);
                Array.Copy(tokenValue, 0, token, 1, this.Length);
            }

            return(token);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Convert this object into a byte stream in network byte order
 /// </summary>
 /// <param name="reserved">Not used now</param>
 /// <returns>byte array</returns>
 public byte[] ToStream(UInt16 reserved)
 {
     byte[] mID = AbstractByteUtils.GetBytes(this.Value);
     mID = AbstractNetworkUtils.ToNetworkByteOrder(mID);
     return(mID);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Convert this option into a byte stream int network byte order
        /// </summary>
        /// <param name="previousOptionNumber">The previous option number in the sequence of options</param>
        /// <returns>byte array</returns>
        public byte[] ToStream(UInt16 previousOptionNumber)
        {
            //The option format is given below

            /*********************************************
            | 4 bits option delta | 4 bits length        |
            |  ----------------------------------------------
            |      0-2 bytes option delta extended       |
            |  ----------------------------------------------
            |      0-2 bytes option length extended      |
            |  ----------------------------------------------
            |               0-* bytes option value       |
            *********************************************/
            int streamLength = 1;
            //Get Option delta
            byte   optionDelta         = 0;
            UInt16 derivedOptionNumber = (UInt16)(this.Number - previousOptionNumber);

            byte[] optionDeltaEx = null;
            if (derivedOptionNumber <= 12)
            {
                optionDelta = (byte)derivedOptionNumber;
            }
            else if (derivedOptionNumber > 12 && derivedOptionNumber <= 255 /*Option number is single byte*/)
            {
                optionDeltaEx = new byte[1] {
                    (byte)(derivedOptionNumber - 13)
                };
                optionDelta = 13;
                streamLength++;//1 additional byte
            }
            else /*Option number is double byte*/
            {
                optionDelta   = 14;
                optionDeltaEx = AbstractByteUtils.GetBytes((UInt16)(derivedOptionNumber - 269));
                optionDeltaEx = AbstractNetworkUtils.ToNetworkByteOrder(optionDeltaEx);
                streamLength += 2; //two additional bytes
            }
            //Get option length
            byte optionLength = 0;

            byte[] optionLengthEx = null;
            if (this.Value != null)
            {
                if (this.Value.Length > 0 && this.Value.Length < 13)
                {
                    optionLength = (byte)this.Value.Length;
                }
                else if (this.Value.Length > 12 && this.Value.Length <= 255)
                {
                    optionLength   = 13;
                    optionLengthEx = new byte[1] {
                        (byte)(this.Value.Length - 13)
                    };
                    streamLength++;//1 additional byte
                }
                else if (this.Value.Length > 255)
                {
                    optionLength   = 14;
                    optionLengthEx = AbstractByteUtils.GetBytes((UInt16)(this.Value.Length - 269));
                    optionLengthEx = AbstractNetworkUtils.ToNetworkByteOrder(optionLengthEx);
                    streamLength  += 2; //two additional bytes
                }
            }
            streamLength += ((this.Value != null && !this.IsEmpty()) ? this.Value.Length : 0);
            byte[] optionStream = new byte[streamLength];
            int    count        = 0;

            optionStream[count++] = (byte)((optionDelta << 4) | (optionLength)); //header + length
            if (optionDelta == 13)                                               //delta extended
            {
                optionStream[count++] = optionDeltaEx[0];
            }
            else if (optionDelta == 14)
            {
                optionStream[count++] = optionDeltaEx[0];
                optionStream[count++] = optionDeltaEx[1];
            }
            if (optionLength == 13)
            {
                optionStream[count++] = optionLengthEx[0];
            }
            else if (optionLength == 14)
            {
                optionStream[count++] = optionLengthEx[0];
                optionStream[count++] = optionLengthEx[1];
            }
            if (this.Value != null && this.Value.Length > 0 && !this.IsEmpty())
            {
                byte[] optionValue = new byte[this.Value.Length];
                Array.Copy(this.Value, optionValue, this.Value.Length);
                if (this.NeedsByteOrdering(this.Number))
                {
                    optionValue = AbstractNetworkUtils.ToNetworkByteOrder(optionValue);
                }
                Array.Copy(optionValue, 0, optionStream, count, optionValue.Length);
            }

            return(optionStream);
        }