private bool HandleWriteUpgradeResponseComplete(IAsyncResult result)
 {
     bool flag;
     this.channel.Connection.EndWrite(result);
     IConnection innerConnection = this.channel.Connection;
     if (this.channel.size > 0)
     {
         innerConnection = new PreReadConnection(innerConnection, this.channel.connectionBuffer, this.channel.offset, this.channel.size);
     }
     if (onUpgradeConnection == null)
     {
         onUpgradeConnection = Fx.ThunkCallback(new AsyncCallback(ServerSessionPreambleConnectionReader.ServerFramingDuplexSessionChannel.OpenAsyncResult.OnUpgradeConnection));
     }
     try
     {
         IAsyncResult result2 = InitialServerConnectionReader.BeginUpgradeConnection(innerConnection, this.channel.upgradeAcceptor, this.channel, onUpgradeConnection, this);
         if (!result2.CompletedSynchronously)
         {
             return false;
         }
         flag = this.HandleUpgradeConnectionComplete(result2);
     }
     catch (Exception exception)
     {
         if (Fx.IsFatal(exception))
         {
             throw;
         }
         this.channel.WriteAuditFailure(this.channel.upgradeAcceptor as StreamSecurityUpgradeAcceptor, exception);
         throw;
     }
     return flag;
 }
        public IConnection GetRawConnection()
        {
            IConnection innerConnection = null;

            if (this.rawConnection == null)
            {
                return(innerConnection);
            }
            innerConnection    = this.rawConnection;
            this.rawConnection = null;
            if (this.size <= 0)
            {
                return(innerConnection);
            }
            PreReadConnection connection2 = innerConnection as PreReadConnection;

            if (connection2 != null)
            {
                connection2.AddPreReadData(this.buffer, this.offset, this.size);
                return(innerConnection);
            }
            return(new PreReadConnection(innerConnection, this.buffer, this.offset, this.size));
        }
Example #3
0
        public IConnection GetRawConnection()
        {
            IConnection result = null;

            if (_rawConnection != null)
            {
                result         = _rawConnection;
                _rawConnection = null;
                if (_size > 0)
                {
                    PreReadConnection preReadConnection = result as PreReadConnection;
                    if (preReadConnection != null) // make sure we don't keep wrapping
                    {
                        preReadConnection.AddPreReadData(_buffer, _offset, _size);
                    }
                    else
                    {
                        result = new PreReadConnection(result, _buffer, _offset, _size);
                    }
                }
            }

            return(result);
        }
Example #4
0
        public IConnection GetRawConnection()
        {
            IConnection result = null;
            if (_rawConnection != null)
            {
                result = _rawConnection;
                _rawConnection = null;
                if (_size > 0)
                {
                    PreReadConnection preReadConnection = result as PreReadConnection;
                    if (preReadConnection != null) // make sure we don't keep wrapping
                    {
                        preReadConnection.AddPreReadData(_buffer, _offset, _size);
                    }
                    else
                    {
                        result = new PreReadConnection(result, _buffer, _offset, _size);
                    }
                }
            }

            return result;
        }
