Example #1
0
 private void ClientOnReceiveMessage(NamedPipeConnection <TRead, TWrite> connection, TRead message)
 {
     if (ClientMessage != null)
     {
         ClientMessage(connection, message);
     }
 }
Example #2
0
 private void ClientOnConnected(NamedPipeConnection <TRead, TWrite> connection)
 {
     if (ClientConnected != null)
     {
         ClientConnected(connection);
     }
 }
Example #3
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);
            }
        }
Example #4
0
        private void ClientOnDisconnected(NamedPipeConnection <TRead, TWrite> connection)
        {
            if (connection == null)
            {
                return;
            }

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

            if (ClientDisconnected != null)
            {
                ClientDisconnected(connection);
            }
        }
Example #5
0
        private void ClientOnDisconnected(NamedPipeConnection <TRead, TWrite> connection)
        {
            if (connection == null)
            {
                return;
            }

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

            if (ClientDisconnected != null)
            {
                ClientDisconnected(connection);
            }
        }
Example #6
0
 /// <summary>
 ///     Invoked on the UI thread.
 /// </summary>
 private void ConnectionOnError(NamedPipeConnection <TRead, TWrite> connection, Exception exception)
 {
     OnError(exception);
 }