Skip to content

Merge partial classes and complete IForwardedPort interface #1223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Renci.SshNet/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ MA0053.public_class_should_be_sealed = false

#### .NET Compiler Platform analysers rules ####

# CA1030: Use events where appropriate
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1030
dotnet_diagnostic.CA10310.severity = none

# CA1031: Do not catch general exception types
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1031
dotnet_diagnostic.CA1031.severity = none
Expand Down
43 changes: 24 additions & 19 deletions src/Renci.SshNet/ForwardedPort.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;

using Renci.SshNet.Common;

namespace Renci.SshNet
Expand All @@ -16,28 +17,19 @@ public abstract class ForwardedPort : IForwardedPort
/// </value>
internal ISession Session { get; set; }

/// <summary>
/// The <see cref="Closing"/> event occurs as the forwarded port is being stopped.
/// </summary>
internal event EventHandler Closing;

/// <summary>
/// The <see cref="IForwardedPort.Closing"/> event occurs as the forwarded port is being stopped.
/// </summary>
event EventHandler IForwardedPort.Closing
{
add { Closing += value; }
remove { Closing -= value; }
}

/// <summary>
/// Gets a value indicating whether port forwarding is started.
/// </summary>
/// <value>
/// <c>true</c> if port forwarding is started; otherwise, <c>false</c>.
/// <see langword="true"/> if port forwarding is started; otherwise, <see langword="false"/>.
/// </value>
public abstract bool IsStarted { get; }

/// <summary>
/// The <see cref="Closing"/> event occurs as the forwarded port is being stopped.
/// </summary>
public event EventHandler Closing;

/// <summary>
/// Occurs when an exception is thrown.
/// </summary>
Expand All @@ -51,6 +43,8 @@ event EventHandler IForwardedPort.Closing
/// <summary>
/// Starts port forwarding.
/// </summary>
/// <exception cref="InvalidOperationException">The current <see cref="ForwardedPort"/> is already started -or- is not linked to a SSH session.</exception>
/// <exception cref="SshConnectionException">The client is not connected.</exception>
public virtual void Start()
{
CheckDisposed();
Expand All @@ -77,14 +71,25 @@ public virtual void Start()
/// <summary>
/// Stops port forwarding.
/// </summary>
#pragma warning disable CA1716 // Identifiers should not match keywords
public virtual void Stop()
#pragma warning restore CA1716 // Identifiers should not match keywords
{
if (IsStarted)
{
StopPort(Session.ConnectionInfo.Timeout);
}
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Starts port forwarding.
/// </summary>
Expand All @@ -100,22 +105,22 @@ protected virtual void StopPort(TimeSpan timeout)
RaiseClosing();

var session = Session;
if (session != null)
if (session is not null)
{
session.ErrorOccured -= Session_ErrorOccured;
}
}

/// <summary>
/// Releases unmanaged and - optionally - managed resources
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
/// <param name="disposing"><see langowrd="true"/> to release both managed and unmanaged resources; <see langowrd="false"/> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
var session = Session;
if (session != null)
if (session is not null)
{
StopPort(session.ConnectionInfo.Timeout);
Session = null;
Expand Down
Loading