Example #1
0
        void MultiplexPumpCompleted(IAsyncResult a)
        {
            try
            {
                try
                {
                    Pump.EndRunPump(a);
                }
                catch (Exception ex)
                {
                    Trace.TraceError("Multiplex pump failed: {0}", ex.Message);
                }

                lock (connectLock)
                {
                    if (dataChannel != null && dataChannel.State == CommunicationState.Opened)
                    {
                        dataChannel.Disconnect();
                        dataChannel.Close();
                    }
                    dataChannel = null;
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("Error closing data channel: {0}", ex.Message);
                lock (connectLock)
                {
                    if (dataChannel != null && dataChannel.State == CommunicationState.Opened)
                    {
                        dataChannel.Abort();
                    }
                    dataChannel = null;
                }
            }
            finally
            {
                foreach (MultiplexedTcpConnection connection in new List <MultiplexedTcpConnection>(connections.Values))
                {
                    try
                    {
                        connections.Remove(connection.Id);
                        connection.Dispose();
                    }
                    catch (Exception ex1)
                    {
                        Trace.TraceError("Error shutting down multiplex connection: {0}", ex1.Message);
                    }
                }
            }
        }
Example #2
0
 private void WriteToDataChannel(byte[] b, int o, int s)
 {
     lock (connectLock)
     {
         try
         {
             dataChannel.Write(new TransferBuffer(b, o, s));
         }
         catch (Exception ex)
         {
             Trace.TraceError("Failed writing to data channel: {0}", ex.Message);
             if (dataChannel != null)
             {
                 dataChannel.Abort();
                 dataChannel = null;
             }
             throw;
         }
     }
 }
Example #3
0
        void EnsureConnection()
        {
            lock (connectLock)
            {
                if (this.dataChannel == null || this.dataChannel.State != CommunicationState.Opened)
                {
                    this.multiplexedOutputStream = new ThrottledQueueBufferedStream(5);

                    QueueBufferedStream multiplexedInputStream = new QueueBufferedStream();
                    this.dataChannelFactory = CreateDataChannelFactory(multiplexedInputStream);
                    this.dataChannelFactory.Open();

                    this.dataChannel = dataChannelFactory.CreateChannel(new EndpointAddress("sb:"), endpointVia);

                    try
                    {
                        this.dataChannel.Open();
                        this.dataChannel.Closed  += DataChannelClosed;
                        this.dataChannel.Faulted += DataChannelClosed;

                        IHybridConnectionStatus status = dataChannel.GetProperty <IHybridConnectionStatus>();
                        if (status != null)
                        {
                            status.ConnectionStateChanged += (o, e) =>
                            {
                                Trace.TraceInformation("Data channel upgraded to direct connection.");
                            };
                        }

                        this.dataChannel.Connect("tcp:" + toPort.ToString());

                        this.inputPump = new MultiplexConnectionInputPump(multiplexedInputStream.Read, CorrelateConnection, null);
                        this.inputPump.Run(false);

                        this.outputPump = new StreamBufferWritePump(multiplexedOutputStream, WriteToDataChannel);
                        this.dataChannel.Extensions.Add(new DataExchangeChannelFaultHelper(outputPump));
                        this.outputPump.BeginRunPump(MultiplexPumpCompleted, null);

                        return;
                    }
                    catch (AuthorizationFailedException af)
                    {
                        Trace.TraceError("Authorization failed: {0}", af.Message);
                        if (dataChannel != null)
                        {
                            dataChannel.Abort();
                            dataChannel = null;
                        }
                        throw;
                    }
                    catch (Exception ex)
                    {
                        this.dataChannelFactory.Abort();
                        this.dataChannelFactory = null;

                        Trace.TraceError("Unable to establish data channel: {0}", ex.Message);
                        if (dataChannel != null)
                        {
                            dataChannel.Abort();
                            dataChannel = null;
                        }
                        throw;
                    }
                }
            }
        }
 private void WriteToDataChannel(byte[] b, int o, int s)
 {
     lock (connectLock)
     {
         try
         {
             dataChannel.Write(new TransferBuffer(b, o, s));
         }
         catch (Exception ex)
         {
             Trace.TraceError("Failed writing to data channel: {0}", ex.Message);
             if (dataChannel != null)
             {
                 dataChannel.Abort();
                 dataChannel = null;
             }
             throw;
         }
     }
 }
        void MultiplexPumpCompleted(IAsyncResult a)
        {
            try
            {
                try
                {
                    Pump.EndRunPump(a);
                }
                catch (Exception ex)
                {
                    Trace.TraceError("Multiplex pump failed: {0}", ex.Message);
                }

                lock (connectLock)
                {
                    if (dataChannel != null && dataChannel.State == CommunicationState.Opened)
                    {
                        dataChannel.Disconnect();
                        dataChannel.Close();
                    }
                    dataChannel = null;
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("Error closing data channel: {0}", ex.Message);
                lock (connectLock)
                {
                    if (dataChannel != null && dataChannel.State == CommunicationState.Opened)
                    {
                        dataChannel.Abort();
                    }
                    dataChannel = null;
                }
            }
            finally
            {
                foreach (MultiplexedPipeConnection connection in new List<MultiplexedPipeConnection>(connections.Values))
                {
                    try
                    {
                        connections.Remove(connection.Id);
                        connection.Dispose();
                    }
                    catch (Exception ex1)
                    {
                        Trace.TraceError("Error shutting down multiplex connection: {0}", ex1.Message);
                    }
                }
            }
        }
        void EnsureConnection()
        {
            lock (connectLock)
            {
                if (this.dataChannel == null || this.dataChannel.State != CommunicationState.Opened)
                {
                    this.multiplexedOutputStream = new ThrottledQueueBufferedStream(10);

                    Microsoft.Samples.ServiceBus.Connections.QueueBufferedStream multiplexedInputStream = new Microsoft.Samples.ServiceBus.Connections.QueueBufferedStream();
                    this.dataChannelFactory = CreateDataChannelFactory(multiplexedInputStream);
                    this.dataChannelFactory.Open();

                    this.dataChannel = dataChannelFactory.CreateChannel(new EndpointAddress("sb:"), endpointVia);

                    try
                    {
                        this.dataChannel.Open();
                        this.dataChannel.Closed += DataChannelClosed;
                        this.dataChannel.Faulted += DataChannelClosed;

                        IHybridConnectionStatus status = dataChannel.GetProperty<IHybridConnectionStatus>();
                        if (status != null)
                        {
                            status.ConnectionStateChanged += (o, e) =>
                            {
                                Trace.TraceInformation("Data channel upgraded to direct connection.");
                            };
                        }

                        this.dataChannel.Connect("np:"+toPipe);

                        this.inputPump = new MultiplexConnectionInputPump(multiplexedInputStream.Read, CorrelateConnection, null);
                        this.inputPump.Run(false);

                        this.outputPump = new StreamBufferWritePump(multiplexedOutputStream, WriteToDataChannel);
                        this.dataChannel.Extensions.Add(new DataExchangeChannelFaultHelper(outputPump));
                        this.outputPump.BeginRunPump(MultiplexPumpCompleted, null);

                        return;
                    }
                    catch (AuthorizationFailedException af)
                    {
                        Trace.TraceError("Authorization failed: {0}", af.Message);
                        if (dataChannel != null)
                        {
                            dataChannel.Abort();
                            dataChannel = null;
                        }
                        throw;
                    }
                    catch (Exception ex)
                    {
                        this.dataChannelFactory.Abort();
                        this.dataChannelFactory = null;

                        Trace.TraceError("Unable to establish data channel: {0}", ex.Message);
                        if (dataChannel != null)
                        {
                            dataChannel.Abort();
                            dataChannel = null;
                        }
                        throw;
                    }
                }
            }
        }