/// <summary>
        /// Finds the command.
        /// "SEND 0008 xg^89W(v"
        /// Read 10 chars as command name and command data length
        /// </summary>
        /// <param name="session">The session.</param>
        /// <param name="readBuffer">The read buffer.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="length">The length.</param>
        /// <param name="isReusableBuffer">if set to <c>true</c> [is reusable buffer].</param>
        /// <returns></returns>
        public override BinaryCommandInfo FindCommandInfo(IAppSession session, byte[] readBuffer, int offset, int length, bool isReusableBuffer, out int left)
        {
            left = 0;

            int leftLength = 10 - this.BufferSegments.Count;

            if (length < leftLength)
            {
                AddArraySegment(readBuffer, offset, length, isReusableBuffer);
                NextCommandReader = this;
                return(null);
            }

            AddArraySegment(readBuffer, offset, leftLength, isReusableBuffer);

            string commandName       = BufferSegments.Decode(Encoding.ASCII, 0, 4);
            int    commandDataLength = Convert.ToInt32(BufferSegments.Decode(Encoding.ASCII, 5, 4).TrimStart('0'));

            if (length > leftLength)
            {
                int leftDataLength = length - leftLength;
                if (leftDataLength >= commandDataLength)
                {
                    byte[] commandData = readBuffer.CloneRange(offset + leftLength, commandDataLength);
                    BufferSegments.ClearSegements();
                    NextCommandReader = this;
                    var commandInfo = new BinaryCommandInfo(commandName, commandData);

                    //The next commandInfo is comming
                    if (leftDataLength > commandDataLength)
                    {
                        left = leftDataLength - commandDataLength;
                    }

                    return(commandInfo);
                }
                else// if (leftDataLength < commandDataLength)
                {
                    //Clear previous cached header data
                    BufferSegments.ClearSegements();
                    //Save left data part
                    AddArraySegment(readBuffer, offset + leftLength, length - leftLength, isReusableBuffer);
                    NextCommandReader = GetMyCommandDataReader(commandName, commandDataLength);
                    return(null);
                }
            }
            else//length == leftLength
            {
                BufferSegments.ClearSegements();
                NextCommandReader = GetMyCommandDataReader(commandName, commandDataLength);
                return(null);
            }
        }
