Пример #1
0
 private void ClientOnConnected(NamedPipeConnection <TRead, TWrite> connection)
 {
     if (ClientConnected != null)
     {
         ClientConnected(connection);
     }
 }
Пример #2
0
 private void ClientOnReceiveMessage(NamedPipeConnection <TRead, TWrite> connection, TRead message)
 {
     if (ClientMessage != null)
     {
         ClientMessage(connection, message);
     }
 }
 private void ClientOnReceiveMessage(NamedPipeConnection <TRead, TWrite> connection, TRead message)
 {
     if (ClientMessage != null)
     {
         LogDebug($"ClientOnReceiveMessage, connection.Id: {connection.Id}");
         ClientMessage(connection, message);
     }
 }
 private void ClientOnConnected(NamedPipeConnection <TRead, TWrite> connection)
 {
     if (ClientConnected != null)
     {
         LogDebug($"ClientOnConnected, connection.Id: {connection.Id}");
         ClientConnected(connection);
     }
 }
Пример #5
0
        private void ClientOnDisconnected(NamedPipeConnection<TRead, TWrite> connection)
        {
            if (connection == null)
                return;

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

            if (ClientDisconnected != null)
                ClientDisconnected(connection);
        }
Пример #6
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);
            }
        }
Пример #7
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);
            }
        }
Пример #8
0
        private void ClientOnDisconnected(NamedPipeConnection <TRead, TWrite> connection)
        {
            if (connection == null)
            {
                return;
            }

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

            if (ClientDisconnected != null)
            {
                ClientDisconnected(connection);
            }
        }
Пример #9
0
 private void OnClientMessage(NamedPipeConnection <MessageContainer, MessageContainer> connection, MessageContainer message)
 {
     Console.WriteLine("Client {0} says: {1}", connection.Id, message);
     //_messageQueue.Add(message);
     if (message == null || message.Message == null)
     {
         Logger.Log("message null");
     }
     else
     {
         _messageQueue.Post(message.Message);
         //EventHandler<MessageType> handler = MsgReceived;
         //if (handler != null)
         //{
         //    handler(this, message.Message);
         //}
     }
 }
Пример #10
0
        private void ClientOnDisconnected(NamedPipeConnection <TRead, TWrite> connection)
        {
            if (connection == null)
            {
                return;
            }

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

            if (ClientDisconnected != null)
            {
                LogDebug($"ClientOnDisconnected, connection.Id: {connection.Id}");
                ClientDisconnected(connection);
            }
        }
Пример #11
0
 /// <summary>
 ///     Invoked on the UI thread.
 /// </summary>
 private void ConnectionOnError(NamedPipeConnection <TRead, TWrite> connection, Exception exception)
 {
     OnError(exception);
 }
Пример #12
0
 private void OnClientDisconnected(NamedPipeConnection <MessageContainer, MessageContainer> connection)
 {
     Console.WriteLine("Client {0} disconnected", connection.Id);
     //_messageQueue.Add(new MessageContainer(new MsgExit()));
     //TODO
 }
Пример #13
0
 private void OnClientConnected(NamedPipeConnection <MessageContainer, MessageContainer> connection)
 {
     Console.WriteLine("Client {0} is now connected!", connection.Id);
 }
Пример #14
0
 /// <summary>
 ///     Invoked on the UI thread.
 /// </summary>
 private void ConnectionOnError(NamedPipeConnection <TRead, TWrite> connection, Exception exception)
 {
     LogDebug($"ConnectionOnError, connection.Id: {connection.Id}");
     OnError(exception);
 }
Пример #15
0
        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);
            }
        }
Пример #16
0
 private void OnServerMessage(NamedPipeConnection <MessageContainer, MessageContainer> connection, MessageContainer message)
 {
     Console.WriteLine("Server says: {0}", message);
 }