Beispiel #1
0
        private IConnection SendPreamble(IConnection connection, ArraySegment <byte> preamble, ref TimeoutHelper timeoutHelper)
        {
            // initialize a new decoder
            _decoder = new ClientDuplexDecoder(0);
            byte[] ackBuffer = new byte[1];
            connection.Write(preamble.Array, preamble.Offset, preamble.Count, true, timeoutHelper.RemainingTime());

            if (_upgrade != null)
            {
                StreamUpgradeInitiator upgradeInitiator = _upgrade.CreateUpgradeInitiator(this.RemoteAddress, this.Via);

                upgradeInitiator.Open(timeoutHelper.RemainingTime());
                if (!ConnectionUpgradeHelper.InitiateUpgrade(upgradeInitiator, ref connection, _decoder, this, ref timeoutHelper))
                {
                    ConnectionUpgradeHelper.DecodeFramingFault(_decoder, connection, this.Via, MessageEncoder.ContentType, ref timeoutHelper);
                }

                SetRemoteSecurity(upgradeInitiator);
                upgradeInitiator.Close(timeoutHelper.RemainingTime());
                connection.Write(ClientDuplexEncoder.PreambleEndBytes, 0, ClientDuplexEncoder.PreambleEndBytes.Length, true, timeoutHelper.RemainingTime());
            }

            // read ACK
            int ackBytesRead = connection.Read(ackBuffer, 0, ackBuffer.Length, timeoutHelper.RemainingTime());

            if (!ConnectionUpgradeHelper.ValidatePreambleResponse(ackBuffer, ackBytesRead, _decoder, Via))
            {
                ConnectionUpgradeHelper.DecodeFramingFault(_decoder, connection, Via,
                                                           MessageEncoder.ContentType, ref timeoutHelper);
            }

            return(connection);
        }
Beispiel #2
0
        private IConnection SendPreamble(IConnection connection, ref TimeoutHelper timeoutHelper, ClientFramingDecoder decoder, out SecurityMessageProperty remoteSecurity)
        {
            connection.Write(this.Preamble, 0, this.Preamble.Length, true, timeoutHelper.RemainingTime());
            if (this.upgrade != null)
            {
                IStreamUpgradeChannelBindingProvider property = this.upgrade.GetProperty <IStreamUpgradeChannelBindingProvider>();
                StreamUpgradeInitiator upgradeInitiator       = this.upgrade.CreateUpgradeInitiator(base.RemoteAddress, base.Via);
                if (!ConnectionUpgradeHelper.InitiateUpgrade(upgradeInitiator, ref connection, decoder, this, ref timeoutHelper))
                {
                    ConnectionUpgradeHelper.DecodeFramingFault(decoder, connection, base.Via, this.messageEncoder.ContentType, ref timeoutHelper);
                }
                if ((property != null) && property.IsChannelBindingSupportEnabled)
                {
                    this.channelBindingToken = property.GetChannelBinding(upgradeInitiator, ChannelBindingKind.Endpoint);
                }
                remoteSecurity = StreamSecurityUpgradeInitiator.GetRemoteSecurity(upgradeInitiator);
                connection.Write(ClientSingletonEncoder.PreambleEndBytes, 0, ClientSingletonEncoder.PreambleEndBytes.Length, true, timeoutHelper.RemainingTime());
            }
            else
            {
                remoteSecurity = null;
            }
            byte[] buffer = new byte[1];
            int    count  = connection.Read(buffer, 0, buffer.Length, timeoutHelper.RemainingTime());

            if (!ConnectionUpgradeHelper.ValidatePreambleResponse(buffer, count, decoder, base.Via))
            {
                ConnectionUpgradeHelper.DecodeFramingFault(decoder, connection, base.Via, this.messageEncoder.ContentType, ref timeoutHelper);
            }
            return(connection);
        }
                bool HandlePreambleAck()
                {
                    int ackBytesRead = connection.EndRead();

                    if (!ConnectionUpgradeHelper.ValidatePreambleResponse(
                            connection.AsyncReadBuffer, ackBytesRead, decoder, channel.Via))
                    {
                        if (onFailedUpgrade == null)
                        {
                            onFailedUpgrade = Fx.ThunkCallback(new AsyncCallback(OnFailedUpgrade));
                        }
                        IAsyncResult decodeFaultResult = ConnectionUpgradeHelper.BeginDecodeFramingFault(decoder,
                                                                                                         connection, channel.Via, channel.messageEncoder.ContentType, ref timeoutHelper,
                                                                                                         onFailedUpgrade, this);

                        if (!decodeFaultResult.CompletedSynchronously)
                        {
                            return(false);
                        }

                        ConnectionUpgradeHelper.EndDecodeFramingFault(decodeFaultResult);
                        return(true);
                    }

                    return(true);
                }
