Ejemplo n.º 1
0
        public static void DecodeFramingFault(ClientFramingDecoder decoder, IConnection connection, Uri via, string contentType, ref TimeoutHelper timeoutHelper)
        {
            ValidateReadingFaultString(decoder);
            int offset = 0;

            byte[] buffer = DiagnosticUtility.Utility.AllocateByteArray(0x100);
            int    size   = connection.Read(buffer, offset, buffer.Length, timeoutHelper.RemainingTime());

            while (size > 0)
            {
                int num3 = decoder.Decode(buffer, offset, size);
                offset += num3;
                size   -= num3;
                if (decoder.CurrentState == ClientFramingDecoderState.Fault)
                {
                    ConnectionUtilities.CloseNoThrow(connection, timeoutHelper.RemainingTime());
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(FaultStringDecoder.GetFaultException(decoder.Fault, via.ToString(), contentType));
                }
                if (decoder.CurrentState != ClientFramingDecoderState.ReadingFaultString)
                {
                    throw Fx.AssertAndThrow("invalid framing client state machine");
                }
                if (size == 0)
                {
                    offset = 0;
                    size   = connection.Read(buffer, offset, buffer.Length, timeoutHelper.RemainingTime());
                }
            }
            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(decoder.CreatePrematureEOFException());
        }
Ejemplo n.º 2
0
            private void CompleteReadFaultData()
            {
                int offset = 0;
                int size   = this.connection.EndRead();

                while (size > 0)
                {
                    int num3 = this.decoder.Decode(this.connection.AsyncReadBuffer, offset, size);
                    offset += num3;
                    size   -= num3;
                    if (this.decoder.CurrentState == ClientFramingDecoderState.Fault)
                    {
                        ConnectionUtilities.CloseNoThrow(this.connection, this.timeoutHelper.RemainingTime());
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(FaultStringDecoder.GetFaultException(this.decoder.Fault, this.via.ToString(), this.contentType));
                    }
                    if (this.decoder.CurrentState != ClientFramingDecoderState.ReadingFaultString)
                    {
                        throw Fx.AssertAndThrow("invalid framing client state machine");
                    }
                    if (size == 0)
                    {
                        offset = 0;
                        if (this.connection.BeginRead(0, Math.Min(0x100, this.connection.AsyncReadBufferSize), this.timeoutHelper.RemainingTime(), onReadFaultData, this) == AsyncReadResult.Queued)
                        {
                            return;
                        }
                        size = this.connection.EndRead();
                    }
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(this.decoder.CreatePrematureEOFException());
            }
Ejemplo n.º 3
0
        public static async Task DecodeFramingFaultAsync(ClientFramingDecoder decoder, IConnection connection,
                                                         Uri via, string contentType, TimeSpan timeout)
        {
            var timeoutHelper = new TimeoutHelper(timeout);

            ValidateReadingFaultString(decoder);

            var tcs    = new TaskCompletionSource <bool>();
            var result = connection.BeginRead(0, Math.Min(FaultStringDecoder.FaultSizeQuota, connection.AsyncReadBufferSize),
                                              timeoutHelper.RemainingTime(), FramingDuplexSessionChannel.OnIoComplete, tcs);

            if (result == AsyncCompletionResult.Completed)
            {
                tcs.TrySetResult(true);
            }

            await tcs.Task;

            int offset = 0;
            int size   = connection.EndRead();

            while (size > 0)
            {
                int bytesDecoded = decoder.Decode(connection.AsyncReadBuffer, offset, size);
                offset += bytesDecoded;
                size   -= bytesDecoded;

                if (decoder.CurrentState == ClientFramingDecoderState.Fault)
                {
                    ConnectionUtilities.CloseNoThrow(connection, timeoutHelper.RemainingTime());
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                              FaultStringDecoder.GetFaultException(decoder.Fault, via.ToString(), contentType));
                }
                else
                {
                    if (decoder.CurrentState != ClientFramingDecoderState.ReadingFaultString)
                    {
                        throw new Exception("invalid framing client state machine");
                    }
                    if (size == 0)
                    {
                        offset = 0;
                        tcs    = new TaskCompletionSource <bool>();
                        result = connection.BeginRead(0, Math.Min(FaultStringDecoder.FaultSizeQuota, connection.AsyncReadBufferSize),
                                                      timeoutHelper.RemainingTime(), FramingDuplexSessionChannel.OnIoComplete, tcs);
                        if (result == AsyncCompletionResult.Completed)
                        {
                            tcs.TrySetResult(true);
                        }

                        await tcs.Task;
                        size = connection.EndRead();
                    }
                }
            }

            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(decoder.CreatePrematureEOFException());
        }
