private void ClientOnReceiveMessage(NamedPipeConnection <TRead, TWrite> connection, TRead message) { if (ClientMessage != null) { ClientMessage(connection, message); } }
private void ClientOnConnected(NamedPipeConnection <TRead, TWrite> connection) { if (ClientConnected != null) { ClientConnected(connection); } }
private void OnClientConnected(NamedPipeConnection <string, string> connection) { if (FindByName(connection.Name) == null) { _clients.Add(new HeartbeatClient(connection.Name)); } //SendCommand(Commands.SetTimeOut, HardTimeout); }
private void OnClientDisconnected(NamedPipeConnection <string, string> connection) { var client = FindByName(connection.Name); if (client != null) { _clients.Remove(client); } }
private void ClientOnDisconnected(NamedPipeConnection <TRead, TWrite> connection) { if (connection == null) { return; } lock (_connections) { _connections.Remove(connection); } if (ClientDisconnected != null) { ClientDisconnected(connection); } }
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(); 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); } }
private void OnClientMessage(NamedPipeConnection <string, string> connection, string message) { var args = message.Split(','); if (args.Length == 0) { return; } uint command; if (!uint.TryParse(args[0], out command)) { return; } switch (command) { case (int)Commands.Heartbeat: { var client = FindByName(connection.Name); if (client == null) { break; } if (args.Length < 2) { return; } client.ProcessName = args[1]; client.LastHeartbeat = DateTime.Now; Debug.WriteLine("received heartbeat"); } break; case (int)Commands.RequestKill: { var client = FindByName(connection.Name); if (client == null) { break; } if (args.Length < 2) { return; } client.ProcessName = args[1]; if (args.Length == 3) { uint delay; if (!uint.TryParse(args[2], out delay)) { return; } client.KillTime = DateTime.Now + TimeSpan.FromSeconds(delay); Logger.Warn("Received kill after {0} seconds request by Process {1}", delay, client.ProcessName); //Debug.WriteLine("received delayed kill"); } else { client.KillTime = DateTime.Now; Logger.Warn("Received kill request by Process {0}", client.ProcessName); //Debug.WriteLine("received kill"); } client.RequestKill = true; } break; default: Debug.WriteLine("Unrecognized command"); break; } }
/// <summary> /// Invoked on the UI thread. /// </summary> private void ConnectionOnError(NamedPipeConnection <TRead, TWrite> connection, Exception exception) { OnError(exception); }