Beispiel #4
0
        private IConnection SendPreamble(IConnection connection, ArraySegment <byte> preamble, ref TimeoutHelper timeoutHelper)
        {
            this.decoder = new ClientDuplexDecoder(0L);
            byte[] buffer = new byte[1];
            connection.Write(preamble.Array, preamble.Offset, preamble.Count, true, timeoutHelper.RemainingTime());
            if (this.upgrade != null)
            {
                IStreamUpgradeChannelBindingProvider property = this.upgrade.GetProperty <IStreamUpgradeChannelBindingProvider>();
                StreamUpgradeInitiator upgradeInitiator       = this.upgrade.CreateUpgradeInitiator(this.RemoteAddress, this.Via);
                upgradeInitiator.Open(timeoutHelper.RemainingTime());
                if (!ConnectionUpgradeHelper.InitiateUpgrade(upgradeInitiator, ref connection, this.decoder, this, ref timeoutHelper))
                {
                    ConnectionUpgradeHelper.DecodeFramingFault(this.decoder, connection, this.Via, base.MessageEncoder.ContentType, ref timeoutHelper);
                }
                if ((property != null) && property.IsChannelBindingSupportEnabled)
                {
                    base.SetChannelBinding(property.GetChannelBinding(upgradeInitiator, ChannelBindingKind.Endpoint));
                }
                this.SetRemoteSecurity(upgradeInitiator);
                upgradeInitiator.Close(timeoutHelper.RemainingTime());
                connection.Write(SessionEncoder.PreambleEndBytes, 0, SessionEncoder.PreambleEndBytes.Length, true, timeoutHelper.RemainingTime());
            }
            int count = connection.Read(buffer, 0, buffer.Length, timeoutHelper.RemainingTime());

            if (!ConnectionUpgradeHelper.ValidatePreambleResponse(buffer, count, this.decoder, this.Via))
            {
                ConnectionUpgradeHelper.DecodeFramingFault(this.decoder, connection, this.Via, base.MessageEncoder.ContentType, ref timeoutHelper);
            }
            return(connection);
        }
                static void OnFailedUpgrade(IAsyncResult result)
                {
                    if (result.CompletedSynchronously)
                    {
                        return;
                    }

                    SendPreambleAsyncResult thisPtr = (SendPreambleAsyncResult)result.AsyncState;

                    Exception completionException = null;

                    try
                    {
                        ConnectionUpgradeHelper.EndDecodeFramingFault(result);
                    }
#pragma warning suppress 56500 // Microsoft, transferring exception to another thread
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e))
                        {
                            throw;
                        }

                        completionException = e;
                    }

                    thisPtr.Complete(false, completionException);
                }
                bool HandleUpgrade(IAsyncResult result)
                {
                    connection = ConnectionUpgradeHelper.EndInitiateUpgrade(result);

                    if (this.channelBindingProvider != null && this.channelBindingProvider.IsChannelBindingSupportEnabled)
                    {
                        this.channel.channelBindingToken = this.channelBindingProvider.GetChannelBinding(this.upgradeInitiator, ChannelBindingKind.Endpoint);
                    }

                    this.remoteSecurity   = StreamSecurityUpgradeInitiator.GetRemoteSecurity(this.upgradeInitiator);
                    this.upgradeInitiator = null; // we're done with the initiator
                    if (onWritePreambleEnd == null)
                    {
                        onWritePreambleEnd = Fx.ThunkCallback(new WaitCallback(OnWritePreambleEnd));
                    }

                    AsyncCompletionResult writePreambleResult = connection.BeginWrite(
                        ClientSingletonEncoder.PreambleEndBytes, 0, ClientSingletonEncoder.PreambleEndBytes.Length, true,
                        timeoutHelper.RemainingTime(), onWritePreambleEnd, this);

                    if (writePreambleResult == AsyncCompletionResult.Queued)
                    {
                        return(false);
                    }

                    connection.EndWrite();
                    return(ReadPreambleAck());
                }
                bool HandleWritePreamble()
                {
                    connection.EndWrite();

                    if (channel.upgrade == null)
                    {
                        return(ReadPreambleAck());
                    }
                    else
                    {
                        this.channelBindingProvider = channel.upgrade.GetProperty <IStreamUpgradeChannelBindingProvider>();
                        this.upgradeInitiator       = channel.upgrade.CreateUpgradeInitiator(channel.RemoteAddress, channel.Via);
                        if (onUpgrade == null)
                        {
                            onUpgrade = Fx.ThunkCallback(new AsyncCallback(OnUpgrade));
                        }

                        IAsyncResult initiateUpgradeResult = ConnectionUpgradeHelper.BeginInitiateUpgrade(channel.settings, channel.RemoteAddress,
                                                                                                          connection, decoder, this.upgradeInitiator, channel.messageEncoder.ContentType, null,
                                                                                                          this.timeoutHelper, onUpgrade, this);

                        if (!initiateUpgradeResult.CompletedSynchronously)
                        {
                            return(false);
                        }
                        return(HandleUpgrade(initiateUpgradeResult));
                    }
                }