Example #5
0
        public async Task <Message> ReceiveAsync(TimeoutHelper timeoutHelper)
        {
            byte[] buffer = Fx.AllocateByteArray(_connection.AsyncReadBufferSize);

            if (_size > 0)
            {
                Buffer.BlockCopy(_connection.AsyncReadBuffer, _offset, buffer, _offset, _size);
            }

            for (;;)
            {
                if (DecodeBytes(buffer, ref _offset, ref _size, ref _isAtEof))
                {
                    break;
                }

                if (_isAtEof)
                {
                    DoneReceiving(true, timeoutHelper.RemainingTime());
                    return(null);
                }

                if (_size == 0)
                {
                    _offset = 0;
                    _size   = await _connection.ReadAsync(buffer, 0, buffer.Length, timeoutHelper.RemainingTime());

                    if (_size == 0)
                    {
                        DoneReceiving(true, timeoutHelper.RemainingTime());
                        return(null);
                    }
                }
            }

            // we're ready to read a message
            IConnection singletonConnection = _connection;

            if (_size > 0)
            {
                byte[] initialData = Fx.AllocateByteArray(_size);
                Buffer.BlockCopy(buffer, _offset, initialData, 0, _size);
                singletonConnection = new PreReadConnection(singletonConnection, initialData);
            }

            Stream connectionStream = new SingletonInputConnectionStream(this, singletonConnection, _transportSettings);

            _inputStream = new MaxMessageSizeStream(connectionStream, _transportSettings.MaxReceivedMessageSize);
            using (ServiceModelActivity activity = DiagnosticUtility.ShouldUseActivity ? ServiceModelActivity.CreateBoundedActivity(true) : null)
            {
                if (DiagnosticUtility.ShouldUseActivity)
                {
                    ServiceModelActivity.Start(activity, SR.Format(SR.ActivityProcessingMessage, TraceUtility.RetrieveMessageNumber()), ActivityType.ProcessMessage);
                }

                Message message = null;
                try
                {
                    message = await _transportSettings.MessageEncoderFactory.Encoder.ReadMessageAsync(
                        _inputStream, _transportSettings.MaxBufferSize, this.ContentType);
                }
                catch (XmlException xmlException)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                              new ProtocolException(SR.Format(SR.MessageXmlProtocolError), xmlException));
                }

                if (DiagnosticUtility.ShouldUseActivity)
                {
                    TraceUtility.TransferFromTransport(message);
                }

                PrepareMessage(message);

                return(message);
            }
        }
