Example #1
0
        public static async Task <bool> InitiateUpgradeAsync(StreamUpgradeInitiator upgradeInitiator, OutWrapper <IConnection> connectionWrapper,
                                                             ClientFramingDecoder decoder, IDefaultCommunicationTimeouts defaultTimeouts, TimeSpan timeout)
        {
            IConnection connection         = connectionWrapper.Value;
            string      upgradeContentType = upgradeInitiator.GetNextUpgrade();

            while (upgradeContentType != null)
            {
                EncodedUpgrade encodedUpgrade = new EncodedUpgrade(upgradeContentType);
                // write upgrade request framing for synchronization
                await connection.WriteAsync(encodedUpgrade.EncodedBytes, 0, encodedUpgrade.EncodedBytes.Length, true, timeout);

                byte[] buffer = new byte[1];

                // read upgrade response framing
                int size = await connection.ReadAsync(buffer, 0, buffer.Length, timeout);

                if (!ValidateUpgradeResponse(buffer, size, decoder)) // we have a problem
                {
                    return(false);
                }

                // initiate wire upgrade
                ConnectionStream connectionStream = new ConnectionStream(connection, defaultTimeouts);
                Stream           upgradedStream   = await upgradeInitiator.InitiateUpgradeAsync(connectionStream);

                // and re-wrap connection
                connection = new StreamConnection(upgradedStream, connectionStream);
                connectionWrapper.Value = connection;

                upgradeContentType = upgradeInitiator.GetNextUpgrade();
            }

            return(true);
        }
Example #2
0
        public static bool InitiateUpgrade(StreamUpgradeInitiator upgradeInitiator, ref IConnection connection,
                                           ClientFramingDecoder decoder, IDefaultCommunicationTimeouts defaultTimeouts, ref TimeoutHelper timeoutHelper)
        {
            string upgradeContentType = upgradeInitiator.GetNextUpgrade();

            while (upgradeContentType != null)
            {
                EncodedUpgrade encodedUpgrade = new EncodedUpgrade(upgradeContentType);
                // write upgrade request framing for synchronization
                connection.Write(encodedUpgrade.EncodedBytes, 0, encodedUpgrade.EncodedBytes.Length, true, timeoutHelper.RemainingTime());
                byte[] buffer = new byte[1];

                // read upgrade response framing
                int size = connection.Read(buffer, 0, buffer.Length, timeoutHelper.RemainingTime());

                if (!ValidateUpgradeResponse(buffer, size, decoder)) // we have a problem
                {
                    return(false);
                }

                // initiate wire upgrade
                ConnectionStream connectionStream = new ConnectionStream(connection, defaultTimeouts);
                Stream           upgradedStream   = upgradeInitiator.InitiateUpgrade(connectionStream);

                // and re-wrap connection
                connection = new StreamConnection(upgradedStream, connectionStream);

                upgradeContentType = upgradeInitiator.GetNextUpgrade();
            }

            return(true);
        }
Example #3
0
        public StreamConnection(Stream stream, ConnectionStream innerStream)
        {
            Contract.Assert(stream != null, "StreamConnection: Stream cannot be null.");
            Contract.Assert(innerStream != null, "StreamConnection: Inner stream cannot be null.");

            _stream      = stream;
            _innerStream = innerStream;

            _onRead  = new Action <Task <int>, object>(OnRead);
            _onWrite = new Action <Task, object>(OnWrite);
        }
        public static IConnection UpgradeConnection(IConnection connection, StreamUpgradeAcceptor upgradeAcceptor, IDefaultCommunicationTimeouts defaultTimeouts)
        {
            ConnectionStream stream  = new ConnectionStream(connection, defaultTimeouts);
            Stream           stream2 = upgradeAcceptor.AcceptUpgrade(stream);

            if ((upgradeAcceptor is StreamSecurityUpgradeAcceptor) && DiagnosticUtility.ShouldTraceInformation)
            {
                TraceUtility.TraceEvent(TraceEventType.Information, 0x4002e, System.ServiceModel.SR.GetString("TraceCodeStreamSecurityUpgradeAccepted"), new StringTraceRecord("Type", upgradeAcceptor.GetType().ToString()), connection, null);
            }
            return(new StreamConnection(stream2, stream));
        }