Beispiel #8
0
        private async Task <IConnection> SendPreambleAsync(IConnection connection, ArraySegment <byte> preamble, TimeSpan timeout)
        {
            var timeoutHelper = new TimeoutHelper(timeout);

            // initialize a new decoder
            _decoder = new ClientDuplexDecoder(0);
            byte[] ackBuffer = new byte[1];
            var    tcs       = new TaskCompletionSource <bool>();
            var    result    = connection.BeginWrite(preamble.Array, preamble.Offset, preamble.Count, true, timeoutHelper.RemainingTime(), FramingDuplexSessionChannel.OnIoComplete, tcs);

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

            await tcs.Task;

            connection.EndWrite();

            if (_upgrade != null)
            {
                StreamUpgradeInitiator upgradeInitiator = _upgrade.CreateUpgradeInitiator(this.RemoteAddress, this.Via);

                upgradeInitiator.Open(timeoutHelper.RemainingTime());
                if (!ConnectionUpgradeHelper.InitiateUpgrade(upgradeInitiator, ref connection, _decoder, this, ref timeoutHelper))
                {
                    ConnectionUpgradeHelper.DecodeFramingFault(_decoder, connection, this.Via, MessageEncoder.ContentType, ref timeoutHelper);
                }

                SetRemoteSecurity(upgradeInitiator);
                upgradeInitiator.Close(timeoutHelper.RemainingTime());
                connection.Write(ClientDuplexEncoder.PreambleEndBytes, 0, ClientDuplexEncoder.PreambleEndBytes.Length, true, timeoutHelper.RemainingTime());
            }

            // read ACK
            tcs = new TaskCompletionSource <bool>();
            //ackBuffer

            result = connection.BeginRead(0, ackBuffer.Length, timeoutHelper.RemainingTime(), OnIoComplete, tcs);
            if (result == AsyncCompletionResult.Completed)
            {
                tcs.SetResult(true);
            }

            await tcs.Task;
            int ackBytesRead = connection.EndRead();

            Buffer.BlockCopy((Array)connection.AsyncReadBuffer, 0, (Array)ackBuffer, 0, ackBytesRead);

            if (!ConnectionUpgradeHelper.ValidatePreambleResponse(ackBuffer, ackBytesRead, _decoder, Via))
            {
                await ConnectionUpgradeHelper.DecodeFramingFaultAsync(_decoder, connection, Via,
                                                                      MessageEncoder.ContentType, timeoutHelper.RemainingTime());
            }

            return(connection);
        }
