public void Read(
            ByteBuf byteBuf,
            ref WebSocketReadState state,
            out WebSocketFrameInfo?frameInfo,
            out IWebSocketDecoderStep nextStep)
        {
            frameInfo = null;
            nextStep  = null;

            if (state.PayloadLen == 126)
            {
                if (byteBuf.ReadableBytes() < 2)
                {
                    return;
                }

                state.ExtendedPayloadLen = byteBuf.ReadUShort();
            }
            else if (state.PayloadLen == 127)
            {
                throw new NotSupportedException();
            }

            nextStep = state.Mask
                ? _readMaskingKeyStep
                : _readPayloadDataStep;
        }
        public void Read(
            ByteBuf byteBuf,
            ref WebSocketReadState state,
            out WebSocketFrameInfo?frameInfo,
            out IWebSocketDecoderStep nextStep)
        {
            frameInfo = null;
            nextStep  = null;

            int factPayloadLen = state.ExtendedPayloadLen > 0
                ? (int)state.ExtendedPayloadLen
                : state.PayloadLen;

            if (byteBuf.ReadableBytes() < factPayloadLen)
            {
                return;
            }

            byte[] mask = new byte[4];
            state.MaskingKey.CopyTo(mask, 0);

            frameInfo = new WebSocketFrameInfo(
                Utils.GetFrameType(state.OpCode),
                state.Fin,
                state.Mask,
                mask,
                factPayloadLen
                );
        }
示例#3
0
        public WebSocketDecoderStateMachine()
        {
            var readHeaderStep      = new WebSocketReadHeaderStep();
            var readExtendedLenStep = new WebSocketReadExtendedPayloadLengthStep();
            var readMaskingKeyStep  = new WebSocketReadMaskingKeyStep();
            var readPayloadDataStep = new WebSocketReadPayloadDataStep();

            readHeaderStep.Init(readExtendedLenStep, readMaskingKeyStep, readPayloadDataStep);
            readExtendedLenStep.Init(readMaskingKeyStep, readPayloadDataStep);
            readMaskingKeyStep.Init(readPayloadDataStep);

            _readHeaderStep      = readHeaderStep;
            _readExtendedLenStep = readExtendedLenStep;
            _readMaskingKeyStep  = readMaskingKeyStep;
            _readPayloadDataStep = readPayloadDataStep;

            // TODO: пулинг byte[]
            _state = new WebSocketReadState(
                false /* fin */,
                0 /* opcode */,
                false /* mask */,
                0 /* payloadLen */,
                0 /* extendedPayloadLen */,
                new byte[4] /* maskingKeys */
                );

            _currentStep = _readHeaderStep;
        }
        public void Read(
            ByteBuf byteBuf,
            ref WebSocketReadState state,
            out WebSocketFrameInfo?frameInfo,
            out IWebSocketDecoderStep nextStep)
        {
            frameInfo = null;
            nextStep  = null;

            if (byteBuf.ReadableBytes() < 4)
            {
                return;
            }

            state.MaskingKey[0] = byteBuf.ReadByte();
            state.MaskingKey[1] = byteBuf.ReadByte();
            state.MaskingKey[2] = byteBuf.ReadByte();
            state.MaskingKey[3] = byteBuf.ReadByte();

            nextStep = _readPayloadDataStep;
        }
示例#5
0
        public void Read(
            ByteBuf byteBuf,
            ref WebSocketReadState state,
            out WebSocketFrameInfo?frameInfo,
            out IWebSocketDecoderStep nextStep)
        {
            frameInfo = null;
            nextStep  = null;

            if (byteBuf.ReadableBytes() < 2)
            {
                return;
            }

            state.Clear();

            byte headerByte1 = byteBuf.ReadByte();
            byte headerByte2 = byteBuf.ReadByte();

            state.Fin        = (headerByte1 & Utils.MaskFin) == Utils.MaskFin;
            state.OpCode     = (byte)(headerByte1 & Utils.MaskOpCode);
            state.Mask       = (headerByte2 & Utils.MaskMask) == Utils.MaskMask;
            state.PayloadLen = (byte)(headerByte2 & Utils.MaskPayloadLen);

            if (state.PayloadLen > 125)
            {
                nextStep = _readExtendedLenStep;
            }
            else if (state.Mask)
            {
                nextStep = _readMaskingKeyStep;
            }
            else
            {
                nextStep = _readPayloadDataStep;
            }
        }