void EnsureConnection()
        {
            lock (connectLock)
            {
                if (dataChannel == null)
                {
                    multiplexedOutputStream = new ThrottledQueueBufferedStream(10);

                    QueueBufferedStream multiplexedInputStream = new QueueBufferedStream();

                    dataChannelFactory = new HybridConnectionClient(endpointVia, tokenProvider);
                    dataChannel        = dataChannelFactory.CreateConnectionAsync().GetAwaiter().GetResult();

                    try
                    {
                        var preambleWriter = new BinaryWriter(dataChannel);
                        preambleWriter.Write("np:" + toPipe);

                        rawInputPump = new StreamBufferWritePump(dataChannel, multiplexedInputStream.Write);
                        rawInputPump.BeginRunPump(RawInputPumpCompleted, false);

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

                        outputPump = new StreamBufferWritePump(multiplexedOutputStream, WriteToDataChannel);
                        outputPump.BeginRunPump(MultiplexPumpCompleted, null);
                    }
                    catch (AuthorizationFailedException af)
                    {
                        Trace.TraceError("Authorization failed: {0}", af.Message);
                        if (dataChannel != null)
                        {
                            DataChannelClose();
                            dataChannel = null;
                        }
                        throw;
                    }
                    catch (Exception ex)
                    {
                        Trace.TraceError("Unable to establish data channel: {0}", ex.Message);
                        if (dataChannel != null)
                        {
                            DataChannelClose();
                            dataChannel = null;
                        }
                        throw;
                    }
                }
            }
        }
コード例 #2
0
        void StreamAccepted(HybridConnectionStream hybridConnectionStream)
        {
            try
            {
                if (hybridConnectionStream != null)
                {
                    relayListener.AcceptConnectionAsync().ContinueWith(t => StreamAccepted(t.Result));
                    var preambleReader = new BinaryReader(hybridConnectionStream);
                    var connectionInfo = preambleReader.ReadString();
                    if (connectionInfo.StartsWith("tcp:", StringComparison.OrdinalIgnoreCase))
                    {
                        int port;

                        if (!int.TryParse(connectionInfo.Substring(4), out port))
                        {
                            try
                            {
                                hybridConnectionStream.Close();
                            }
                            catch (Exception ex)
                            {
                                Trace.TraceError("Error closing stream: {0}", ex.Message);
                            }
                            return;
                        }
                        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
                            {
                                hybridConnectionStream.Close();
                            }
                            catch (Exception ex)
                            {
                                Trace.TraceError("Error closing stream: {0}", ex.Message);
                            }
                            return;
                        }
                    }
                    else if (connectionInfo.StartsWith("np:", StringComparison.OrdinalIgnoreCase))
                    {
                        string pipeName = 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
                            {
                                hybridConnectionStream.Close();
                            }
                            catch (Exception ex)
                            {
                                Trace.TraceError("Error closing stream: {0}", ex.Message);
                            }
                            return;
                        }
                    }
                    else
                    {
                        Trace.TraceError("Unable to handle connection for {0}", connectionInfo);
                        hybridConnectionStream.Close();
                        return;
                    }

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