Ejemplo n.º 1
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;
                    }
                }
            }
        }
 public DataExchangeChannelFaultHelper(StreamBufferWritePump pump)
 {
     this.pump = pump;
 }