Ejemplo n.º 4
0
        public static async Task DecodeFramingFaultAsync(ClientFramingDecoder decoder, IConnection connection,
                                                         Uri via, string contentType, TimeSpan timeout)
        {
            var timeoutHelper = new TimeoutHelper(timeout);

            ValidateReadingFaultString(decoder);

            int size = await connection.ReadAsync(0,
                                                  Math.Min(FaultStringDecoder.FaultSizeQuota, connection.AsyncReadBufferSize),
                                                  timeoutHelper.RemainingTime());

            int offset = 0;

            while (size > 0)
            {
                int bytesDecoded = decoder.Decode(connection.AsyncReadBuffer, offset, size);
                offset += bytesDecoded;
                size   -= bytesDecoded;

                if (decoder.CurrentState == ClientFramingDecoderState.Fault)
                {
                    ConnectionUtilities.CloseNoThrow(connection, timeoutHelper.RemainingTime());
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                              FaultStringDecoder.GetFaultException(decoder.Fault, via.ToString(), contentType));
                }
                else
                {
                    if (decoder.CurrentState != ClientFramingDecoderState.ReadingFaultString)
                    {
                        throw new Exception("invalid framing client state machine");
                    }
                    if (size == 0)
                    {
                        offset = 0;
                        size   = await connection.ReadAsync(0,
                                                            Math.Min(FaultStringDecoder.FaultSizeQuota, connection.AsyncReadBufferSize),
                                                            timeoutHelper.RemainingTime());
                    }
                }
            }

            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(decoder.CreatePrematureEOFException());
        }
