Esempio n. 1
0
        /// <summary>
        ///     Invoked on the background thread.
        /// </summary>
        /// <exception cref="SerializationException">An object in the graph of type parameter <typeparamref name="TWrite"/> is not marked as serializable.</exception>
        private void WritePipe()
        {
            while (IsConnected && _streamWrapper.CanWrite)
            {
                try
                {
                    //using blockcollection, we needn't use singal to wait for result.
                    //_writeSignal.WaitOne();
                    //while (_writeQueue.Count > 0)
                    {
                        _streamWrapper.WriteObject(_writeQueue.Take());

                        // TODO:
                        // In the original code we waited for the pipe to be read here
                        // HOWEVER: since we also need to target UNIX platforms - we cannot do this
                    }
                }
                catch (Exception e)
                {
                    //we must igonre exception, otherwise, the namepipe wrapper will stop work.
                    Console.WriteLine("Exception in WritePipe(): \"{0}\" Inner: \"{1}\"", e.Message, e.InnerException);
                    Console.WriteLine(e.StackTrace);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        ///     Invoked on the background thread.
        /// </summary>
        /// <exception cref="SerializationException">An object in the graph of type parameter <typeparamref name="TWrite"/> is not marked as serializable.</exception>
        private void WritePipe()
        {
            while (IsConnected && _streamWrapper.CanWrite)
            {
                try
                {
                    //using blockcollection, we needn't use singal to wait for result.
                    //_writeSignal.WaitOne();
                    //while (_writeQueue.Count > 0)
                    {
                        _streamWrapper.WriteObject(_writeQueue.Take());
                        _streamWrapper.WaitForPipeDrain();

                        if (_futureSymmetricKey != null)
                        {
                            _streamWrapper.SetEncryptionKey(_futureSymmetricKey);
                            _futureSymmetricKey = null;
                        }
                    }
                }
                catch
                {
                    //we must ignore exception, otherwise, the namepipe wrapper will stop work.
                }
            }
        }
Esempio n. 3
0
 /// <summary>
 ///     Invoked on the background thread.
 /// </summary>
 /// <exception cref="SerializationException">An object in the graph of type parameter <typeparamref name="TWrite"/> is not marked as serializable.</exception>
 private void WritePipe()
 {
     while (IsConnected && _streamWrapper.CanWrite)
     {
         _writeSignal.WaitOne();
         while (_writeQueue.Count > 0)
         {
             _streamWrapper.WriteObject(_writeQueue.Dequeue());
             _streamWrapper.WaitForPipeDrain();
         }
     }
 }
Esempio n. 4
0
        private void WaitForConnection(string pipeName, PipeSecurity pipeSecurity)
        {
            NamedPipeServerStream handshakePipe            = null;
            NamedPipeServerStream dataPipe                 = null;
            NamedPipeConnection <TRead, TWrite> connection = null;

            var connectionPipeName = GetNextConnectionPipeName(pipeName);

            try
            {
                // Send the client the name of the data pipe to use
                handshakePipe = PipeServerFactory.CreateAndConnectPipe(pipeName, pipeSecurity);
                var handshakeWrapper = new PipeStreamWrapper <string, string>(handshakePipe);
                handshakeWrapper.WriteObject(connectionPipeName);
                handshakeWrapper.WaitForPipeDrain();
                handshakeWrapper.Close();

                // Wait for the client to connect to the data pipe
                dataPipe = PipeServerFactory.CreatePipe(connectionPipeName, pipeSecurity);
                dataPipe.WaitForConnection();

                // Add the client's connection to the list of connections
                connection = ConnectionFactory.CreateConnection <TRead, TWrite>(dataPipe);
                connection.ReceiveMessage += ClientOnReceiveMessage;
                connection.Disconnected   += ClientOnDisconnected;
                connection.Error          += ConnectionOnError;
                connection.Open();

                if (System.Threading.Monitor.TryEnter(_connections, 10000))
                {
                    try
                    {
                        _connections.Add(connection);
                    }
                    finally
                    {
                        System.Threading.Monitor.Exit(_connections);
                    }
                }

                ClientOnConnected(connection);
            }
            // Catch the IOException that is raised if the pipe is broken or disconnected.
            catch (Exception e)
            {
                Console.Error.WriteLine("Named pipe is broken or disconnected: {0}", e);

                Cleanup(handshakePipe);
                Cleanup(dataPipe);

                ClientOnDisconnected(connection);
            }
        }
Esempio n. 5
0
        private void WaitForConnection(string pipeName)
        {
            NamedPipeServerStream handshakePipe            = null;
            NamedPipeServerStream dataPipe                 = null;
            NamedPipeConnection <TRead, TWrite> connection = null;

            var connectionPipeName = GetNextConnectionPipeName(pipeName);

            try
            {
                // Send the client the name of the data pipe to use
                handshakePipe = PipeServerFactory.CreateAndConnectPipe(pipeName);
                var handshakeWrapper = new PipeStreamWrapper <string, string>(handshakePipe);
                handshakeWrapper.WriteObject(connectionPipeName);

                // Wait for the client to connect to the data pipe
                dataPipe = PipeServerFactory.CreatePipe(connectionPipeName);
                dataPipe.WaitForConnection();

                // Client has now connected (Which means that they found our data pipe)
                // We can now close the handshake pipe
                handshakeWrapper.Close();

                // Add the client's connection to the list of connections
                connection = ConnectionFactory.CreateConnection <TRead, TWrite>(dataPipe);
                connection.ReceiveMessage += ClientOnReceiveMessage;
                connection.Disconnected   += ClientOnDisconnected;
                connection.Error          += ConnectionOnError;
                connection.Open();

                lock (_connections)
                {
                    _connections.Add(connection);
                }

                ClientOnConnected(connection);
            }
            // Catch the IOException that is raised if the pipe is broken or disconnected.
            catch (Exception e)
            {
                Console.Error.WriteLine("Named pipe is broken or disconnected: {0}", e);

                Cleanup(handshakePipe);
                Cleanup(dataPipe);

                ClientOnDisconnected(connection);
            }
        }
Esempio n. 6
0
        private void WaitForConnection(string pipeName, PipeSecurity pipeSecurity)
        {
            NamedPipeServerStream handshakePipe            = null;
            NamedPipeServerStream dataPipe                 = null;
            NamedPipeConnection <TRead, TWrite> connection = null;

            var connectionPipeName = GetNextConnectionPipeName(pipeName);

            Console.WriteLine(connectionPipeName);
            try
            {
                // Send the client the name of the data pipe to use将要使用的数据管道的名称发送给客户机
                handshakePipe = PipeServerFactory.CreateAndConnectPipe(pipeName, pipeSecurity);
                var handshakeWrapper = new PipeStreamWrapper <string, string>(handshakePipe);
                handshakeWrapper.WriteObject(connectionPipeName);
                handshakeWrapper.WaitForPipeDrain();
                handshakeWrapper.Close();

                // Wait for the client to connect to the data pipe等待客户机连接到数据管道
                dataPipe = PipeServerFactory.CreatePipe(connectionPipeName, pipeSecurity);
                dataPipe.WaitForConnection();

                // Add the client's connection to the list of connections将客户端连接添加到连接列表中
                connection = ConnectionFactory.CreateConnection <TRead, TWrite>(dataPipe);
                connection.ReceiveMessage += ClientOnReceiveMessage;
                connection.Disconnected   += ClientOnDisconnected;
                connection.Error          += ConnectionOnError;
                connection.Open();

                lock (_connections)
                {
                    _connections.Add(connection);
                }

                ClientOnConnected(connection);
            }
            // Catch the IOException that is raised if the pipe is broken or disconnected.
            catch (Exception e)
            {
                Console.Error.WriteLine("Named pipe is broken or disconnected: {0}", e);

                Cleanup(handshakePipe);
                Cleanup(dataPipe);

                ClientOnDisconnected(connection);
            }
        }
Esempio n. 7
0
        /// <summary>
        ///     Invoked on the background thread.
        /// </summary>
        /// <exception cref="SerializationException">An object in the graph of type parameter <typeparamref name="TWrite"/> is not marked as serializable.</exception>
        private void WritePipe()
        {
            while (IsConnected && _streamWrapper.CanWrite)
            {
                try
                {
                    var data = _writeQueue.Take();
                    if (ReferenceEquals(data, _writeQueue))
                    {
                        return;
                    }

                    _streamWrapper.WriteObject((TWrite)data);
                    _streamWrapper.WaitForPipeDrain();
                }
                catch
                {
                    //we must igonre exception, otherwise, the namepipe wrapper will stop work.
                }
            }
        }
Esempio n. 8
0
 /// <summary>
 ///     Invoked on the background thread.
 /// </summary>
 /// <exception cref="SerializationException">An object in the graph of type parameter <typeparamref name="TWrite"/> is not marked as serializable.</exception>
 private void WritePipe()
 {
     while (IsConnected && _streamWrapper.CanWrite)
     {
         try
         {
             //using blockcollection, we needn't use singal to wait for result.
             //_writeSignal.WaitOne();
             //while (_writeQueue.Count > 0)
             {
                 _streamWrapper.WriteObject(_writeQueue.Take());
                 _streamWrapper.WaitForPipeDrain();
             }
         }
         catch (Exception ex)
         {
             //we must igonre exception, otherwise, the namepipe wrapper will stop work.
             this.OnError(ex);
         }
     }
 }
 /// <summary>
 ///     Invoked on the background thread.
 /// </summary>
 /// <exception cref="SerializationException">An object in the graph of type parameter <typeparamref name="TWrite"/> is not marked as serializable.</exception>
 private void WritePipe()
 {
     while (IsConnected && _streamWrapper.CanWrite)
     {
         TWrite x;
         try
         {
             x = _writeQueue.Take();
         }
         catch (InvalidOperationException)
         {
             // we have marked the queue as finished, so we don't have more to write
             break;
         }
         catch (OperationCanceledException)
         {
             // we have marked the queue as finished, so we don't have more to write
             break;
         }
         _streamWrapper.WriteObject(x);
         _streamWrapper.WaitForPipeDrain();
     }
 }
        /// <summary>
        ///     Invoked on the background thread.
        /// </summary>
        /// <exception cref="SerializationException">An object in the graph of type parameter <typeparamref name="TWrite"/> is not marked as serializable.</exception>
        private void WritePipe()
        {
            try
            {
                Debug.WriteLine($"BEGIN Write Pipie:{Id}");

                while (ShutDown == false && IsConnected && _streamWrapper.CanWrite)
                {
                    try
                    {
                        //using blockcollection, we needn't use singal to wait for result.
                        //_writeSignal.WaitOne();
                        //while (_writeQueue.Count > 0)
                        {
                            var obj = _writeQueue.Take(_cts.Token);
                            if (obj == null)
                            {
                                // 取消了,直接退出
                                return;
                            }

                            _streamWrapper.WriteObject(obj);
                            _streamWrapper.WaitForPipeDrain();
                        }
                    }
                    catch
                    {
                        //we must igonre exception, otherwise, the namepipe wrapper will stop work.
                    }
                }
            }
            finally
            {
                Debug.WriteLine($"END Write Pipie:{Id}");
            }
        }
        private void WaitForConnection()
        {
            LogDebug("WaitForConnection");
            NamedPipeServerStream handshakePipe            = null;
            NamedPipeServerStream dataPipe                 = null;
            NamedPipeConnection <TRead, TWrite> connection = null;

            var connectionPipeName = GetNextConnectionPipeName();

            try
            {
                dataPipe = CreatePipe(connectionPipeName);
                LogDebug("dataPipe created");

                // Send the client the name of the data pipe to use
                LogDebug("Send the client the name of the data pipe to use");
                handshakePipe = CreateAndConnectPipe();
                LogDebug("handshakePipe created");

                var handshakeWrapper = new PipeStreamWrapper <string, string>(handshakePipe);
                LogDebug("handshakeWrapper created");


                handshakeWrapper.SetLogger(_logger);
                handshakeWrapper.WriteObject(connectionPipeName);
                handshakeWrapper.WaitForPipeDrain();
                handshakeWrapper.Close();
                LogDebug("handshakeWrapper closed");


                // Wait for the client to connect to the data pipe
                LogDebug("Wait for the client to connect to the data pipe");
                dataPipe.WaitForConnection();

                LogDebug("Add the client's connection to the list of connections");
                // Add the client's connection to the list of connections
                connection = ConnectionFactory.CreateConnection <TRead, TWrite>(dataPipe);
                connection.ReceiveMessage += ClientOnReceiveMessage;
                connection.Disconnected   += ClientOnDisconnected;
                connection.Error          += ConnectionOnError;
                connection.Open();
                LogDebug("Open connection");

                lock (_connections) { _connections.Add(connection); }

                ClientOnConnected(connection);
            }
            // Catch the IOException that is raised if the pipe is broken or disconnected.
            catch (Exception e)
            {
                LogError(e, "Named pipe is broken or disconnected");
                Console.Error.WriteLine("Named pipe is broken or disconnected: {0}", e);

                LogDebug("Cleanup handshakePipe");
                Cleanup(handshakePipe);
                LogDebug("Cleanup dataPipe");
                Cleanup(dataPipe);

                ClientOnDisconnected(connection);
            }
        }