Skip to content

Commit 42ff920

Browse files
Merge partial classes and complete IForwardedPort interface (#1223)
* Merge partial classes. Complete IForwardedPort interface. Expoe Closing event publicly on ForwardedPort. Disable CA1030 (use events where appropriate). * Merge partial classes. Complete IForwardedPort interface. Expoe Closing event publicly on ForwardedPort. Disable CA1030 (use events where appropriate). * Use local function and remove suppression. * Compile regular expressions. * Fix build, pfff. --------- Co-authored-by: Wojciech Nagórski <wojtpl2@gmail.com>
1 parent 066f998 commit 42ff920

14 files changed

+1434
-1436
lines changed

src/Renci.SshNet/.editorconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ MA0053.public_class_should_be_sealed = false
2121

2222
#### .NET Compiler Platform analysers rules ####
2323

24+
# CA1030: Use events where appropriate
25+
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1030
26+
dotnet_diagnostic.CA10310.severity = none
27+
2428
# CA1031: Do not catch general exception types
2529
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1031
2630
dotnet_diagnostic.CA1031.severity = none

src/Renci.SshNet/ForwardedPort.cs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
23
using Renci.SshNet.Common;
34

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

19-
/// <summary>
20-
/// The <see cref="Closing"/> event occurs as the forwarded port is being stopped.
21-
/// </summary>
22-
internal event EventHandler Closing;
23-
24-
/// <summary>
25-
/// The <see cref="IForwardedPort.Closing"/> event occurs as the forwarded port is being stopped.
26-
/// </summary>
27-
event EventHandler IForwardedPort.Closing
28-
{
29-
add { Closing += value; }
30-
remove { Closing -= value; }
31-
}
32-
3320
/// <summary>
3421
/// Gets a value indicating whether port forwarding is started.
3522
/// </summary>
3623
/// <value>
37-
/// <c>true</c> if port forwarding is started; otherwise, <c>false</c>.
24+
/// <see langword="true"/> if port forwarding is started; otherwise, <see langword="false"/>.
3825
/// </value>
3926
public abstract bool IsStarted { get; }
4027

28+
/// <summary>
29+
/// The <see cref="Closing"/> event occurs as the forwarded port is being stopped.
30+
/// </summary>
31+
public event EventHandler Closing;
32+
4133
/// <summary>
4234
/// Occurs when an exception is thrown.
4335
/// </summary>
@@ -51,6 +43,8 @@ event EventHandler IForwardedPort.Closing
5143
/// <summary>
5244
/// Starts port forwarding.
5345
/// </summary>
46+
/// <exception cref="InvalidOperationException">The current <see cref="ForwardedPort"/> is already started -or- is not linked to a SSH session.</exception>
47+
/// <exception cref="SshConnectionException">The client is not connected.</exception>
5448
public virtual void Start()
5549
{
5650
CheckDisposed();
@@ -77,14 +71,25 @@ public virtual void Start()
7771
/// <summary>
7872
/// Stops port forwarding.
7973
/// </summary>
74+
#pragma warning disable CA1716 // Identifiers should not match keywords
8075
public virtual void Stop()
76+
#pragma warning restore CA1716 // Identifiers should not match keywords
8177
{
8278
if (IsStarted)
8379
{
8480
StopPort(Session.ConnectionInfo.Timeout);
8581
}
8682
}
8783

84+
/// <summary>
85+
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
86+
/// </summary>
87+
public void Dispose()
88+
{
89+
Dispose(disposing: true);
90+
GC.SuppressFinalize(this);
91+
}
92+
8893
/// <summary>
8994
/// Starts port forwarding.
9095
/// </summary>
@@ -100,22 +105,22 @@ protected virtual void StopPort(TimeSpan timeout)
100105
RaiseClosing();
101106

102107
var session = Session;
103-
if (session != null)
108+
if (session is not null)
104109
{
105110
session.ErrorOccured -= Session_ErrorOccured;
106111
}
107112
}
108113

109114
/// <summary>
110-
/// Releases unmanaged and - optionally - managed resources
115+
/// Releases unmanaged and - optionally - managed resources.
111116
/// </summary>
112-
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
117+
/// <param name="disposing"><see langowrd="true"/> to release both managed and unmanaged resources; <see langowrd="false"/> to release only unmanaged resources.</param>
113118
protected virtual void Dispose(bool disposing)
114119
{
115120
if (disposing)
116121
{
117122
var session = Session;
118-
if (session != null)
123+
if (session is not null)
119124
{
120125
StopPort(session.ConnectionInfo.Timeout);
121126
Session = null;

0 commit comments

Comments
 (0)