Beispiel #9
0
        private async Task <IConnection> SendPreambleAsync(IConnection connection, ArraySegment <byte> preamble, TimeSpan timeout)
        {
            var timeoutHelper = new TimeoutHelper(timeout);

            // initialize a new decoder
            _decoder = new ClientDuplexDecoder(0);
            byte[] ackBuffer = new byte[1];

            if (!await SendLock.WaitAsync(TimeoutHelper.ToMilliseconds(timeout)))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(
                                                                              SR.Format(SR.CloseTimedOut, timeout),
                                                                              TimeoutHelper.CreateEnterTimedOutException(timeout)));
            }

            try
            {
                await connection.WriteAsync(preamble.Array, preamble.Offset, preamble.Count, true, timeoutHelper.RemainingTime());

                if (_upgrade != null)
                {
                    StreamUpgradeInitiator upgradeInitiator = _upgrade.CreateUpgradeInitiator(this.RemoteAddress, this.Via);

                    await upgradeInitiator.OpenAsync(timeoutHelper.RemainingTime());

                    var connectionWrapper = new OutWrapper <IConnection>();
                    connectionWrapper.Value = connection;
                    bool upgradeInitiated = await ConnectionUpgradeHelper.InitiateUpgradeAsync(upgradeInitiator, connectionWrapper, _decoder, this, timeoutHelper.RemainingTime());

                    connection = connectionWrapper.Value;
                    if (!upgradeInitiated)
                    {
                        await ConnectionUpgradeHelper.DecodeFramingFaultAsync(_decoder, connection, this.Via, MessageEncoder.ContentType, timeoutHelper.RemainingTime());
                    }

                    SetRemoteSecurity(upgradeInitiator);
                    await upgradeInitiator.CloseAsync(timeoutHelper.RemainingTime());

                    await connection.WriteAsync(ClientDuplexEncoder.PreambleEndBytes, 0, ClientDuplexEncoder.PreambleEndBytes.Length, true, timeoutHelper.RemainingTime());
                }

                int ackBytesRead = await connection.ReadAsync(ackBuffer, 0, ackBuffer.Length, timeoutHelper.RemainingTime());

                if (!ConnectionUpgradeHelper.ValidatePreambleResponse(ackBuffer, ackBytesRead, _decoder, Via))
                {
                    await ConnectionUpgradeHelper.DecodeFramingFaultAsync(_decoder, connection, Via,
                                                                          MessageEncoder.ContentType, timeoutHelper.RemainingTime());
                }

                return(connection);
            }
            finally
            {
                SendLock.Release();
            }
        }
Beispiel #10
0
 public DecodeFailedUpgradeAsyncResult(ClientFramingDecoder decoder, IConnection connection, Uri via, string contentType, ref TimeoutHelper timeoutHelper, AsyncCallback callback, object state) : base(callback, state)
 {
     ConnectionUpgradeHelper.ValidateReadingFaultString(decoder);
     this.decoder       = decoder;
     this.connection    = connection;
     this.via           = via;
     this.contentType   = contentType;
     this.timeoutHelper = timeoutHelper;
     if (connection.BeginRead(0, Math.Min(0x100, connection.AsyncReadBufferSize), timeoutHelper.RemainingTime(), onReadFaultData, this) != AsyncReadResult.Queued)
     {
         this.CompleteReadFaultData();
     }
 }
Beispiel #11
0
            private bool HandlePreambleAck()
            {
                int count = this.connection.EndRead();

                if (!ConnectionUpgradeHelper.ValidatePreambleResponse(this.connection.AsyncReadBuffer, count, this.channel.decoder, this.channel.Via))
                {
                    IAsyncResult result = ConnectionUpgradeHelper.BeginDecodeFramingFault(this.channel.decoder, this.connection, this.channel.Via, this.channel.MessageEncoder.ContentType, ref this.timeoutHelper, Fx.ThunkCallback(new AsyncCallback(this.OnFailedPreamble)), this);
                    if (!result.CompletedSynchronously)
                    {
                        return(false);
                    }
                    ConnectionUpgradeHelper.EndDecodeFramingFault(result);
                }
                return(true);
            }
