@@ -478,6 +478,73 @@ public void CancelAsync(bool forceKill = false, int millisecondsTimeout = 500)
478478 }
479479 }
480480
481+ private static string ? GetSignalName ( CommandSignal signal )
482+ {
483+ #if NETCOREAPP
484+ return Enum . GetName ( signal ) ;
485+ #else
486+
487+ // Boxes signal, but Enum.GetName does not have a non-boxing overload prior to .NET Core.
488+ return Enum . GetName ( typeof ( CommandSignal ) , signal ) ;
489+ #endif
490+ }
491+
492+ /// <summary>
493+ /// Tries to send a POSIX/ANSI signal to the remote process executing the command, such as SIGINT or SIGTERM.
494+ /// </summary>
495+ /// <param name="signal">The signal to send</param>
496+ /// <returns>If the signal was sent.</returns>
497+ public bool TrySendSignal ( CommandSignal signal )
498+ {
499+ var signalName = GetSignalName ( signal ) ;
500+ if ( signalName is null )
501+ {
502+ return false ;
503+ }
504+
505+ if ( _tcs is null || _tcs . Task . IsCompleted || _channel ? . IsOpen != true )
506+ {
507+ return false ;
508+ }
509+
510+ try
511+ {
512+ // Try to send the cancellation signal.
513+ return _channel . SendSignalRequest ( signalName ) ;
514+ }
515+ catch ( Exception )
516+ {
517+ // Exception can be ignored since we are in a Try method
518+ // Possible exceptions here: InvalidOperationException, SshConnectionException, SshOperationTimeoutException
519+ }
520+
521+ return false ;
522+ }
523+
524+ /// <summary>
525+ /// Tries to send a POSIX/ANSI signal to the remote process executing the command, such as SIGINT or SIGTERM.
526+ /// </summary>
527+ /// <param name="signal">The signal to send</param>
528+ /// <exception cref="ArgumentException">Signal was not a valid CommandSignal.</exception>
529+ /// <exception cref="SshConnectionException">The client is not connected.</exception>
530+ /// <exception cref="SshOperationTimeoutException">The operation timed out.</exception>
531+ /// <exception cref="InvalidOperationException">The size of the packet exceeds the maximum size defined by the protocol.</exception>
532+ /// <exception cref="InvalidOperationException">Command has not been started.</exception>
533+ public void SendSignal ( CommandSignal signal )
534+ {
535+ var signalName = GetSignalName ( signal ) ;
536+ if ( signalName is null )
537+ {
538+ throw new ArgumentException ( "Signal was not a valid CommandSignal." ) ;
539+ }
540+ if ( _tcs is null || _tcs . Task . IsCompleted || _channel ? . IsOpen != true )
541+ {
542+ throw new InvalidOperationException ( "Command has not been started." ) ;
543+ }
544+
545+ _ = _channel . SendSignalRequest ( signalName ) ;
546+ }
547+
481548 /// <summary>
482549 /// Executes the command specified by <see cref="CommandText"/>.
483550 /// </summary>
0 commit comments