Ejemplo n.º 5
0
        public override int Decode(byte[] bytes, int offset, int size)
        {
            DecoderHelper.ValidateSize(size);

            try
            {
                int bytesConsumed;
                FramingRecordType recordType;
                switch (CurrentState)
                {
                case ClientFramingDecoderState.ReadingUpgradeRecord:
                    recordType = (FramingRecordType)bytes[offset];
                    if (recordType == FramingRecordType.UpgradeResponse)
                    {
                        bytesConsumed     = 1;
                        base.CurrentState = ClientFramingDecoderState.UpgradeResponse;
                    }
                    else
                    {
                        bytesConsumed     = 0;
                        base.CurrentState = ClientFramingDecoderState.ReadingAckRecord;
                    }
                    break;

                case ClientFramingDecoderState.UpgradeResponse:
                    bytesConsumed     = 0;
                    base.CurrentState = ClientFramingDecoderState.ReadingUpgradeRecord;
                    break;

                case ClientFramingDecoderState.ReadingAckRecord:
                    recordType = (FramingRecordType)bytes[offset];
                    if (recordType == FramingRecordType.Fault)
                    {
                        bytesConsumed     = 1;
                        _faultDecoder     = new FaultStringDecoder();
                        base.CurrentState = ClientFramingDecoderState.ReadingFaultString;
                        break;
                    }
                    ValidatePreambleAck(recordType);
                    bytesConsumed     = 1;
                    base.CurrentState = ClientFramingDecoderState.Start;
                    break;

                case ClientFramingDecoderState.Start:
                    bytesConsumed     = 0;
                    base.CurrentState = ClientFramingDecoderState.ReadingEnvelopeRecord;
                    break;

                case ClientFramingDecoderState.ReadingEnvelopeRecord:
                    recordType = (FramingRecordType)bytes[offset];
                    if (recordType == FramingRecordType.End)
                    {
                        bytesConsumed     = 1;
                        base.CurrentState = ClientFramingDecoderState.End;
                        break;
                    }
                    else if (recordType == FramingRecordType.Fault)
                    {
                        bytesConsumed     = 0;
                        base.CurrentState = ClientFramingDecoderState.ReadingFault;
                        break;
                    }
                    ValidateRecordType(FramingRecordType.UnsizedEnvelope, recordType);
                    bytesConsumed     = 1;
                    base.CurrentState = ClientFramingDecoderState.EnvelopeStart;
                    break;

                case ClientFramingDecoderState.EnvelopeStart:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                              CreateException(new InvalidDataException(SRServiceModel.FramingAtEnd)));

                case ClientFramingDecoderState.ReadingFault:
                    recordType = (FramingRecordType)bytes[offset];
                    ValidateRecordType(FramingRecordType.Fault, recordType);
                    bytesConsumed     = 1;
                    _faultDecoder     = new FaultStringDecoder();
                    base.CurrentState = ClientFramingDecoderState.ReadingFaultString;
                    break;

                case ClientFramingDecoderState.ReadingFaultString:
                    bytesConsumed = _faultDecoder.Decode(bytes, offset, size);
                    if (_faultDecoder.IsValueDecoded)
                    {
                        base.CurrentState = ClientFramingDecoderState.Fault;
                    }
                    break;

                case ClientFramingDecoderState.Fault:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                              CreateException(new InvalidDataException(SRServiceModel.FramingAtEnd)));

                default:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                              CreateException(new InvalidDataException(SRServiceModel.InvalidDecoderStateMachine)));
                }

                StreamPosition += bytesConsumed;
                return(bytesConsumed);
            }
            catch (InvalidDataException e)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateException(e));
            }
        }
        protected override Message DecodeMessage(byte[] buffer, ref int offset, ref int size, ref bool isAtEOF, TimeSpan timeout)
        {
            while (size > 0)
            {
                int envelopeSize;
                int count = this.decoder.Decode(buffer, offset, size);
                if (count > 0)
                {
                    if (base.EnvelopeBuffer != null)
                    {
                        if (!object.ReferenceEquals(buffer, base.EnvelopeBuffer))
                        {
                            Buffer.BlockCopy(buffer, offset, base.EnvelopeBuffer, base.EnvelopeOffset, count);
                        }
                        base.EnvelopeOffset += count;
                    }
                    offset += count;
                    size   -= count;
                }
                switch (this.decoder.CurrentState)
                {
                case ClientFramingDecoderState.EnvelopeStart:
                    envelopeSize = this.decoder.EnvelopeSize;
                    if (envelopeSize > this.maxBufferSize)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException((long)this.maxBufferSize));
                    }
                    break;

                case ClientFramingDecoderState.ReadingEnvelopeBytes:
                case ClientFramingDecoderState.ReadingEndRecord:
                {
                    continue;
                }

                case ClientFramingDecoderState.EnvelopeEnd:
                {
                    if (base.EnvelopeBuffer == null)
                    {
                        continue;
                    }
                    Message message = null;
                    try
                    {
                        using (CreateProcessActionActivity())
                        {
                            message = this.messageEncoder.ReadMessage(new ArraySegment <byte>(base.EnvelopeBuffer, 0, base.EnvelopeSize), this.bufferManager);
                            if (DiagnosticUtility.ShouldUseActivity)
                            {
                                TraceUtility.TransferFromTransport(message);
                            }
                        }
                    }
                    catch (XmlException exception)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("MessageXmlProtocolError"), exception));
                    }
                    base.EnvelopeBuffer = null;
                    return(message);
                }

                case ClientFramingDecoderState.End:
                    isAtEOF = true;
                    return(null);

                case ClientFramingDecoderState.Fault:
                    this.channel.Session.CloseOutputSession(this.channel.InternalCloseTimeout);
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(FaultStringDecoder.GetFaultException(this.decoder.Fault, this.channel.RemoteAddress.Uri.ToString(), this.messageEncoder.ContentType));

                default:
                {
                    continue;
                }
                }
                base.EnvelopeBuffer = this.bufferManager.TakeBuffer(envelopeSize);
                base.EnvelopeOffset = 0;
                base.EnvelopeSize   = envelopeSize;
            }
            return(null);
        }
