/// <summary>
        /// Call from socket receiver and create a message from this.
        /// </summary>
        /// <param name="receive"></param>
        internal async Task <bool> ReadBytesAndBuildMessage(int receive)
        {
            var processing = true;

            while (processing)
            {
                // Log( "ReadBytesAndBuildMessage:" + receive.ToString());
                if (_state.Flag == 0)
                {
                    DeconstructHeaderField(_state.Buffer[0]);
                    var length = CalculateHeaderFieldsLength();
                    if (_state.Buffer.Length < length)
                    {
                        return(true);
                    }

                    byte headerFieldByte = _state.Buffer[0];
                    DeconstructHeaderFields(headerFieldByte, _state.Buffer);
                    byte[] remainingBytes = new byte[_state.Buffer.Length - length];
                    receive -= length;

                    Array.Copy(_state.Buffer, length, remainingBytes, 0, remainingBytes.Length);
                    _state.ChangeBuffer(remainingBytes);
                    _state.Flag++;
                }

                if (_state.Buffer.Length < 2)
                {
                    processing = false;
                }


                if (_state.Flag == 1)
                {
                    var enoughBytes = DeconstructHeader();
                    if (!enoughBytes || _state.Flag != 2)
                    {
                        processing = false;
                    }
                    receive -= _headerLength;
                }

                if (_state.Flag == 2)
                {
                    processing = await ReadData(receive);

                    if (processing)
                    {
                        receive = _state.Buffer.Length;
                    }
                }
            }

            return(true);
        }
Example #2
0
        /// <summary>
        /// Call from socket receiver and create a message from this.
        /// </summary>
        /// <param name="receive"></param>
        internal bool ReadBytesAndBuildMessage(int receive)
        {
            var processing = true;

            while (processing)
            {
                if (_state.Flag == MessageFlag.Idle)
                {
                    DeconstructHeaderField(_state.Buffer[0]);
                    var length = CalculateHeaderFieldsLength();
                    if (_state.Buffer.Length < length)
                    {
                        return(true);
                    }

                    byte headerFieldByte = _state.Buffer[0];
                    DeconstructHeaderFields(headerFieldByte, _state.Buffer);
                    byte[] remainingBytes = new byte[_state.Buffer.Length - length];
                    receive -= length;

                    Array.Copy(_state.Buffer, length, remainingBytes, 0, remainingBytes.Length);
                    _state.ChangeBuffer(remainingBytes);
                    _state.Flag = MessageFlag.ProcessingHeader;
                }

                if (_state.Buffer.Length < 2)
                {
                    processing = false;
                }


                if (_state.Flag == MessageFlag.ProcessingHeader)
                {
                    var enoughBytes = DeconstructHeader();
                    if (!enoughBytes || _state.Flag != MessageFlag.ProcessingData || _state.Flag != MessageFlag.ProcessingMetadata)
                    {
                        processing = false;
                    }
                    receive -= _headerLength;
                }

                if (_state.Flag == MessageFlag.ProcessingMetadata)
                {
                    processing = ReadMetaData(receive);
                    receive    = _state.Buffer.Length;
                }

                if (_state.Flag == MessageFlag.ProcessingData)
                {
                    processing = ReadData(receive);
                    receive    = _state.Buffer.Length;
                }
            }

            return(true);
        }
Example #3
0
        /// <summary>
        /// Call from socket receiver and create a message from this.
        /// </summary>
        /// <param name="receive"></param>
        internal async Task <bool> ReadBytesAndBuildMessage(int receive)
        {
            if (_state.Flag == 0)
            {
                DeconstructHeaderField(_state.Buffer[0]);
                var length = CalculateHeaderFieldsLength();
                if (_state.Buffer.Length < length)
                {
                    return(true);
                }

                byte headerFieldByte = _state.Buffer[0];
                DeconstructHeaderFields(headerFieldByte, _state.Buffer);
                byte[] remainingBytes = new byte[_state.Buffer.Length - length];
                receive -= length;

                Array.Copy(_state.Buffer, length, remainingBytes, 0, remainingBytes.Length);
                _state.ChangeBuffer(remainingBytes);
                _state.Flag++;
            }

            if (_state.Buffer.Length < 1)
            {
                return(true);
            }


            if (_state.Flag == 1)
            {
                var enoughBytes = DeconstructHeader();
                if (!enoughBytes)
                {
                    return(true);
                }
                receive -= _headerLength;
            }

            if (_state.Flag == 2)
            {
                return(await ReadData(receive));
            }

            return(true);
        }