Example #6
0
        public Message Receive(TimeSpan timeout)
        {
            byte[] dst = DiagnosticUtility.Utility.AllocateByteArray(this.connection.AsyncReadBufferSize);
            if (this.size > 0)
            {
                Buffer.BlockCopy(this.connection.AsyncReadBuffer, this.offset, dst, this.offset, this.size);
            }
            TimeoutHelper helper = new TimeoutHelper(timeout);

            while (!this.DecodeBytes(dst, ref this.offset, ref this.size, ref this.isAtEof))
            {
                if (this.isAtEof)
                {
                    this.DoneReceiving(true, helper.RemainingTime());
                    return(null);
                }
                if (this.size == 0)
                {
                    this.offset = 0;
                    this.size   = this.connection.Read(dst, 0, dst.Length, helper.RemainingTime());
                    if (this.size == 0)
                    {
                        this.DoneReceiving(true, helper.RemainingTime());
                        return(null);
                    }
                }
            }
            IConnection innerConnection = this.connection;

            if (this.size > 0)
            {
                byte[] buffer2 = DiagnosticUtility.Utility.AllocateByteArray(this.size);
                Buffer.BlockCopy(dst, this.offset, buffer2, 0, this.size);
                innerConnection = new PreReadConnection(innerConnection, buffer2);
            }
            Stream stream = new SingletonInputConnectionStream(this, innerConnection, this.transportSettings);

            this.inputStream = new MaxMessageSizeStream(stream, this.transportSettings.MaxReceivedMessageSize);
            using (ServiceModelActivity activity = DiagnosticUtility.ShouldUseActivity ? ServiceModelActivity.CreateBoundedActivity(true) : null)
            {
                if (DiagnosticUtility.ShouldUseActivity)
                {
                    ServiceModelActivity.Start(activity, System.ServiceModel.SR.GetString("ActivityProcessingMessage", new object[] { TraceUtility.RetrieveMessageNumber() }), ActivityType.ProcessMessage);
                }
                Message message = null;
                try
                {
                    message = this.transportSettings.MessageEncoderFactory.Encoder.ReadMessage(this.inputStream, this.transportSettings.MaxBufferSize, this.ContentType);
                }
                catch (XmlException exception)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("MessageXmlProtocolError"), exception));
                }
                if (DiagnosticUtility.ShouldUseActivity)
                {
                    TraceUtility.TransferFromTransport(message);
                }
                this.PrepareMessage(message);
                return(message);
            }
        }
        public async Task<Message> ReceiveAsync(TimeoutHelper timeoutHelper)
        {
            byte[] buffer = Fx.AllocateByteArray(_connection.AsyncReadBufferSize);

            if (_size > 0)
            {
                Buffer.BlockCopy(_connection.AsyncReadBuffer, _offset, buffer, _offset, _size);
            }

            for (;;)
            {
                if (DecodeBytes(buffer, ref _offset, ref _size, ref _isAtEof))
                {
                    break;
                }

                if (_isAtEof)
                {
                    DoneReceiving(true, timeoutHelper.RemainingTime());
                    return null;
                }

                if (_size == 0)
                {
                    _offset = 0;
                    _size = await _connection.ReadAsync(buffer, 0, buffer.Length, timeoutHelper.RemainingTime());
                    if (_size == 0)
                    {
                        DoneReceiving(true, timeoutHelper.RemainingTime());
                        return null;
                    }
                }
            }

            // we're ready to read a message
            IConnection singletonConnection = _connection;
            if (_size > 0)
            {
                byte[] initialData = Fx.AllocateByteArray(_size);
                Buffer.BlockCopy(buffer, _offset, initialData, 0, _size);
                singletonConnection = new PreReadConnection(singletonConnection, initialData);
            }

            Stream connectionStream = new SingletonInputConnectionStream(this, singletonConnection, _transportSettings);
            _inputStream = new MaxMessageSizeStream(connectionStream, _transportSettings.MaxReceivedMessageSize);
            using (ServiceModelActivity activity = DiagnosticUtility.ShouldUseActivity ? ServiceModelActivity.CreateBoundedActivity(true) : null)
            {
                if (DiagnosticUtility.ShouldUseActivity)
                {
                    ServiceModelActivity.Start(activity, SR.Format(SR.ActivityProcessingMessage, TraceUtility.RetrieveMessageNumber()), ActivityType.ProcessMessage);
                }

                Message message = null;
                try
                {
                    message = await _transportSettings.MessageEncoderFactory.Encoder.ReadMessageAsync(
                        _inputStream, _transportSettings.MaxBufferSize, this.ContentType);
                }
                catch (XmlException xmlException)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                        new ProtocolException(SR.Format(SR.MessageXmlProtocolError), xmlException));
                }

                if (DiagnosticUtility.ShouldUseActivity)
                {
                    TraceUtility.TransferFromTransport(message);
                }

                PrepareMessage(message);

                return message;
            }
        }
 public Message Receive(TimeSpan timeout)
 {
     byte[] dst = DiagnosticUtility.Utility.AllocateByteArray(this.connection.AsyncReadBufferSize);
     if (this.size > 0)
     {
         Buffer.BlockCopy(this.connection.AsyncReadBuffer, this.offset, dst, this.offset, this.size);
     }
     TimeoutHelper helper = new TimeoutHelper(timeout);
     while (!this.DecodeBytes(dst, ref this.offset, ref this.size, ref this.isAtEof))
     {
         if (this.isAtEof)
         {
             this.DoneReceiving(true, helper.RemainingTime());
             return null;
         }
         if (this.size == 0)
         {
             this.offset = 0;
             this.size = this.connection.Read(dst, 0, dst.Length, helper.RemainingTime());
             if (this.size == 0)
             {
                 this.DoneReceiving(true, helper.RemainingTime());
                 return null;
             }
         }
     }
     IConnection innerConnection = this.connection;
     if (this.size > 0)
     {
         byte[] buffer2 = DiagnosticUtility.Utility.AllocateByteArray(this.size);
         Buffer.BlockCopy(dst, this.offset, buffer2, 0, this.size);
         innerConnection = new PreReadConnection(innerConnection, buffer2);
     }
     Stream stream = new SingletonInputConnectionStream(this, innerConnection, this.transportSettings);
     this.inputStream = new MaxMessageSizeStream(stream, this.transportSettings.MaxReceivedMessageSize);
     using (ServiceModelActivity activity = DiagnosticUtility.ShouldUseActivity ? ServiceModelActivity.CreateBoundedActivity(true) : null)
     {
         if (DiagnosticUtility.ShouldUseActivity)
         {
             ServiceModelActivity.Start(activity, System.ServiceModel.SR.GetString("ActivityProcessingMessage", new object[] { TraceUtility.RetrieveMessageNumber() }), ActivityType.ProcessMessage);
         }
         Message message = null;
         try
         {
             message = this.transportSettings.MessageEncoderFactory.Encoder.ReadMessage(this.inputStream, this.transportSettings.MaxBufferSize, this.ContentType);
         }
         catch (XmlException exception)
         {
             throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("MessageXmlProtocolError"), exception));
         }
         if (DiagnosticUtility.ShouldUseActivity)
         {
             TraceUtility.TransferFromTransport(message);
         }
         this.PrepareMessage(message);
         return message;
     }
 }
        public IConnection CompletePreamble(TimeSpan timeout)
        {
            int           num;
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            if (!this.transportSettings.MessageEncoderFactory.Encoder.IsContentTypeSupported(this.decoder.ContentType))
            {
                this.SendFault("http://schemas.microsoft.com/ws/2006/05/framing/faults/ContentTypeInvalid", ref timeoutHelper);
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("ContentTypeMismatch", new object[] { this.decoder.ContentType, this.transportSettings.MessageEncoderFactory.Encoder.ContentType })));
            }
            StreamUpgradeAcceptor upgradeAcceptor         = null;
            StreamUpgradeProvider upgrade                 = this.transportSettings.Upgrade;
            IStreamUpgradeChannelBindingProvider property = null;

            if (upgrade != null)
            {
                property        = upgrade.GetProperty <IStreamUpgradeChannelBindingProvider>();
                upgradeAcceptor = upgrade.CreateUpgradeAcceptor();
            }
            IConnection connection = base.Connection;