Beispiel #12
0
            private bool HandleInitiatorOpen(IAsyncResult result)
            {
                this.upgradeInitiator.EndOpen(result);
                if (onUpgrade == null)
                {
                    onUpgrade = Fx.ThunkCallback(new AsyncCallback(ClientFramingDuplexSessionChannel.SendPreambleAsyncResult.OnUpgrade));
                }
                IAsyncResult result2 = ConnectionUpgradeHelper.BeginInitiateUpgrade(this.channel, this.channel.RemoteAddress, this.connection, this.channel.decoder, this.upgradeInitiator, this.channel.MessageEncoder.ContentType, this.identityToImpersonate, this.timeoutHelper, onUpgrade, this);

                if (!result2.CompletedSynchronously)
                {
                    return(false);
                }
                return(this.HandleUpgrade(result2));
            }
Beispiel #13
0
        private IConnection SendPreamble(IConnection connection, ArraySegment <byte> preamble, ref TimeoutHelper timeoutHelper)
        {
            TimeSpan timeout = timeoutHelper.RemainingTime();

            if (!SendLock.Wait(TimeoutHelper.ToMilliseconds(timeout)))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(
                                                                              SR.Format(SR.CloseTimedOut, timeout),
                                                                              TimeoutHelper.CreateEnterTimedOutException(timeout)));
            }
            try
            {
                // initialize a new decoder
                _decoder = new ClientDuplexDecoder(0);
                byte[] ackBuffer = new byte[1];
                connection.Write(preamble.Array, preamble.Offset, preamble.Count, true, timeoutHelper.RemainingTime());

                if (_upgrade != null)
                {
                    StreamUpgradeInitiator upgradeInitiator = _upgrade.CreateUpgradeInitiator(this.RemoteAddress, this.Via);

                    upgradeInitiator.Open(timeoutHelper.RemainingTime());
                    if (!ConnectionUpgradeHelper.InitiateUpgrade(upgradeInitiator, ref connection, _decoder, this, ref timeoutHelper))
                    {
                        ConnectionUpgradeHelper.DecodeFramingFault(_decoder, connection, this.Via, MessageEncoder.ContentType, ref timeoutHelper);
                    }

                    SetRemoteSecurity(upgradeInitiator);
                    upgradeInitiator.Close(timeoutHelper.RemainingTime());
                    connection.Write(ClientDuplexEncoder.PreambleEndBytes, 0, ClientDuplexEncoder.PreambleEndBytes.Length, true, timeoutHelper.RemainingTime());
                }

                // read ACK
                int ackBytesRead = connection.Read(ackBuffer, 0, ackBuffer.Length, timeoutHelper.RemainingTime());
                if (!ConnectionUpgradeHelper.ValidatePreambleResponse(ackBuffer, ackBytesRead, _decoder, Via))
                {
                    ConnectionUpgradeHelper.DecodeFramingFault(_decoder, connection, Via,
                                                               MessageEncoder.ContentType, ref timeoutHelper);
                }

                return(connection);
            }
            finally
            {
                SendLock.Release();
            }
        }
