Пример #1
0
        public StreamedVirtualTransportClientHandleResult Handle(int Count)
        {
            var ret = StreamedVirtualTransportClientHandleResult.CreateContinue();

            var Buffer        = c.ReadBuffer.Array;
            var FirstPosition = c.ReadBuffer.Offset;
            var BufferLength  = c.ReadBuffer.Offset + c.ReadBuffer.Count;

            if (Transformer != null)
            {
                Transformer.Inverse(Buffer, BufferLength, Count);
            }
            BufferLength += Count;

            while (true)
            {
                var r = TryShift(c, Buffer, FirstPosition, BufferLength - FirstPosition);
                if (r == null)
                {
                    break;
                }
                FirstPosition = r.Position;

                if (r.Command != null)
                {
                    var CommandName = r.Command.CommandName;
                    var CommandHash = r.Command.CommandHash;
                    var Parameters  = r.Command.Parameters;
                    ret = StreamedVirtualTransportClientHandleResult.CreateCommand(new StreamedVirtualTransportClientHandleResultCommand
                    {
                        CommandName  = CommandName,
                        HandleResult = () => bc.HandleResult(CommandName, CommandHash, Parameters)
                    });
                    break;
                }
            }

            if (BufferLength >= Buffer.Length && FirstPosition > 0)
            {
                var CopyLength = BufferLength - FirstPosition;
                for (int i = 0; i < CopyLength; i += 1)
                {
                    Buffer[i] = Buffer[FirstPosition + i];
                }
                BufferLength  = CopyLength;
                FirstPosition = 0;
            }
            if (FirstPosition >= BufferLength)
            {
                c.ReadBuffer = new ArraySegment <Byte>(Buffer, 0, 0);
            }
            else
            {
                c.ReadBuffer = new ArraySegment <Byte>(Buffer, FirstPosition, BufferLength - FirstPosition);
            }

            return(ret);
        }
Пример #2
0
        public StreamedVirtualTransportClientHandleResult Handle(int Count)
        {
            var ret = StreamedVirtualTransportClientHandleResult.CreateContinue();

            var Buffer        = c.ReadBuffer.Array;
            var FirstPosition = c.ReadBuffer.Offset;
            var BufferLength  = c.ReadBuffer.Offset + c.ReadBuffer.Count;
            var CheckPosition = FirstPosition;

            if (Transformer != null)
            {
                Transformer.Inverse(Buffer, BufferLength, Count);
            }
            BufferLength += Count;

            var LineFeedPosition = -1;

            for (int i = CheckPosition; i < BufferLength; i += 1)
            {
                Byte b = Buffer[i];
                if (b == '\n')
                {
                    LineFeedPosition = i;
                    break;
                }
            }
            if (LineFeedPosition >= FirstPosition)
            {
                var LineBytes = Buffer.Skip(FirstPosition).Take(LineFeedPosition - FirstPosition).Where(b => b != '\r').ToArray();
                var Line      = System.Text.Encoding.UTF8.GetString(LineBytes, 0, LineBytes.Length);
                var cmd       = ParseCommand(Line);
                if (cmd.OnSome)
                {
                    var CommandName = cmd.Some.CommandName;
                    var CommandHash = cmd.Some.CommandHash;
                    var Parameters  = cmd.Some.Parameters;
                    ret = StreamedVirtualTransportClientHandleResult.CreateCommand(new StreamedVirtualTransportClientHandleResultCommand
                    {
                        CommandName  = CommandName,
                        HandleResult = () => jc.HandleResult(CommandName, CommandHash, Parameters)
                    });
                }
                else
                {
                    throw new InvalidOperationException();
                }
                FirstPosition = LineFeedPosition + 1;
                CheckPosition = FirstPosition;
            }

            if (BufferLength >= Buffer.Length && FirstPosition > 0)
            {
                var CopyLength = BufferLength - FirstPosition;
                for (int i = 0; i < CopyLength; i += 1)
                {
                    Buffer[i] = Buffer[FirstPosition + i];
                }
                BufferLength  = CopyLength;
                FirstPosition = 0;
            }
            if (FirstPosition >= BufferLength)
            {
                c.ReadBuffer = new ArraySegment <Byte>(Buffer, 0, 0);
            }
            else
            {
                c.ReadBuffer = new ArraySegment <Byte>(Buffer, FirstPosition, BufferLength - FirstPosition);
            }

            return(ret);
        }