Example #5
0
        public StreamConnection(Stream stream, ConnectionStream innerStream)
        {
            Fx.Assert(stream != null, "StreamConnection: Stream cannot be null.");
            Fx.Assert(innerStream != null, "StreamConnection: Inner stream cannot be null.");

            this.stream      = stream;
            this.innerStream = innerStream;

            onRead  = Fx.ThunkCallback(new AsyncCallback(OnRead));
            onWrite = Fx.ThunkCallback(new AsyncCallback(OnWrite));
        }
        public static IConnection UpgradeConnection(IConnection connection, StreamUpgradeAcceptor upgradeAcceptor, IDefaultCommunicationTimeouts defaultTimeouts)
        {
            ConnectionStream connectionStream = new ConnectionStream(connection, defaultTimeouts);
            Stream           stream           = upgradeAcceptor.AcceptUpgrade(connectionStream);

            if (upgradeAcceptor is StreamSecurityUpgradeAcceptor)
            {
                if (DiagnosticUtility.ShouldTraceInformation)
                {
                    TraceUtility.TraceEvent(TraceEventType.Information,
                                            TraceCode.StreamSecurityUpgradeAccepted, SR.GetString(SR.TraceCodeStreamSecurityUpgradeAccepted),
                                            new StringTraceRecord("Type", upgradeAcceptor.GetType().ToString()), connection, null);
                }
            }

            return(new StreamConnection(stream, connectionStream));
        }
            public UpgradeConnectionAsyncResult(IConnection connection, StreamUpgradeAcceptor upgradeAcceptor, IDefaultCommunicationTimeouts defaultTimeouts, AsyncCallback callback, object state) : base(callback, state)
            {
                this.upgradeAcceptor  = upgradeAcceptor;
                this.connectionStream = new ConnectionStream(connection, defaultTimeouts);
                bool         flag   = false;
                IAsyncResult result = upgradeAcceptor.BeginAcceptUpgrade(this.connectionStream, onAcceptUpgrade, this);

                if (result.CompletedSynchronously)
                {
                    this.CompleteAcceptUpgrade(result);
                    flag = true;
                }
                if (flag)
                {
                    base.Complete(true);
                }
            }
Example #8
0
 public static bool InitiateUpgrade(StreamUpgradeInitiator upgradeInitiator, ref IConnection connection, ClientFramingDecoder decoder, IDefaultCommunicationTimeouts defaultTimeouts, ref TimeoutHelper timeoutHelper)
 {
     for (string str = upgradeInitiator.GetNextUpgrade(); str != null; str = upgradeInitiator.GetNextUpgrade())
     {
         EncodedUpgrade upgrade = new EncodedUpgrade(str);
         connection.Write(upgrade.EncodedBytes, 0, upgrade.EncodedBytes.Length, true, timeoutHelper.RemainingTime());
         byte[] buffer = new byte[1];
         int    count  = connection.Read(buffer, 0, buffer.Length, timeoutHelper.RemainingTime());
         if (!ValidateUpgradeResponse(buffer, count, decoder))
         {
             return(false);
         }
         ConnectionStream stream  = new ConnectionStream(connection, defaultTimeouts);
         Stream           stream2 = upgradeInitiator.InitiateUpgrade(stream);
         connection = new StreamConnection(stream2, stream);
     }
     return(true);
 }
 public static bool InitiateUpgrade(StreamUpgradeInitiator upgradeInitiator, ref IConnection connection, ClientFramingDecoder decoder, IDefaultCommunicationTimeouts defaultTimeouts, ref TimeoutHelper timeoutHelper)
 {
     for (string str = upgradeInitiator.GetNextUpgrade(); str != null; str = upgradeInitiator.GetNextUpgrade())
     {
         EncodedUpgrade upgrade = new EncodedUpgrade(str);
         connection.Write(upgrade.EncodedBytes, 0, upgrade.EncodedBytes.Length, true, timeoutHelper.RemainingTime());
         byte[] buffer = new byte[1];
         int count = connection.Read(buffer, 0, buffer.Length, timeoutHelper.RemainingTime());
         if (!ValidateUpgradeResponse(buffer, count, decoder))
         {
             return false;
         }
         ConnectionStream stream = new ConnectionStream(connection, defaultTimeouts);
         Stream stream2 = upgradeInitiator.InitiateUpgrade(stream);
         connection = new StreamConnection(stream2, stream);
     }
     return true;
 }