Exemple #2
0
        public override BinaryRequestInfo Filter(IAppSession <BinaryRequestInfo> session, byte[] readBuffer, int offset, int length, bool toBeCopied, out int left)
        {
            left = 0;

            int leftLength = 6 - this.BufferSegments.Count;

            if (length < leftLength)
            {
                AddArraySegment(readBuffer, offset, length, toBeCopied);
                NextRequestFilter = this;
                return(null);
            }

            AddArraySegment(readBuffer, offset, leftLength, toBeCopied);

            string commandName = BufferSegments.Decode(Encoding.ASCII, 0, 4);

            int commandDataLength = (int)BufferSegments[4] * 256 + (int)BufferSegments[5];

            if (length > leftLength)
            {
                int leftDataLength = length - leftLength;
                if (leftDataLength >= commandDataLength)
                {
                    byte[] commandData = readBuffer.CloneRange(offset + leftLength, commandDataLength);
                    BufferSegments.ClearSegements();
                    NextRequestFilter = this;
                    var requestInfo = new BinaryRequestInfo(commandName, commandData);

                    //The next requestInfo is comming
                    if (leftDataLength > commandDataLength)
                    {
                        left = leftDataLength - commandDataLength;
                    }

                    return(requestInfo);
                }
                else// if (leftDataLength < commandDataLength)
                {
                    //Clear previous cached header data
                    BufferSegments.ClearSegements();
                    //Save left data part
                    AddArraySegment(readBuffer, offset + leftLength, length - leftLength, toBeCopied);
                    NextRequestFilter = GetMyCommandDataReader(commandName, commandDataLength);
                    return(null);
                }
            }
            else
            {
                NextRequestFilter = GetMyCommandDataReader(commandName, commandDataLength);
                return(null);
            }
        }
        public override IWebSocketFragment Filter(IAppSession <IWebSocketFragment> session, byte[] readBuffer, int offset, int length, bool isReusableBuffer, out int left)
        {
            left = 0;

            var skipByteCount = 0;

            if (!m_Type.HasValue)
            {
                byte startByte = readBuffer[offset];
                skipByteCount = 1;
                m_Type        = startByte;
            }

            //0xxxxxxx: Collect protocol data by end mark
            if ((m_Type.Value & 0x80) == 0x00)
            {
                byte lookForByte = 0xFF;

                int i;

                for (i = offset + skipByteCount; i < offset + length; i++)
                {
                    if (readBuffer[i] == lookForByte)
                    {
                        left = length - (i - offset + 1);

                        if (BufferSegments.Count <= 0)
                        {
                            var commandInfo = new PlainFragment(Encoding.UTF8.GetString(readBuffer, offset + skipByteCount, i - offset - skipByteCount));
                            Reset();
                            return(commandInfo);
                        }
                        else
                        {
                            AddArraySegment(readBuffer, offset + skipByteCount, i - offset - skipByteCount, false);
                            var commandInfo = new PlainFragment(BufferSegments.Decode(Encoding.UTF8));
                            Reset();
                            return(commandInfo);
                        }
                    }
                }

                AddArraySegment(readBuffer, offset + skipByteCount, length - skipByteCount, isReusableBuffer);
                return(null);
            }
            else//10000000: Collect protocol data by length
            {
                while (!m_Length.HasValue)
                {
                    if (length <= skipByteCount)
                    {
                        //No data to read
                        return(null);
                    }

                    byte lengthByte = readBuffer[skipByteCount];
                    //Closing handshake
                    if (lengthByte == 0x00 && m_Type.Value == m_ClosingHandshakeType)
                    {
                        session.Close(CloseReason.ClientClosing);
                        return(null);
                    }

                    int thisLength = (int)(lengthByte & 0x7F);
                    m_TempLength = m_TempLength * 128 + thisLength;
                    skipByteCount++;

                    if ((lengthByte & 0x80) != 0x80)
                    {
                        m_Length = m_TempLength;
                        break;
                    }
                }

                int requiredSize = m_Length.Value - BufferSegments.Count;

                int leftSize = length - skipByteCount;

                if (leftSize < requiredSize)
                {
                    AddArraySegment(readBuffer, skipByteCount, length - skipByteCount, isReusableBuffer);
                    return(null);
                }
                else
                {
                    left = leftSize - requiredSize;

                    if (BufferSegments.Count <= 0)
                    {
                        var commandInfo = new PlainFragment(Encoding.UTF8.GetString(readBuffer, offset + skipByteCount, requiredSize));
                        Reset();
                        return(commandInfo);
                    }
                    else
                    {
                        AddArraySegment(readBuffer, offset + skipByteCount, requiredSize, false);
                        var commandInfo = new PlainFragment(BufferSegments.Decode(Encoding.UTF8));
                        Reset();
                        return(commandInfo);
                    }
                }
            }
        }
        public override WebSocketCommandInfo GetCommandInfo(byte[] readBuffer, int offset, int length, out int left)
        {
            left = 0;

            var skipByteCount = 0;

            if (!m_Type.HasValue)
            {
                byte startByte = readBuffer[offset];
                skipByteCount = 1;
                m_Type        = startByte;
            }

            //0xxxxxxx: Collect protocol data by end mark
            if ((m_Type.Value & 0x80) == 0x00)
            {
                byte lookForByte = 0xFF;

                int i;

                for (i = offset + skipByteCount; i < offset + length; i++)
                {
                    if (readBuffer[i] == lookForByte)
                    {
                        left = length - (i - offset + 1);

                        if (BufferSegments.Count <= 0)
                        {
                            var commandInfo = new WebSocketCommandInfo(OpCode.Text.ToString(), Encoding.UTF8.GetString(readBuffer, offset + skipByteCount, i - offset - skipByteCount));
                            Reset(false);
                            return(commandInfo);
                        }
                        else
                        {
                            this.BufferSegments.AddSegment(readBuffer, offset + skipByteCount, i - offset - skipByteCount, false);
                            var commandInfo = new WebSocketCommandInfo(OpCode.Text.ToString(), BufferSegments.Decode(Encoding.UTF8));
                            Reset(true);
                            return(commandInfo);
                        }
                    }
                }

                this.AddArraySegment(readBuffer, offset + skipByteCount, length - skipByteCount);
                return(null);
            }
            else            //10000000: Collect protocol data by length
            {
                while (!m_Length.HasValue)
                {
                    if (length <= skipByteCount)
                    {
                        //No data to read
                        return(null);
                    }

                    byte lengthByte = readBuffer[skipByteCount];
                    //Closing handshake
                    if (lengthByte == 0x00 && m_Type.Value == m_ClosingHandshakeType)
                    {
                        var commandInfo = new WebSocketCommandInfo(OpCode.Close.ToString());
                        Reset(true);
                        return(commandInfo);
                    }

                    int thisLength = (int)(lengthByte & 0x7F);
                    m_TempLength = m_TempLength * 128 + thisLength;
                    skipByteCount++;

                    if ((lengthByte & 0x80) != 0x80)
                    {
                        m_Length = m_TempLength;
                        break;
                    }
                }

                int requiredSize = m_Length.Value - BufferSegments.Count;

                int leftSize = length - skipByteCount;

                if (leftSize < requiredSize)
                {
                    this.AddArraySegment(readBuffer, skipByteCount, length - skipByteCount);
                    return(null);
                }
                else
                {
                    left = leftSize - requiredSize;

                    if (BufferSegments.Count <= 0)
                    {
                        var commandInfo = new WebSocketCommandInfo(OpCode.Text.ToString(), Encoding.UTF8.GetString(readBuffer, offset + skipByteCount, requiredSize));
                        Reset(false);
                        return(commandInfo);
                    }
                    else
                    {
                        this.BufferSegments.AddSegment(readBuffer, offset + skipByteCount, requiredSize, false);
                        var commandInfo = new WebSocketCommandInfo(BufferSegments.Decode(Encoding.UTF8));
                        Reset(true);
                        return(commandInfo);
                    }
                }
            }
        }