Ejemplo n.º 7
0
        protected override Message DecodeMessage(byte[] buffer, ref int offset, ref int size, ref bool isAtEOF, TimeSpan timeout)
        {
            while (size > 0)
            {
                int bytesRead = _decoder.Decode(buffer, offset, size);
                if (bytesRead > 0)
                {
                    if (EnvelopeBuffer != null)
                    {
                        if (!object.ReferenceEquals(buffer, EnvelopeBuffer))
                        {
                            System.Buffer.BlockCopy(buffer, offset, EnvelopeBuffer, EnvelopeOffset, bytesRead);
                        }

                        EnvelopeOffset += bytesRead;
                    }

                    offset += bytesRead;
                    size   -= bytesRead;
                }

                switch (_decoder.CurrentState)
                {
                case ClientFramingDecoderState.Fault:
                    _channel.Session.CloseOutputSession(_channel.GetInternalCloseTimeout());
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(FaultStringDecoder.GetFaultException(_decoder.Fault, _channel.RemoteAddress.Uri.ToString(), _messageEncoder.ContentType));

                case ClientFramingDecoderState.End:
                    isAtEOF = true;
                    return(null);    // we're done

                case ClientFramingDecoderState.EnvelopeStart:
                    int envelopeSize = _decoder.EnvelopeSize;
                    if (envelopeSize > _maxBufferSize)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                                  ExceptionHelper.CreateMaxReceivedMessageSizeExceededException(_maxBufferSize));
                    }
                    EnvelopeBuffer = _bufferManager.TakeBuffer(envelopeSize);
                    EnvelopeOffset = 0;
                    EnvelopeSize   = envelopeSize;
                    break;

                case ClientFramingDecoderState.EnvelopeEnd:
                    if (EnvelopeBuffer != null)
                    {
                        Message message = null;
                        try
                        {
                            IDisposable activity = ClientDuplexConnectionReader.CreateProcessActionActivity();
                            using (activity)
                            {
                                message = _messageEncoder.ReadMessage(new ArraySegment <byte>(EnvelopeBuffer, 0, EnvelopeSize), _bufferManager);
                            }
                        }
                        catch (XmlException xmlException)
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                                      new ProtocolException(SR.MessageXmlProtocolError, xmlException));
                        }
                        EnvelopeBuffer = null;
                        return(message);
                    }
                    break;
                }
            }
            return(null);
        }
