Skip to content
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

Move IDisposable implementation declaration from inheritees to parent #746

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Move common Dispose code to AuthenticationMethod class
  • Loading branch information
Michał Drzymała committed May 16, 2021
commit 8ca32fade4ab676556e89951a452ebd02b853b84
48 changes: 47 additions & 1 deletion src/Renci.SshNet/AuthenticationMethod.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Renci.SshNet.Common;
using System;
using System.Threading;
using Renci.SshNet.Security.Org.BouncyCastle.Crypto.Parameters;

namespace Renci.SshNet
{
Expand All @@ -8,6 +10,16 @@ namespace Renci.SshNet
/// </summary>
public abstract class AuthenticationMethod : IAuthenticationMethod, IDisposable
{
/// <summary>
/// Tracks result of current authentication process
/// </summary>
protected AuthenticationResult _authenticationResult = AuthenticationResult.Failure;

/// <summary>
/// Tracks completion of current authentication process
/// </summary>
protected EventWaitHandle _authenticationCompleted = null;

/// <summary>
/// Gets the name of the authentication method.
/// </summary>
Expand Down Expand Up @@ -39,10 +51,44 @@ protected AuthenticationMethod(string username)
Username = username;
}

#region IDisposable Members

private bool _isDisposed = false;

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


/// <summary>
/// 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>
protected void Dispose(bool disposing)
{
if (_isDisposed)
return;

if (disposing)
{
var authenticationCompleted = _authenticationCompleted;
if (authenticationCompleted != null)
{
authenticationCompleted.Dispose();
_authenticationCompleted = null;
}

// Only if called with Dispose(true) otherwise we treat it is as not Disposed properly
_isDisposed = true;
}
}

#endregion

/// <summary>
/// Authenticates the specified session.
Expand Down
39 changes: 2 additions & 37 deletions src/Renci.SshNet/KeyboardInteractiveAuthenticationMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ namespace Renci.SshNet
/// </summary>
public class KeyboardInteractiveAuthenticationMethod : AuthenticationMethod
{
private AuthenticationResult _authenticationResult = AuthenticationResult.Failure;

private Session _session;
private EventWaitHandle _authenticationCompleted = new AutoResetEvent(false);
private Session _session;
private Exception _exception;
private readonly RequestMessage _requestMessage;

Expand All @@ -42,6 +39,7 @@ public KeyboardInteractiveAuthenticationMethod(string username)
: base(username)
{
_requestMessage = new RequestMessageKeyboardInteractive(ServiceName.Connection, username);
_authenticationCompleted = new AutoResetEvent(false);
}

/// <summary>
Expand Down Expand Up @@ -135,39 +133,6 @@ private void Session_UserAuthenticationInformationRequestReceived(object sender,

#region IDisposable Members

private bool _isDisposed;

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

/// <summary>
/// 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>
protected virtual void Dispose(bool disposing)
{
if (_isDisposed)
return;

if (disposing)
{
var authenticationCompleted = _authenticationCompleted;
if (authenticationCompleted != null)
{
_authenticationCompleted = null;
authenticationCompleted.Dispose();
}

_isDisposed = true;
}
}

/// <summary>
/// Releases unmanaged resources and performs other cleanup operations before the
/// <see cref="KeyboardInteractiveAuthenticationMethod"/> is reclaimed by garbage collection.
Expand Down
41 changes: 3 additions & 38 deletions src/Renci.SshNet/NoneAuthenticationMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ namespace Renci.SshNet
/// </summary>
public class NoneAuthenticationMethod : AuthenticationMethod
{
private AuthenticationResult _authenticationResult = AuthenticationResult.Failure;
private EventWaitHandle _authenticationCompleted = new AutoResetEvent(false);

/// <summary>
/// <summary>
/// Gets connection name
/// </summary>
public override string Name
Expand All @@ -32,6 +29,7 @@ public override string Name
public NoneAuthenticationMethod(string username)
: base(username)
{
_authenticationCompleted = new AutoResetEvent(false);
}

/// <summary>
Expand Down Expand Up @@ -83,41 +81,8 @@ private void Session_UserAuthenticationFailureReceived(object sender, MessageEve

_authenticationCompleted.Set();
}

#region IDisposable Members

private bool _isDisposed;

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

/// <summary>
/// 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>
protected virtual void Dispose(bool disposing)
{
if (_isDisposed)
return;

if (disposing)
{
var authenticationCompleted = _authenticationCompleted;
if (authenticationCompleted != null)
{
authenticationCompleted.Dispose();
_authenticationCompleted = null;
}

_isDisposed = true;
}
}
#region IDisposable Members

/// <summary>
/// Releases unmanaged resources and performs other cleanup operations before the
Expand Down
39 changes: 3 additions & 36 deletions src/Renci.SshNet/PasswordAuthenticationMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ namespace Renci.SshNet
/// </summary>
public class PasswordAuthenticationMethod : AuthenticationMethod
{
private AuthenticationResult _authenticationResult = AuthenticationResult.Failure;
private Session _session;
private EventWaitHandle _authenticationCompleted = new AutoResetEvent(false);
private Session _session;
private Exception _exception;
private readonly RequestMessage _requestMessage;
private readonly byte[] _password;
Expand Down Expand Up @@ -54,6 +52,7 @@ internal byte[] Password
public PasswordAuthenticationMethod(string username, string password)
: this(username, Encoding.UTF8.GetBytes(password))
{
_authenticationCompleted = new AutoResetEvent(false);
}

/// <summary>
Expand All @@ -71,6 +70,7 @@ public PasswordAuthenticationMethod(string username, byte[] password)

_password = password;
_requestMessage = new RequestMessagePassword(ServiceName.Connection, Username, _password);
_authenticationCompleted = new AutoResetEvent(false);
}

/// <summary>
Expand Down Expand Up @@ -161,39 +161,6 @@ private void Session_UserAuthenticationPasswordChangeRequiredReceived(object sen

#region IDisposable Members

private bool _isDisposed;

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

/// <summary>
/// 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>
protected virtual void Dispose(bool disposing)
{
if (_isDisposed)
return;

if (disposing)
{
var authenticationCompleted = _authenticationCompleted;
if (authenticationCompleted != null)
{
authenticationCompleted.Dispose();
_authenticationCompleted = null;
}

_isDisposed = true;
}
}

/// <summary>
/// Releases unmanaged resources and performs other cleanup operations before the
/// <see cref="PasswordAuthenticationMethod"/> is reclaimed by garbage collection.
Expand Down
38 changes: 2 additions & 36 deletions src/Renci.SshNet/PrivateKeyAuthenticationMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ namespace Renci.SshNet
/// </summary>
public class PrivateKeyAuthenticationMethod : AuthenticationMethod
{
private AuthenticationResult _authenticationResult = AuthenticationResult.Failure;
private EventWaitHandle _authenticationCompleted = new ManualResetEvent(false);
private bool _isSignatureRequired;
private bool _isSignatureRequired;

/// <summary>
/// Gets authentication method name
Expand Down Expand Up @@ -43,6 +41,7 @@ public PrivateKeyAuthenticationMethod(string username, params PrivateKeyFile[] k
throw new ArgumentNullException("keyFiles");

KeyFiles = new Collection<PrivateKeyFile>(keyFiles);
_authenticationCompleted = new ManualResetEvent(false);
}

/// <summary>
Expand Down Expand Up @@ -149,39 +148,6 @@ private void Session_UserAuthenticationPublicKeyReceived(object sender, MessageE

#region IDisposable Members

private bool _isDisposed;

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

/// <summary>
/// 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>
protected virtual void Dispose(bool disposing)
{
if (_isDisposed)
return;

if (disposing)
{
var authenticationCompleted = _authenticationCompleted;
if (authenticationCompleted != null)
{
_authenticationCompleted = null;
authenticationCompleted.Dispose();
}

_isDisposed = true;
}
}

/// <summary>
/// Releases unmanaged resources and performs other cleanup operations before the
/// <see cref="PasswordConnectionInfo"/> is reclaimed by garbage collection.
Expand Down