Exemple #1
0
        /// <summary>
        /// Processes the incoming <see cref="byte"/> array to see if a <see cref="PacketHeader"/> is within it.
        /// </summary>
        /// <param name="data">The data to check for a <see cref="PacketHeader"/> - may be incomplete and the rest stored in the buffer.</param>
        /// <param name="buffer">A <see cref="ByteBufferCollection"/> that potentially has stored part of a <see cref="PacketHeader"/>.</param>
        /// <returns>true, if the method encountered no error; otherwise false.</returns>
        /// <remarks>Just because the method returns true, does not mean it found the <see cref="PacketHeader"/> - just that there were no errors.</remarks>
        public bool ProcessHeader(ref byte[] data, ref ByteBufferCollection buffer)
        {
            if (!HasDelimiter)
            {
                Delimiter = ArrayMethods.Shrink(ref data, 1)[0];
                if (data == null || data.Length == 0) return true;
            }

            int delimiterIndex = Array.IndexOf(data, Delimiter);
            if (delimiterIndex == -1)
            {
                buffer.Add(data);
                return true;
            }

            byte[] headerData = buffer.Combine(ArrayMethods.Shrink(ref data, delimiterIndex + 1));
            Array.Resize(ref headerData, headerData.Length - 1);
            buffer.Clear();

            string size = Encoding.ASCII.GetString(headerData);
            if (!int.TryParse(size, out _size))
            {
                Reset();
                return false;
            }

            if (Size == 0)
                Reset();
            return true;
        }
Exemple #2
0
        /// <summary>
        /// Processes the incoming <see cref="byte"/> array to see if a <see cref="PacketHeader"/> is within it.
        /// </summary>
        /// <param name="data">The data to check for a <see cref="PacketHeader"/> - may be incomplete and the rest stored in the buffer.</param>
        /// <param name="buffer">A <see cref="ByteBufferCollection"/> that potentially has stored part of a <see cref="PacketHeader"/>.</param>
        /// <returns>true, if the method encountered no error; otherwise false.</returns>
        /// <remarks>Just because the method returns true, does not mean it found the <see cref="PacketHeader"/> - just that there were no errors.</remarks>
        public bool ProcessHeader(ref byte[] data, ref ByteBufferCollection buffer)
        {
            if (!HasDelimiter)
            {
                Delimiter = ArrayMethods.Shrink(ref data, 1)[0];
                if (data == null || data.Length == 0)
                {
                    return(true);
                }
            }

            int delimiterIndex = Array.IndexOf(data, Delimiter);

            if (delimiterIndex == -1)
            {
                buffer.Add(data);
                return(true);
            }

            byte[] headerData = buffer.Combine(ArrayMethods.Shrink(ref data, delimiterIndex + 1));
            Array.Resize(ref headerData, headerData.Length - 1);
            buffer.Clear();

            string size = Encoding.ASCII.GetString(headerData);

            if (!int.TryParse(size, out _size))
            {
                Reset();
                return(false);
            }

            if (Size == 0)
            {
                Reset();
            }
            return(true);
        }