Ejemplo n.º 8
0
        public override int Decode(byte[] bytes, int offset, int size)
        {
            DecoderHelper.ValidateSize(size);

            try
            {
                int bytesConsumed;
                FramingRecordType recordType;
                switch (CurrentState)
                {
                    case ClientFramingDecoderState.ReadingUpgradeRecord:
                        recordType = (FramingRecordType)bytes[offset];
                        if (recordType == FramingRecordType.UpgradeResponse)
                        {
                            bytesConsumed = 1;
                            base.CurrentState = ClientFramingDecoderState.UpgradeResponse;
                        }
                        else
                        {
                            bytesConsumed = 0;
                            base.CurrentState = ClientFramingDecoderState.ReadingAckRecord;
                        }
                        break;
                    case ClientFramingDecoderState.UpgradeResponse:
                        bytesConsumed = 0;
                        base.CurrentState = ClientFramingDecoderState.ReadingUpgradeRecord;
                        break;
                    case ClientFramingDecoderState.ReadingAckRecord:
                        recordType = (FramingRecordType)bytes[offset];
                        if (recordType == FramingRecordType.Fault)
                        {
                            bytesConsumed = 1;
                            faultDecoder = new FaultStringDecoder();
                            base.CurrentState = ClientFramingDecoderState.ReadingFaultString;
                            break;
                        }
                        ValidatePreambleAck(recordType);
                        bytesConsumed = 1;
                        base.CurrentState = ClientFramingDecoderState.Start;
                        break;

                    case ClientFramingDecoderState.Start:
                        bytesConsumed = 0;
                        base.CurrentState = ClientFramingDecoderState.ReadingEnvelopeRecord;
                        break;

                    case ClientFramingDecoderState.ReadingEnvelopeRecord:
                        recordType = (FramingRecordType)bytes[offset];
                        if (recordType == FramingRecordType.End)
                        {
                            bytesConsumed = 1;
                            base.CurrentState = ClientFramingDecoderState.End;
                            break;
                        }
                        else if (recordType == FramingRecordType.Fault)
                        {
                            bytesConsumed = 0;
                            base.CurrentState = ClientFramingDecoderState.ReadingFault;
                            break;
                        }
                        ValidateRecordType(FramingRecordType.UnsizedEnvelope, recordType);
                        bytesConsumed = 1;
                        base.CurrentState = ClientFramingDecoderState.EnvelopeStart;
                        break;

                    case ClientFramingDecoderState.EnvelopeStart:
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                            CreateException(new InvalidDataException(SR.GetString(SR.FramingAtEnd))));

                    case ClientFramingDecoderState.ReadingFault:
                        recordType = (FramingRecordType)bytes[offset];
                        ValidateRecordType(FramingRecordType.Fault, recordType);
                        bytesConsumed = 1;
                        faultDecoder = new FaultStringDecoder();
                        base.CurrentState = ClientFramingDecoderState.ReadingFaultString;
                        break;
                    case ClientFramingDecoderState.ReadingFaultString:
                        bytesConsumed = faultDecoder.Decode(bytes, offset, size);
                        if (faultDecoder.IsValueDecoded)
                        {
                            base.CurrentState = ClientFramingDecoderState.Fault;
                        }
                        break;
                    case ClientFramingDecoderState.Fault:
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                            CreateException(new InvalidDataException(SR.GetString(SR.FramingAtEnd))));
                    default:
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                            CreateException(new InvalidDataException(SR.GetString(SR.InvalidDecoderStateMachine))));
                }

                StreamPosition += bytesConsumed;
                return bytesConsumed;
            }
            catch (InvalidDataException e)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateException(e));
            }
        }
        public override int Decode(byte[] bytes, int offset, int size)
        {
            int num2;
            DecoderHelper.ValidateSize(size);
            try
            {
                int envelopeBytesNeeded;
                FramingRecordType type;
                switch (base.CurrentState)
                {
                    case ClientFramingDecoderState.ReadingUpgradeRecord:
                        type = (FramingRecordType) bytes[offset];
                        if (type != FramingRecordType.UpgradeResponse)
                        {
                            break;
                        }
                        envelopeBytesNeeded = 1;
                        base.CurrentState = ClientFramingDecoderState.UpgradeResponse;
                        goto Label_0248;

                    case ClientFramingDecoderState.UpgradeResponse:
                        envelopeBytesNeeded = 0;
                        base.CurrentState = ClientFramingDecoderState.ReadingUpgradeRecord;
                        goto Label_0248;

                    case ClientFramingDecoderState.ReadingAckRecord:
                        type = (FramingRecordType) bytes[offset];
                        if (type != FramingRecordType.Fault)
                        {
                            goto Label_00AA;
                        }
                        envelopeBytesNeeded = 1;
                        this.faultDecoder = new FaultStringDecoder();
                        base.CurrentState = ClientFramingDecoderState.ReadingFaultString;
                        goto Label_0248;

                    case ClientFramingDecoderState.Start:
                        envelopeBytesNeeded = 0;
                        base.CurrentState = ClientFramingDecoderState.ReadingEnvelopeRecord;
                        goto Label_0248;

                    case ClientFramingDecoderState.ReadingFaultString:
                        envelopeBytesNeeded = this.faultDecoder.Decode(bytes, offset, size);
                        if (this.faultDecoder.IsValueDecoded)
                        {
                            base.CurrentState = ClientFramingDecoderState.Fault;
                        }
                        goto Label_0248;

                    case ClientFramingDecoderState.Fault:
                        envelopeBytesNeeded = 0;
                        base.CurrentState = ClientFramingDecoderState.ReadingEndRecord;
                        goto Label_0248;

                    case ClientFramingDecoderState.ReadingEnvelopeRecord:
                        type = (FramingRecordType) bytes[offset];
                        if (type != FramingRecordType.End)
                        {
                            goto Label_00E4;
                        }
                        envelopeBytesNeeded = 1;
                        base.CurrentState = ClientFramingDecoderState.End;
                        goto Label_0248;

                    case ClientFramingDecoderState.ReadingEnvelopeSize:
                        envelopeBytesNeeded = this.sizeDecoder.Decode(bytes, offset, size);
                        if (this.sizeDecoder.IsValueDecoded)
                        {
                            base.CurrentState = ClientFramingDecoderState.EnvelopeStart;
                            this.envelopeSize = this.sizeDecoder.Value;
                            this.envelopeBytesNeeded = this.envelopeSize;
                        }
                        goto Label_0248;

                    case ClientFramingDecoderState.EnvelopeStart:
                        envelopeBytesNeeded = 0;
                        base.CurrentState = ClientFramingDecoderState.ReadingEnvelopeBytes;
                        goto Label_0248;

                    case ClientFramingDecoderState.ReadingEnvelopeBytes:
                        envelopeBytesNeeded = size;
                        if (envelopeBytesNeeded > this.envelopeBytesNeeded)
                        {
                            envelopeBytesNeeded = this.envelopeBytesNeeded;
                        }
                        this.envelopeBytesNeeded -= envelopeBytesNeeded;
                        if (this.envelopeBytesNeeded == 0)
                        {
                            base.CurrentState = ClientFramingDecoderState.EnvelopeEnd;
                        }
                        goto Label_0248;

                    case ClientFramingDecoderState.EnvelopeEnd:
                        envelopeBytesNeeded = 0;
                        base.CurrentState = ClientFramingDecoderState.ReadingEnvelopeRecord;
                        goto Label_0248;

                    case ClientFramingDecoderState.ReadingEndRecord:
                        base.ValidateRecordType(FramingRecordType.End, (FramingRecordType) bytes[offset]);
                        envelopeBytesNeeded = 1;
                        base.CurrentState = ClientFramingDecoderState.End;
                        goto Label_0248;

                    case ClientFramingDecoderState.End:
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(new InvalidDataException(System.ServiceModel.SR.GetString("FramingAtEnd"))));

                    default:
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(new InvalidDataException(System.ServiceModel.SR.GetString("InvalidDecoderStateMachine"))));
                }
                envelopeBytesNeeded = 0;
                base.CurrentState = ClientFramingDecoderState.ReadingAckRecord;
                goto Label_0248;
            Label_00AA:
                base.ValidatePreambleAck(type);
                envelopeBytesNeeded = 1;
                base.CurrentState = ClientFramingDecoderState.Start;
                goto Label_0248;
            Label_00E4:
                if (type == FramingRecordType.Fault)
                {
                    envelopeBytesNeeded = 1;
                    this.faultDecoder = new FaultStringDecoder();
                    base.CurrentState = ClientFramingDecoderState.ReadingFaultString;
                }
                else
                {
                    base.ValidateRecordType(FramingRecordType.SizedEnvelope, type);
                    envelopeBytesNeeded = 1;
                    base.CurrentState = ClientFramingDecoderState.ReadingEnvelopeSize;
                    this.sizeDecoder.Reset();
                }
            Label_0248:
                base.StreamPosition += envelopeBytesNeeded;
                num2 = envelopeBytesNeeded;
            }
            catch (InvalidDataException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(exception));
            }
            return num2;
        }
        public override int Decode(byte[] bytes, int offset, int size)
        {
            int num2;

            DecoderHelper.ValidateSize(size);
            try
            {
                int num;
                FramingRecordType type;
                switch (base.CurrentState)
                {
                case ClientFramingDecoderState.ReadingUpgradeRecord:
                    type = (FramingRecordType)bytes[offset];
                    if (type != FramingRecordType.UpgradeResponse)
                    {
                        break;
                    }
                    num = 1;
                    base.CurrentState = ClientFramingDecoderState.UpgradeResponse;
                    goto Label_01A4;

                case ClientFramingDecoderState.UpgradeResponse:
                    num = 0;
                    base.CurrentState = ClientFramingDecoderState.ReadingUpgradeRecord;
                    goto Label_01A4;

                case ClientFramingDecoderState.ReadingAckRecord:
                    type = (FramingRecordType)bytes[offset];
                    if (type != FramingRecordType.Fault)
                    {
                        goto Label_009A;
                    }
                    num = 1;
                    this.faultDecoder = new FaultStringDecoder();
                    base.CurrentState = ClientFramingDecoderState.ReadingFaultString;
                    goto Label_01A4;

                case ClientFramingDecoderState.Start:
                    num = 0;
                    base.CurrentState = ClientFramingDecoderState.ReadingEnvelopeRecord;
                    goto Label_01A4;

                case ClientFramingDecoderState.ReadingFault:
                    type = (FramingRecordType)bytes[offset];
                    base.ValidateRecordType(FramingRecordType.Fault, type);
                    num = 1;
                    this.faultDecoder = new FaultStringDecoder();
                    base.CurrentState = ClientFramingDecoderState.ReadingFaultString;
                    goto Label_01A4;

                case ClientFramingDecoderState.ReadingFaultString:
                    num = this.faultDecoder.Decode(bytes, offset, size);
                    if (this.faultDecoder.IsValueDecoded)
                    {
                        base.CurrentState = ClientFramingDecoderState.Fault;
                    }
                    goto Label_01A4;

                case ClientFramingDecoderState.Fault:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(new InvalidDataException(System.ServiceModel.SR.GetString("FramingAtEnd"))));

                case ClientFramingDecoderState.ReadingEnvelopeRecord:
                    type = (FramingRecordType)bytes[offset];
                    if (type != FramingRecordType.End)
                    {
                        goto Label_00D4;
                    }
                    num = 1;
                    base.CurrentState = ClientFramingDecoderState.End;
                    goto Label_01A4;

                case ClientFramingDecoderState.EnvelopeStart:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(new InvalidDataException(System.ServiceModel.SR.GetString("FramingAtEnd"))));

                default:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(new InvalidDataException(System.ServiceModel.SR.GetString("InvalidDecoderStateMachine"))));
                }
                num = 0;
                base.CurrentState = ClientFramingDecoderState.ReadingAckRecord;
                goto Label_01A4;