Label_00B1:
            if (this.size == 0)
            {
                this.offset = 0;
                this.size   = connection.Read(this.connectionBuffer, 0, this.connectionBuffer.Length, timeoutHelper.RemainingTime());
                if (this.size == 0)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(this.decoder.CreatePrematureEOFException());
                }
            }
Label_0101:
            num = this.decoder.Decode(this.connectionBuffer, this.offset, this.size);
            if (num > 0)
            {
                this.offset += num;
                this.size   -= num;
            }
            switch (this.decoder.CurrentState)
            {
            case ServerSingletonDecoder.State.UpgradeRequest:
            {
                if (upgradeAcceptor == null)
                {
                    this.SendFault("http://schemas.microsoft.com/ws/2006/05/framing/faults/UpgradeInvalid", ref timeoutHelper);
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("UpgradeRequestToNonupgradableService", new object[] { this.decoder.Upgrade })));
                }
                if (!upgradeAcceptor.CanUpgrade(this.decoder.Upgrade))
                {
                    this.SendFault("http://schemas.microsoft.com/ws/2006/05/framing/faults/UpgradeInvalid", ref timeoutHelper);
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("UpgradeProtocolNotSupported", new object[] { this.decoder.Upgrade })));
                }
                connection.Write(ServerSingletonEncoder.UpgradeResponseBytes, 0, ServerSingletonEncoder.UpgradeResponseBytes.Length, true, timeoutHelper.RemainingTime());
                IConnection innerConnection = connection;
                if (this.size > 0)
                {
                    innerConnection = new PreReadConnection(innerConnection, this.connectionBuffer, this.offset, this.size);
                }
                try
                {
                    connection            = InitialServerConnectionReader.UpgradeConnection(innerConnection, upgradeAcceptor, this.transportSettings);
                    this.connectionBuffer = connection.AsyncReadBuffer;
                    if ((property != null) && property.IsChannelBindingSupportEnabled)
                    {
                        this.channelBindingToken = property.GetChannelBinding(upgradeAcceptor, ChannelBindingKind.Endpoint);
                    }
                    goto Label_02C0;
                }
                catch (Exception exception)
                {
                    if (Fx.IsFatal(exception))
                    {
                        throw;
                    }
                    this.WriteAuditFailure(upgradeAcceptor as StreamSecurityUpgradeAcceptor, exception);
                    throw;
                }
                break;
            }

            case ServerSingletonDecoder.State.Start:
                break;

            default:
                goto Label_02C0;
            }
            this.SetupSecurityIfNecessary(upgradeAcceptor);
            connection.Write(ServerSessionEncoder.AckResponseBytes, 0, ServerSessionEncoder.AckResponseBytes.Length, true, timeoutHelper.RemainingTime());
            return(connection);

