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());
        }
        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.º 6
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);
        }