Ejemplo n.º 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);
        }
Ejemplo n.º 2
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);
        }
 public ClientDuplexConnectionReader(ClientFramingDuplexSessionChannel channel, IConnection connection, ClientDuplexDecoder decoder, IConnectionOrientedTransportFactorySettings settings, MessageEncoder messageEncoder) : base(connection, null, 0, 0, null)
 {
     this.decoder        = decoder;
     this.maxBufferSize  = settings.MaxBufferSize;
     this.bufferManager  = settings.BufferManager;
     this.messageEncoder = messageEncoder;
     this.channel        = channel;
 }
 public ClientDuplexConnectionReader(ClientFramingDuplexSessionChannel channel, IConnection connection, ClientDuplexDecoder decoder, IConnectionOrientedTransportFactorySettings settings, MessageEncoder messageEncoder) : base(connection, null, 0, 0, null)
 {
     this.decoder = decoder;
     this.maxBufferSize = settings.MaxBufferSize;
     this.bufferManager = settings.BufferManager;
     this.messageEncoder = messageEncoder;
     this.channel = channel;
 }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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();
            }
        }
Ejemplo n.º 7
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();
            }
        }
Ejemplo n.º 8
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);
        }