/// <summary> /// Stop tunnel /// </summary> /// <returns>Connection state of tunnel and string with error message if occured (null if no error).</returns> /// <exception cref="BashProcessNotInintializedException">Throws when tunneling proces wasn't initialized</exception> /// <exception cref="BashProcessNotRunningException">Throws when tunneling proces is not running</exception> public async Task <(TunnelConnectionState, string)> Stop() { if (_SSHProcess == null) { throw new BashProcessNotInintializedException(); } else if (!_SSHProcess.IsProcessRunning || _SSHProcess.CurrentBashProcess.HasExited) { IsTunnelEstablished = false; throw new BashProcessNotRunningException(); } _SSHProcess.KillProcess(); IsTunnelEstablished = false; return(await CheckAndKillOldProcesses()); }
/// <summary> /// Finds old processes used by last command and kill them /// </summary> /// <returns>Current tunnel connection type</returns> public async Task <(TunnelConnectionState, string)> CheckAndKillOldProcesses() { try { if (await CheckConnectionType() != TunnelConnectionState.NoConnection) { List <int> PIDs = BashProcess.FindPIDs(LastStartedCommandSSH ?? DefaultSSHCommand); foreach (int pid in PIDs) { BashProcess.KillProcess(pid); } } return(await CheckConnectionType(), null); } catch (TunnelEstablishedException ex) { return(TunnelConnectionState.StoppedWithoutChecking, ex.Message); } }