Label_02C0:
            if (this.size != 0)
            {
                goto Label_0101;
            }
            goto Label_00B1;
        }
            protected override void OnOpen(TimeSpan timeout)
            {
                bool flag = false;
                try
                {
                    TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
                    this.ValidateContentType(ref timeoutHelper);
                Label_0017:
                    if (this.size == 0)
                    {
                        this.offset = 0;
                        this.size = base.Connection.Read(this.connectionBuffer, 0, this.connectionBuffer.Length, timeoutHelper.RemainingTime());
                        if (this.size == 0)
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(this.decoder.CreatePrematureEOFException());
                        }
                    }
                Label_006B:
                    this.DecodeBytes();
                    switch (this.decoder.CurrentState)
                    {
                        case ServerSessionDecoder.State.UpgradeRequest:
                        {
                            this.ProcessUpgradeRequest(ref timeoutHelper);
                            base.Connection.Write(ServerSessionEncoder.UpgradeResponseBytes, 0, ServerSessionEncoder.UpgradeResponseBytes.Length, true, timeoutHelper.RemainingTime());
                            IConnection innerConnection = base.Connection;
                            if (this.size > 0)
                            {
                                innerConnection = new PreReadConnection(innerConnection, this.connectionBuffer, this.offset, this.size);
                            }
                            try
                            {
                                base.Connection = InitialServerConnectionReader.UpgradeConnection(innerConnection, this.upgradeAcceptor, this);
                                if ((this.channelBindingProvider != null) && this.channelBindingProvider.IsChannelBindingSupportEnabled)
                                {
                                    base.SetChannelBinding(this.channelBindingProvider.GetChannelBinding(this.upgradeAcceptor, ChannelBindingKind.Endpoint));
                                }
                                this.connectionBuffer = base.Connection.AsyncReadBuffer;
                                goto Label_018C;
                            }
                            catch (Exception exception)
                            {
                                if (Fx.IsFatal(exception))
                                {
                                    throw;
                                }
                                this.WriteAuditFailure(this.upgradeAcceptor as StreamSecurityUpgradeAcceptor, exception);
                                throw;
                            }
                            break;
                        }
                        case ServerSessionDecoder.State.Start:
                            break;

                        default:
                            goto Label_018C;
                    }
                    this.SetupSecurityIfNecessary();
                    base.Connection.Write(ServerSessionEncoder.AckResponseBytes, 0, ServerSessionEncoder.AckResponseBytes.Length, true, timeoutHelper.RemainingTime());
                    this.SetupSessionReader();
                    flag = true;
                    return;
                Label_018C:
                    if (this.size != 0)
                    {
                        goto Label_006B;
                    }
                    goto Label_0017;
                }
                finally
                {
                    if (!flag)
                    {
                        base.Connection.Abort();
                    }
                }
            }
        public IConnection CompletePreamble(TimeSpan timeout)
        {
            int num;
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
            if (!this.transportSettings.MessageEncoderFactory.Encoder.IsContentTypeSupported(this.decoder.ContentType))
            {
                this.SendFault("http://schemas.microsoft.com/ws/2006/05/framing/faults/ContentTypeInvalid", ref timeoutHelper);
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("ContentTypeMismatch", new object[] { this.decoder.ContentType, this.transportSettings.MessageEncoderFactory.Encoder.ContentType })));
            }
            StreamUpgradeAcceptor upgradeAcceptor = null;
            StreamUpgradeProvider upgrade = this.transportSettings.Upgrade;
            IStreamUpgradeChannelBindingProvider property = null;
            if (upgrade != null)
            {
                property = upgrade.GetProperty<IStreamUpgradeChannelBindingProvider>();
                upgradeAcceptor = upgrade.CreateUpgradeAcceptor();
            }
            IConnection connection = base.Connection;
        Label_00B1:
            if (this.size == 0)
            {
                this.offset = 0;
                this.size = connection.Read(this.connectionBuffer, 0, this.connectionBuffer.Length, timeoutHelper.RemainingTime());
                if (this.size == 0)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(this.decoder.CreatePrematureEOFException());
                }
            }
        Label_0101:
            num = this.decoder.Decode(this.connectionBuffer, this.offset, this.size);
            if (num > 0)
            {
                this.offset += num;
                this.size -= num;
            }
            switch (this.decoder.CurrentState)
            {
                case ServerSingletonDecoder.State.UpgradeRequest:
                {
                    if (upgradeAcceptor == null)
                    {
                        this.SendFault("http://schemas.microsoft.com/ws/2006/05/framing/faults/UpgradeInvalid", ref timeoutHelper);
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("UpgradeRequestToNonupgradableService", new object[] { this.decoder.Upgrade })));
                    }
                    if (!upgradeAcceptor.CanUpgrade(this.decoder.Upgrade))
                    {
                        this.SendFault("http://schemas.microsoft.com/ws/2006/05/framing/faults/UpgradeInvalid", ref timeoutHelper);
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("UpgradeProtocolNotSupported", new object[] { this.decoder.Upgrade })));
                    }
                    connection.Write(ServerSingletonEncoder.UpgradeResponseBytes, 0, ServerSingletonEncoder.UpgradeResponseBytes.Length, true, timeoutHelper.RemainingTime());
                    IConnection innerConnection = connection;
                    if (this.size > 0)
                    {
                        innerConnection = new PreReadConnection(innerConnection, this.connectionBuffer, this.offset, this.size);
                    }
                    try
                    {
                        connection = InitialServerConnectionReader.UpgradeConnection(innerConnection, upgradeAcceptor, this.transportSettings);
                        this.connectionBuffer = connection.AsyncReadBuffer;
                        if ((property != null) && property.IsChannelBindingSupportEnabled)
                        {
                            this.channelBindingToken = property.GetChannelBinding(upgradeAcceptor, ChannelBindingKind.Endpoint);
                        }
                        goto Label_02C0;
                    }
                    catch (Exception exception)
                    {
                        if (Fx.IsFatal(exception))
                        {
                            throw;
                        }
                        this.WriteAuditFailure(upgradeAcceptor as StreamSecurityUpgradeAcceptor, exception);
                        throw;
                    }
                    break;
                }
                case ServerSingletonDecoder.State.Start:
                    break;

                default:
                    goto Label_02C0;
            }
            this.SetupSecurityIfNecessary(upgradeAcceptor);
            connection.Write(ServerSessionEncoder.AckResponseBytes, 0, ServerSessionEncoder.AckResponseBytes.Length, true, timeoutHelper.RemainingTime());
            return connection;
        Label_02C0:
            if (this.size != 0)
            {
                goto Label_0101;
            }
            goto Label_00B1;
        }