Beispiel #14
0
        private IConnection SendPreamble(IConnection connection, ArraySegment <byte> preamble, ref TimeoutHelper timeoutHelper)
        {
            // initialize a new decoder
            _decoder = new ClientDuplexDecoder(0);
            byte[] ackBuffer = new byte[1];
            connection.Write(preamble.Array, preamble.Offset, preamble.Count, true, timeoutHelper.RemainingTime());

            // read ACK
            int ackBytesRead = connection.Read(ackBuffer, 0, ackBuffer.Length, timeoutHelper.RemainingTime());

            if (!ConnectionUpgradeHelper.ValidatePreambleResponse(ackBuffer, ackBytesRead, _decoder, Via))
            {
                ConnectionUpgradeHelper.DecodeFramingFault(_decoder, connection, Via,
                                                           MessageEncoder.ContentType, ref timeoutHelper);
            }

            return(connection);
        }
        internal IConnection SendPreamble(IConnection connection, ref TimeoutHelper timeoutHelper,
                                          ClientFramingDecoder decoder, out SecurityMessageProperty remoteSecurity)
        {
            connection.Write(Preamble, 0, Preamble.Length, true, timeoutHelper.RemainingTime());

            if (_upgrade != null)
            {
                IStreamUpgradeChannelBindingProvider channelBindingProvider = _upgrade.GetProperty <IStreamUpgradeChannelBindingProvider>();

                StreamUpgradeInitiator upgradeInitiator = _upgrade.CreateUpgradeInitiator(this.RemoteAddress, this.Via);

                if (!ConnectionUpgradeHelper.InitiateUpgrade(upgradeInitiator, ref connection, decoder,
                                                             this, ref timeoutHelper))
                {
                    ConnectionUpgradeHelper.DecodeFramingFault(decoder, connection, Via, _messageEncoder.ContentType, ref timeoutHelper);
                }

#if FEATURE_CORECLR // ExtendedProtection
                if (channelBindingProvider != null && channelBindingProvider.IsChannelBindingSupportEnabled)
                {
                    _channelBindingToken = channelBindingProvider.GetChannelBinding(upgradeInitiator, ChannelBindingKind.Endpoint);
                }
#endif // FEATURE_CORECLR // ExtendedProtection

                remoteSecurity = StreamSecurityUpgradeInitiator.GetRemoteSecurity(upgradeInitiator);

                connection.Write(ClientSingletonEncoder.PreambleEndBytes, 0,
                                 ClientSingletonEncoder.PreambleEndBytes.Length, true, timeoutHelper.RemainingTime());
            }
            else
            {
                remoteSecurity = null;
            }

            // read ACK
            byte[] ackBuffer    = new byte[1];
            int    ackBytesRead = connection.Read(ackBuffer, 0, ackBuffer.Length, timeoutHelper.RemainingTime());
            if (!ConnectionUpgradeHelper.ValidatePreambleResponse(ackBuffer, ackBytesRead, decoder, this.Via))
            {
                ConnectionUpgradeHelper.DecodeFramingFault(decoder, connection, Via, _messageEncoder.ContentType, ref timeoutHelper);
            }

            return(connection);
        }
Beispiel #16
0
                private bool HandlePreambleAck()
                {
                    int count = this.connection.EndRead();

                    if (!ConnectionUpgradeHelper.ValidatePreambleResponse(this.connection.AsyncReadBuffer, count, this.decoder, this.channel.Via))
                    {
                        if (onFailedUpgrade == null)
                        {
                            onFailedUpgrade = Fx.ThunkCallback(new AsyncCallback(StreamedFramingRequestChannel.StreamedConnectionPoolHelper.SendPreambleAsyncResult.OnFailedUpgrade));
                        }
                        IAsyncResult result = ConnectionUpgradeHelper.BeginDecodeFramingFault(this.decoder, this.connection, this.channel.Via, this.channel.messageEncoder.ContentType, ref this.timeoutHelper, onFailedUpgrade, this);
                        if (!result.CompletedSynchronously)
                        {
                            return(false);
                        }
                        ConnectionUpgradeHelper.EndDecodeFramingFault(result);
                    }
                    return(true);
                }
Beispiel #17
0
            private bool HandleUpgrade(IAsyncResult result)
            {
                this.connection = ConnectionUpgradeHelper.EndInitiateUpgrade(result);
                if ((this.channelBindingProvider != null) && this.channelBindingProvider.IsChannelBindingSupportEnabled)
                {
                    this.channel.SetChannelBinding(this.channelBindingProvider.GetChannelBinding(this.upgradeInitiator, ChannelBindingKind.Endpoint));
                }
                this.channel.SetRemoteSecurity(this.upgradeInitiator);
                if (onUpgradeInitiatorClose == null)
                {
                    onUpgradeInitiatorClose = Fx.ThunkCallback(new AsyncCallback(ClientFramingDuplexSessionChannel.SendPreambleAsyncResult.OnUpgradeInitiatorClose));
                }
                IAsyncResult result2 = this.upgradeInitiator.BeginClose(this.timeoutHelper.RemainingTime(), onUpgradeInitiatorClose, this);

                if (!result2.CompletedSynchronously)
                {
                    return(false);
                }
                return(this.HandleInitiatorClose(result2));
            }
