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 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
53 changes: 52 additions & 1 deletion src/Renci.SshNet/AuthenticationMethod.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
using Renci.SshNet.Common;
using System;
using System.Threading;
using Renci.SshNet.Security.Org.BouncyCastle.Crypto.Parameters;

namespace Renci.SshNet
{
/// <summary>
/// Base class for all supported authentication methods
/// </summary>
public abstract class AuthenticationMethod : IAuthenticationMethod
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,6 +51,45 @@ 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 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.
/// </summary>
Expand Down
54 changes: 3 additions & 51 deletions src/Renci.SshNet/KeyboardInteractiveAuthenticationMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ namespace Renci.SshNet
/// <summary>
/// Provides functionality to perform keyboard interactive authentication.
/// </summary>
public class KeyboardInteractiveAuthenticationMethod : AuthenticationMethod, IDisposable
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 @@ -132,51 +130,5 @@ 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 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.
/// </summary>
~KeyboardInteractiveAuthenticationMethod()
{
Dispose(false);
}

#endregion
}
}
55 changes: 3 additions & 52 deletions src/Renci.SshNet/NoneAuthenticationMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ namespace Renci.SshNet
/// <summary>
/// Provides functionality for "none" authentication method
/// </summary>
public class NoneAuthenticationMethod : AuthenticationMethod, IDisposable
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,52 +81,5 @@ 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 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="NoneAuthenticationMethod"/> is reclaimed by garbage collection.
/// </summary>
~NoneAuthenticationMethod()
{
Dispose(false);
}

#endregion

}
}
54 changes: 4 additions & 50 deletions src/Renci.SshNet/PasswordAuthenticationMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ namespace Renci.SshNet
/// <summary>
/// Provides functionality to perform password authentication.
/// </summary>
public class PasswordAuthenticationMethod : AuthenticationMethod, IDisposable
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 @@ -158,51 +158,5 @@ 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 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.
/// </summary>
~PasswordAuthenticationMethod()
{
Dispose(false);
}

#endregion
}
}
53 changes: 3 additions & 50 deletions src/Renci.SshNet/PrivateKeyAuthenticationMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ namespace Renci.SshNet
/// <summary>
/// Provides functionality to perform private key authentication.
/// </summary>
public class PrivateKeyAuthenticationMethod : AuthenticationMethod, IDisposable
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 @@ -147,52 +146,6 @@ private void Session_UserAuthenticationPublicKeyReceived(object sender, MessageE
_authenticationCompleted.Set();
}

#region IDisposable Members

private bool _isDisposed;

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
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 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.
/// </summary>
~PrivateKeyAuthenticationMethod()
{
Dispose(false);
}

#endregion

private class SignatureData : SshData
{
private readonly RequestMessagePublicKey _message;
Expand Down