Label_009A:
                base.ValidatePreambleAck(type);
                num = 1;
                base.CurrentState = ClientFramingDecoderState.Start;
                goto Label_01A4;
Label_00D4:
                if (type == FramingRecordType.Fault)
                {
                    num = 0;
                    base.CurrentState = ClientFramingDecoderState.ReadingFault;
                }
                else
                {
                    base.ValidateRecordType(FramingRecordType.UnsizedEnvelope, type);
                    num = 1;
                    base.CurrentState = ClientFramingDecoderState.EnvelopeStart;
                }
Label_01A4:
                base.StreamPosition += num;
                num2 = num;
            }
            catch (InvalidDataException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(exception));
            }
            return(num2);
        }
Ejemplo n.º 11
0
        public override int Decode(byte[] bytes, int offset, int size)
        {
            int num2;

            DecoderHelper.ValidateSize(size);
            try
            {
                int envelopeBytesNeeded;
                FramingRecordType type;
                switch (base.CurrentState)
                {
                case ClientFramingDecoderState.ReadingUpgradeRecord:
                    type = (FramingRecordType)bytes[offset];
                    if (type != FramingRecordType.UpgradeResponse)
                    {
                        break;
                    }
                    envelopeBytesNeeded = 1;
                    base.CurrentState   = ClientFramingDecoderState.UpgradeResponse;
                    goto Label_0248;

                case ClientFramingDecoderState.UpgradeResponse:
                    envelopeBytesNeeded = 0;
                    base.CurrentState   = ClientFramingDecoderState.ReadingUpgradeRecord;
                    goto Label_0248;

                case ClientFramingDecoderState.ReadingAckRecord:
                    type = (FramingRecordType)bytes[offset];
                    if (type != FramingRecordType.Fault)
                    {
                        goto Label_00AA;
                    }
                    envelopeBytesNeeded = 1;
                    this.faultDecoder   = new FaultStringDecoder();
                    base.CurrentState   = ClientFramingDecoderState.ReadingFaultString;
                    goto Label_0248;

                case ClientFramingDecoderState.Start:
                    envelopeBytesNeeded = 0;
                    base.CurrentState   = ClientFramingDecoderState.ReadingEnvelopeRecord;
                    goto Label_0248;

                case ClientFramingDecoderState.ReadingFaultString:
                    envelopeBytesNeeded = this.faultDecoder.Decode(bytes, offset, size);
                    if (this.faultDecoder.IsValueDecoded)
                    {
                        base.CurrentState = ClientFramingDecoderState.Fault;
                    }
                    goto Label_0248;

                case ClientFramingDecoderState.Fault:
                    envelopeBytesNeeded = 0;
                    base.CurrentState   = ClientFramingDecoderState.ReadingEndRecord;
                    goto Label_0248;

                case ClientFramingDecoderState.ReadingEnvelopeRecord:
                    type = (FramingRecordType)bytes[offset];
                    if (type != FramingRecordType.End)
                    {
                        goto Label_00E4;
                    }
                    envelopeBytesNeeded = 1;
                    base.CurrentState   = ClientFramingDecoderState.End;
                    goto Label_0248;

                case ClientFramingDecoderState.ReadingEnvelopeSize:
                    envelopeBytesNeeded = this.sizeDecoder.Decode(bytes, offset, size);
                    if (this.sizeDecoder.IsValueDecoded)
                    {
                        base.CurrentState        = ClientFramingDecoderState.EnvelopeStart;
                        this.envelopeSize        = this.sizeDecoder.Value;
                        this.envelopeBytesNeeded = this.envelopeSize;
                    }
                    goto Label_0248;

                case ClientFramingDecoderState.EnvelopeStart:
                    envelopeBytesNeeded = 0;
                    base.CurrentState   = ClientFramingDecoderState.ReadingEnvelopeBytes;
                    goto Label_0248;

                case ClientFramingDecoderState.ReadingEnvelopeBytes:
                    envelopeBytesNeeded = size;
                    if (envelopeBytesNeeded > this.envelopeBytesNeeded)
                    {
                        envelopeBytesNeeded = this.envelopeBytesNeeded;
                    }
                    this.envelopeBytesNeeded -= envelopeBytesNeeded;
                    if (this.envelopeBytesNeeded == 0)
                    {
                        base.CurrentState = ClientFramingDecoderState.EnvelopeEnd;
                    }
                    goto Label_0248;

                case ClientFramingDecoderState.EnvelopeEnd:
                    envelopeBytesNeeded = 0;
                    base.CurrentState   = ClientFramingDecoderState.ReadingEnvelopeRecord;
                    goto Label_0248;

                case ClientFramingDecoderState.ReadingEndRecord:
                    base.ValidateRecordType(FramingRecordType.End, (FramingRecordType)bytes[offset]);
                    envelopeBytesNeeded = 1;
                    base.CurrentState   = ClientFramingDecoderState.End;
                    goto Label_0248;

                case ClientFramingDecoderState.End:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(new InvalidDataException(System.ServiceModel.SR.GetString("FramingAtEnd"))));

                default:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(new InvalidDataException(System.ServiceModel.SR.GetString("InvalidDecoderStateMachine"))));
                }
                envelopeBytesNeeded = 0;
                base.CurrentState   = ClientFramingDecoderState.ReadingAckRecord;
                goto Label_0248;
Label_00AA:
                base.ValidatePreambleAck(type);
                envelopeBytesNeeded = 1;
                base.CurrentState   = ClientFramingDecoderState.Start;
                goto Label_0248;
Label_00E4:
                if (type == FramingRecordType.Fault)
                {
                    envelopeBytesNeeded = 1;
                    this.faultDecoder   = new FaultStringDecoder();
                    base.CurrentState   = ClientFramingDecoderState.ReadingFaultString;
                }
                else
                {
                    base.ValidateRecordType(FramingRecordType.SizedEnvelope, type);
                    envelopeBytesNeeded = 1;
                    base.CurrentState   = ClientFramingDecoderState.ReadingEnvelopeSize;
                    this.sizeDecoder.Reset();
                }
Label_0248:
                base.StreamPosition += envelopeBytesNeeded;
                num2 = envelopeBytesNeeded;
            }
            catch (InvalidDataException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(exception));
            }
            return(num2);
        }