void StreamAccepted(IAsyncResult asyncResult)
        {
            try
            {
                StreamConnection streamConnection = streamServerHost.EndAccept(asyncResult);
                if (streamConnection != null)
                {
                    streamServerHost.BeginAccept(StreamAccepted, null);
                    if (streamConnection.ConnectionInfo.StartsWith("tcp:", StringComparison.OrdinalIgnoreCase))
                    {
                        int port;

                        if (!int.TryParse(streamConnection.ConnectionInfo.Substring(4), out port))
                        {
                            try
                            {
                                streamConnection.Stream.Close();
                            }
                            catch (Exception ex)
                            {
                                Trace.TraceError("Error closing stream: {0}", ex.Message);
                            }
                            return;
                        }
                        else
                        {
                            bool portAllowed = noPortConstraints;
                            Trace.TraceInformation("Incoming connection for port {0}", port);
                            if (!portAllowed)
                            {
                                for (int i = 0; i < allowedPorts.Count; i++)
                                {
                                    if (port == allowedPorts[i])
                                    {
                                        portAllowed = true;
                                        break;
                                    }
                                }
                            }
                            if (!portAllowed)
                            {
                                Trace.TraceWarning("Incoming connection for port {0} not permitted", port);
                                try
                                {
                                    streamConnection.Stream.Close();
                                }
                                catch (Exception ex)
                                {
                                    Trace.TraceError("Error closing stream: {0}", ex.Message);
                                }
                                return;
                            }
                        }
                    }
                    else if (streamConnection.ConnectionInfo.StartsWith("np:", StringComparison.OrdinalIgnoreCase))
                    {
                        string pipeName = streamConnection.ConnectionInfo.Substring(3);
                        Trace.TraceInformation("Incoming connection for pipe {0}", pipeName);

                        bool pipeAllowed = noPipeConstraints;
                        if (!pipeAllowed)
                        {
                            for (int i = 0; i < allowedPipes.Count; i++)
                            {
                                if (pipeName.Equals(allowedPipes[i], StringComparison.OrdinalIgnoreCase))
                                {
                                    pipeAllowed = true;
                                    break;
                                }
                            }
                        }
                        if (!pipeAllowed)
                        {
                            Trace.TraceWarning("Incoming connection for pipe {0} not permitted", pipeName);
                            try
                            {
                                streamConnection.Stream.Close();
                            }
                            catch (Exception ex)
                            {
                                Trace.TraceError("Error closing stream: {0}", ex.Message);
                            }
                            return;
                        }
                    }
                    else
                    {
                        Trace.TraceError("Unable to handle connection for {0}", streamConnection.ConnectionInfo);
                        streamConnection.Stream.Close();
                        return;
                    }

                    MultiplexConnectionInputPump connectionPump =
                        new MultiplexConnectionInputPump(streamConnection.Stream.Read,
                                                         OnCreateConnection,
                                                         streamConnection);
                    connectionPump.Run(false);
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("Error accepting connection: {0}", ex.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;
                    }
                }
            }
        }
Пример #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;
                    }
                }
            }
        }
        void StreamAccepted(IAsyncResult asyncResult)
        {
            try
            {
                StreamConnection streamConnection = streamServerHost.EndAccept(asyncResult);
                if (streamConnection != null)
                {
                    streamServerHost.BeginAccept(StreamAccepted, null);
                    if (streamConnection.ConnectionInfo.StartsWith("tcp:", StringComparison.OrdinalIgnoreCase))
                    {
                        int port;

                        if (!int.TryParse(streamConnection.ConnectionInfo.Substring(4), out port))
                        {
                            try
                            {
                                streamConnection.Stream.Close();
                            }
                            catch (Exception ex)
                            {
                                Trace.TraceError("Error closing stream: {0}", ex.Message);
                            }
                            return;
                        }
                        else
                        {
                            bool portAllowed = noPortConstraints;
                            Trace.TraceInformation("Incoming connection for port {0}", port);
                            if (!portAllowed)
                            {
                                for (int i = 0; i < allowedPorts.Count; i++)
                                {
                                    if (port == allowedPorts[i])
                                    {
                                        portAllowed = true;
                                        break;
                                    }
                                }
                            }
                            if (!portAllowed)
                            {
                                Trace.TraceWarning("Incoming connection for port {0} not permitted", port);
                                try
                                {
                                    streamConnection.Stream.Close();
                                }
                                catch (Exception ex)
                                {
                                    Trace.TraceError("Error closing stream: {0}", ex.Message);
                                }
                                return;
                            }
                        }
                    }
                    else if (streamConnection.ConnectionInfo.StartsWith("np:", StringComparison.OrdinalIgnoreCase))
                    {
                        string pipeName = streamConnection.ConnectionInfo.Substring(3);
                        Trace.TraceInformation("Incoming connection for pipe {0}", pipeName);

                        bool pipeAllowed = noPipeConstraints;
                        if (!pipeAllowed)
                        {
                            for (int i = 0; i < allowedPipes.Count; i++)
                            {
                                if (pipeName.Equals(allowedPipes[i], StringComparison.OrdinalIgnoreCase))
                                {
                                    pipeAllowed = true;
                                    break;
                                }
                            }
                        }
                        if (!pipeAllowed)
                        {
                            Trace.TraceWarning("Incoming connection for pipe {0} not permitted", pipeName);
                            try
                            {
                                streamConnection.Stream.Close();
                            }
                            catch (Exception ex)
                            {
                                Trace.TraceError("Error closing stream: {0}", ex.Message);
                            }
                            return;
                        }
                    }
                    else
                    {
                        Trace.TraceError("Unable to handle connection for {0}", streamConnection.ConnectionInfo);
                        streamConnection.Stream.Close();
                        return;
                    }

                    MultiplexConnectionInputPump connectionPump =
                        new MultiplexConnectionInputPump(streamConnection.Stream.Read,
                                                         OnCreateConnection,
                                                         streamConnection);
                    connectionPump.Run(false);
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("Error accepting connection: {0}", ex.Message);
            }
        }