Beispiel #18
0
 private void OnFailedPreamble(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         Exception exception = null;
         try
         {
             ConnectionUpgradeHelper.EndDecodeFramingFault(result);
         }
         catch (Exception exception2)
         {
             if (Fx.IsFatal(exception2))
             {
                 throw;
             }
             exception = exception2;
         }
         base.Complete(false, exception);
     }
 }
Beispiel #19
0
 private static void OnFailedUpgrade(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         StreamedFramingRequestChannel.StreamedConnectionPoolHelper.SendPreambleAsyncResult asyncState = (StreamedFramingRequestChannel.StreamedConnectionPoolHelper.SendPreambleAsyncResult)result.AsyncState;
         Exception exception = null;
         try
         {
             ConnectionUpgradeHelper.EndDecodeFramingFault(result);
         }
         catch (Exception exception2)
         {
             if (Fx.IsFatal(exception2))
             {
                 throw;
             }
             exception = exception2;
         }
         asyncState.Complete(false, exception);
     }
 }
Beispiel #20
0
 private static void OnFailedUpgrade(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         ConnectionUpgradeHelper.InitiateUpgradeAsyncResult asyncState = (ConnectionUpgradeHelper.InitiateUpgradeAsyncResult)result.AsyncState;
         Exception exception = null;
         try
         {
             ConnectionUpgradeHelper.EndDecodeFramingFault(result);
         }
         catch (Exception exception2)
         {
             if (Fx.IsFatal(exception2))
             {
                 throw;
             }
             exception = exception2;
         }
         asyncState.Complete(false, exception);
     }
 }
Beispiel #21
0
                private bool HandleWritePreamble(IAsyncResult result)
                {
                    this.connection.EndWrite(result);
                    if (this.channel.upgrade == null)
                    {
                        return(this.ReadPreambleAck());
                    }
                    this.channelBindingProvider = this.channel.upgrade.GetProperty <IStreamUpgradeChannelBindingProvider>();
                    this.upgradeInitiator       = this.channel.upgrade.CreateUpgradeInitiator(this.channel.RemoteAddress, this.channel.Via);
                    if (onUpgrade == null)
                    {
                        onUpgrade = Fx.ThunkCallback(new AsyncCallback(StreamedFramingRequestChannel.StreamedConnectionPoolHelper.SendPreambleAsyncResult.OnUpgrade));
                    }
                    IAsyncResult result2 = ConnectionUpgradeHelper.BeginInitiateUpgrade(this.channel.settings, this.channel.RemoteAddress, this.connection, this.decoder, this.upgradeInitiator, this.channel.messageEncoder.ContentType, null, this.timeoutHelper, onUpgrade, this);

                    if (!result2.CompletedSynchronously)
                    {
                        return(false);
                    }
                    return(this.HandleUpgrade(result2));
                }
Beispiel #22
0
                private bool HandleUpgrade(IAsyncResult result)
                {
                    this.connection = ConnectionUpgradeHelper.EndInitiateUpgrade(result);
                    if ((this.channelBindingProvider != null) && this.channelBindingProvider.IsChannelBindingSupportEnabled)
                    {
                        this.channel.channelBindingToken = this.channelBindingProvider.GetChannelBinding(this.upgradeInitiator, ChannelBindingKind.Endpoint);
                    }
                    this.remoteSecurity   = StreamSecurityUpgradeInitiator.GetRemoteSecurity(this.upgradeInitiator);
                    this.upgradeInitiator = null;
                    if (onWritePreambleEnd == null)
                    {
                        onWritePreambleEnd = Fx.ThunkCallback(new AsyncCallback(StreamedFramingRequestChannel.StreamedConnectionPoolHelper.SendPreambleAsyncResult.OnWritePreambleEnd));
                    }
                    IAsyncResult result2 = this.connection.BeginWrite(ClientSingletonEncoder.PreambleEndBytes, 0, ClientSingletonEncoder.PreambleEndBytes.Length, true, this.timeoutHelper.RemainingTime(), onWritePreambleEnd, this);

                    if (!result2.CompletedSynchronously)
                    {
                        return(false);
                    }
                    this.connection.EndWrite(result2);
                    return(this.ReadPreambleAck());
                }