Example #10
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);
            }
Example #11
0
        public StreamConnection(Stream stream, ConnectionStream innerStream)
        {
            Contract.Assert(stream != null, "StreamConnection: Stream cannot be null.");
            Contract.Assert(innerStream != null, "StreamConnection: Inner stream cannot be null.");

            _stream = stream;
            _innerStream = innerStream;

            _onRead = new Action<Task<int>, object>(OnRead);
            _onWrite = new Action<Task, object>(OnWrite);
        }
            public UpgradeConnectionAsyncResult(IConnection connection,
                StreamUpgradeAcceptor upgradeAcceptor, IDefaultCommunicationTimeouts defaultTimeouts,
                AsyncCallback callback, object state)
                : base(callback, state)
            {
                this.upgradeAcceptor = upgradeAcceptor;
                this.connectionStream = new ConnectionStream(connection, defaultTimeouts);
                bool completeSelf = false;

                IAsyncResult result = upgradeAcceptor.BeginAcceptUpgrade(connectionStream, onAcceptUpgrade, this);

                if (result.CompletedSynchronously)
                {
                    CompleteAcceptUpgrade(result);
                    completeSelf = true;
                }

                if (completeSelf)
                {
                    base.Complete(true);
                }
            }
        public static IConnection UpgradeConnection(IConnection connection, StreamUpgradeAcceptor upgradeAcceptor, IDefaultCommunicationTimeouts defaultTimeouts)
        {
            ConnectionStream connectionStream = new ConnectionStream(connection, defaultTimeouts);
            Stream stream = upgradeAcceptor.AcceptUpgrade(connectionStream);
            if (upgradeAcceptor is StreamSecurityUpgradeAcceptor)
            {
                if (DiagnosticUtility.ShouldTraceInformation)
                {
                    TraceUtility.TraceEvent(TraceEventType.Information,
                        TraceCode.StreamSecurityUpgradeAccepted, SR.GetString(SR.TraceCodeStreamSecurityUpgradeAccepted),
                        new StringTraceRecord("Type", upgradeAcceptor.GetType().ToString()), connection, null);
                }
            }

            return new StreamConnection(stream, connectionStream);
        }
 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;
 }
Example #15
0
 public StreamConnection(System.IO.Stream stream, ConnectionStream innerStream)
 {
     this.stream      = stream;
     this.innerStream = innerStream;
     this.onRead      = Fx.ThunkCallback(new AsyncCallback(this.OnRead));
 }
Example #16
0
        public StreamConnection(Stream stream, ConnectionStream innerStream)
        {
            Fx.Assert(stream != null, "StreamConnection: Stream cannot be null.");
            Fx.Assert(innerStream != null, "StreamConnection: Inner stream cannot be null.");

            this.stream = stream;
            this.innerStream = innerStream;

            onRead = Fx.ThunkCallback(new AsyncCallback(OnRead));
            onWrite = Fx.ThunkCallback(new AsyncCallback(OnWrite));
        }
 public StreamConnection(System.IO.Stream stream, ConnectionStream innerStream)
 {
     this.stream = stream;
     this.innerStream = innerStream;
     this.onRead = Fx.ThunkCallback(new AsyncCallback(this.OnRead));
 }