private void ExecuteCommand(PipeCommand cmd) { switch (cmd.Type) { case PipeCommandType.InitiateService: SendCommand(cmd.Data != BitConverter.ToString(AppConfigs.PipeConfiguration.AesKeyBytes) ? new PipeCommand(PipeCommandType.WrongAuth, cmd.Data) : new PipeCommand(PipeCommandType.InitiateService, AppConfigs.PipeConfiguration.AuthentificationString) { Auth = _auth }); break; case PipeCommandType.StopApplication: if (cmd.Auth != _auth) { SendCommand(new PipeCommand(PipeCommandType.WrongAuth, cmd.Auth)); return; } AppLogger.Log.Warn("Received stopping app from ", cmd.Data); Application.Exit(); break; case PipeCommandType.WrongAuth: break; default: throw new ArgumentOutOfRangeException(nameof(cmd), cmd, null); } }
/// <summary> /// Send a command to the Pipe Server /// </summary> /// <param name="cmd"></param> public void SendCommand(PipeCommand cmd) { if (_serverAuth != null) { cmd.Auth = _serverAuth; } cmd.Write(_namedPipe); _namedPipe.WaitForPipeDrain(); }
private void InitPipeClient() { try { _namedPipe.Connect(2000); } catch (TimeoutException) { if (ExitIfNoConnection) { AppLogger.Log.Warn("No Pipe available. Closing the application."); Environment.FailFast("No pipe available"); } else { throw; } } SendCommand(new PipeCommand(PipeCommandType.InitiateService, BitConverter.ToString(AppConfigs.PipeConfiguration.AesKeyBytes))); var cmd = PipeCommand.GetPipeCommand(_namedPipe); if (cmd == null) { throw new Exception("No handshake"); } if (cmd.Type != PipeCommandType.InitiateService) { throw new Exception("Wrong Command: " + cmd.Type); } if (cmd.Data != AppConfigs.PipeConfiguration.AuthentificationString) { throw new Exception("Wrong Auth: " + cmd.Data); } _serverAuth = cmd.Auth; IsConnected = true; var task = new Task(() => { while (_namedPipe.IsConnected) { var pipeCmd = PipeCommand.GetPipeCommand(_namedPipe); if (pipeCmd != null) { PipeCommandReceived?.Invoke(this, new PipeCommandReceivedEvent(pipeCmd)); } } }); task.Start(); }
private void ProcessPipeCommands(IAsyncResult arResult) { _arConnectionRequest = arResult; if (!arResult.IsCompleted) { return; } _namedPipe.WaitForConnection(); while (_applicationRunning) { var pipeCmd = PipeCommand.GetPipeCommand(_namedPipe); if (pipeCmd == null) { continue; } ExecuteCommand(pipeCmd); } _namedPipe.EndWaitForConnection(_arConnectionRequest); _namedPipe.Close(); }
public PipeCommandReceivedEvent(PipeCommand command) { Command = command; }
/// <summary> /// Send command to the pipe client /// </summary> /// <param name="cmd"></param> public void SendCommand(PipeCommand cmd) { cmd.Write(_namedPipe); _namedPipe.WaitForPipeDrain(); }