Beispiel #23
0
            private bool CompleteReadUpgradeResponse()
            {
                int count = this.connection.EndRead();

                if (!ConnectionUpgradeHelper.ValidateUpgradeResponse(this.connection.AsyncReadBuffer, count, this.decoder))
                {
                    if (onFailedUpgrade == null)
                    {
                        onFailedUpgrade = Fx.ThunkCallback(new AsyncCallback(ConnectionUpgradeHelper.InitiateUpgradeAsyncResult.OnFailedUpgrade));
                    }
                    IAsyncResult result = ConnectionUpgradeHelper.BeginDecodeFramingFault(this.decoder, this.connection, this.remoteAddress.Uri, this.contentType, ref this.timeoutHelper, onFailedUpgrade, this);
                    if (result.CompletedSynchronously)
                    {
                        ConnectionUpgradeHelper.EndDecodeFramingFault(result);
                    }
                    return(result.CompletedSynchronously);
                }
                this.connectionStream = new ConnectionStream(this.connection, this.defaultTimeouts);
                IAsyncResult result2 = null;
                WindowsImpersonationContext context = (this.identityToImpersonate == null) ? null : this.identityToImpersonate.Impersonate();

                try
                {
                    using (context)
                    {
                        result2 = this.upgradeInitiator.BeginInitiateUpgrade(this.connectionStream, onInitiateUpgrade, this);
                    }
                }
                catch
                {
                    throw;
                }
                if (!result2.CompletedSynchronously)
                {
                    return(false);
                }
                this.CompleteUpgrade(result2);
                return(true);
            }
        internal async Task <IConnection> SendPreambleAsync(IConnection connection, TimeoutHelper timeoutHelper, ClientFramingDecoder decoder)
        {
            await connection.WriteAsync(Preamble, 0, Preamble.Length, true, timeoutHelper.RemainingTime());

            if (_upgrade != null)
            {
                StreamUpgradeInitiator upgradeInitiator = _upgrade.CreateUpgradeInitiator(this.RemoteAddress, this.Via);

                await upgradeInitiator.OpenAsync(timeoutHelper.RemainingTime());

                var connectionWrapper = new OutWrapper <IConnection>();
                connectionWrapper.Value = connection;
                bool upgradeInitiated = await ConnectionUpgradeHelper.InitiateUpgradeAsync(upgradeInitiator, connectionWrapper, decoder, this, timeoutHelper.RemainingTime());

                connection = connectionWrapper.Value;
                if (!upgradeInitiated)
                {
                    await ConnectionUpgradeHelper.DecodeFramingFaultAsync(decoder, connection, this.Via, _messageEncoder.ContentType, timeoutHelper.RemainingTime());
                }

                await upgradeInitiator.CloseAsync(timeoutHelper.RemainingTime());

                await connection.WriteAsync(ClientSingletonEncoder.PreambleEndBytes, 0, ClientSingletonEncoder.PreambleEndBytes.Length, true, timeoutHelper.RemainingTime());
            }

            byte[] ackBuffer    = new byte[1];
            int    ackBytesRead = await connection.ReadAsync(ackBuffer, 0, ackBuffer.Length, timeoutHelper.RemainingTime());

            if (!ConnectionUpgradeHelper.ValidatePreambleResponse(ackBuffer, ackBytesRead, decoder, Via))
            {
                await ConnectionUpgradeHelper.DecodeFramingFaultAsync(decoder, connection, Via,
                                                                      _messageEncoder.ContentType, timeoutHelper.RemainingTime());
            }

            return(connection);
        }