Skip to content

Commit 8ca32fa

Browse files
author
Michał Drzymała
committed
Move common Dispose code to AuthenticationMethod class
1 parent 894f700 commit 8ca32fa

File tree

5 files changed

+57
-148
lines changed

5 files changed

+57
-148
lines changed

src/Renci.SshNet/AuthenticationMethod.cs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using Renci.SshNet.Common;
22
using System;
3+
using System.Threading;
4+
using Renci.SshNet.Security.Org.BouncyCastle.Crypto.Parameters;
35

46
namespace Renci.SshNet
57
{
@@ -8,6 +10,16 @@ namespace Renci.SshNet
810
/// </summary>
911
public abstract class AuthenticationMethod : IAuthenticationMethod, IDisposable
1012
{
13+
/// <summary>
14+
/// Tracks result of current authentication process
15+
/// </summary>
16+
protected AuthenticationResult _authenticationResult = AuthenticationResult.Failure;
17+
18+
/// <summary>
19+
/// Tracks completion of current authentication process
20+
/// </summary>
21+
protected EventWaitHandle _authenticationCompleted = null;
22+
1123
/// <summary>
1224
/// Gets the name of the authentication method.
1325
/// </summary>
@@ -39,10 +51,44 @@ protected AuthenticationMethod(string username)
3951
Username = username;
4052
}
4153

54+
#region IDisposable Members
55+
56+
private bool _isDisposed = false;
57+
4258
/// <summary>
4359
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
4460
/// </summary>
45-
public abstract void Dispose();
61+
public void Dispose()
62+
{
63+
Dispose(true);
64+
GC.SuppressFinalize(this);
65+
}
66+
67+
68+
/// <summary>
69+
/// Releases unmanaged and - optionally - managed resources
70+
/// </summary>
71+
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
72+
protected void Dispose(bool disposing)
73+
{
74+
if (_isDisposed)
75+
return;
76+
77+
if (disposing)
78+
{
79+
var authenticationCompleted = _authenticationCompleted;
80+
if (authenticationCompleted != null)
81+
{
82+
authenticationCompleted.Dispose();
83+
_authenticationCompleted = null;
84+
}
85+
86+
// Only if called with Dispose(true) otherwise we treat it is as not Disposed properly
87+
_isDisposed = true;
88+
}
89+
}
90+
91+
#endregion
4692

4793
/// <summary>
4894
/// Authenticates the specified session.

src/Renci.SshNet/KeyboardInteractiveAuthenticationMethod.cs

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ namespace Renci.SshNet
1313
/// </summary>
1414
public class KeyboardInteractiveAuthenticationMethod : AuthenticationMethod
1515
{
16-
private AuthenticationResult _authenticationResult = AuthenticationResult.Failure;
17-
18-
private Session _session;
19-
private EventWaitHandle _authenticationCompleted = new AutoResetEvent(false);
16+
private Session _session;
2017
private Exception _exception;
2118
private readonly RequestMessage _requestMessage;
2219

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

4745
/// <summary>
@@ -135,39 +133,6 @@ private void Session_UserAuthenticationInformationRequestReceived(object sender,
135133

136134
#region IDisposable Members
137135

138-
private bool _isDisposed;
139-
140-
/// <summary>
141-
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
142-
/// </summary>
143-
public override void Dispose()
144-
{
145-
Dispose(true);
146-
GC.SuppressFinalize(this);
147-
}
148-
149-
/// <summary>
150-
/// Releases unmanaged and - optionally - managed resources
151-
/// </summary>
152-
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
153-
protected virtual void Dispose(bool disposing)
154-
{
155-
if (_isDisposed)
156-
return;
157-
158-
if (disposing)
159-
{
160-
var authenticationCompleted = _authenticationCompleted;
161-
if (authenticationCompleted != null)
162-
{
163-
_authenticationCompleted = null;
164-
authenticationCompleted.Dispose();
165-
}
166-
167-
_isDisposed = true;
168-
}
169-
}
170-
171136
/// <summary>
172137
/// Releases unmanaged resources and performs other cleanup operations before the
173138
/// <see cref="KeyboardInteractiveAuthenticationMethod"/> is reclaimed by garbage collection.

src/Renci.SshNet/NoneAuthenticationMethod.cs

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ namespace Renci.SshNet
1313
/// </summary>
1414
public class NoneAuthenticationMethod : AuthenticationMethod
1515
{
16-
private AuthenticationResult _authenticationResult = AuthenticationResult.Failure;
17-
private EventWaitHandle _authenticationCompleted = new AutoResetEvent(false);
18-
19-
/// <summary>
16+
/// <summary>
2017
/// Gets connection name
2118
/// </summary>
2219
public override string Name
@@ -32,6 +29,7 @@ public override string Name
3229
public NoneAuthenticationMethod(string username)
3330
: base(username)
3431
{
32+
_authenticationCompleted = new AutoResetEvent(false);
3533
}
3634

3735
/// <summary>
@@ -83,41 +81,8 @@ private void Session_UserAuthenticationFailureReceived(object sender, MessageEve
8381

8482
_authenticationCompleted.Set();
8583
}
86-
87-
#region IDisposable Members
88-
89-
private bool _isDisposed;
90-
91-
/// <summary>
92-
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
93-
/// </summary>
94-
public override void Dispose()
95-
{
96-
Dispose(true);
97-
GC.SuppressFinalize(this);
98-
}
9984

100-
/// <summary>
101-
/// Releases unmanaged and - optionally - managed resources
102-
/// </summary>
103-
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
104-
protected virtual void Dispose(bool disposing)
105-
{
106-
if (_isDisposed)
107-
return;
108-
109-
if (disposing)
110-
{
111-
var authenticationCompleted = _authenticationCompleted;
112-
if (authenticationCompleted != null)
113-
{
114-
authenticationCompleted.Dispose();
115-
_authenticationCompleted = null;
116-
}
117-
118-
_isDisposed = true;
119-
}
120-
}
85+
#region IDisposable Members
12186

12287
/// <summary>
12388
/// Releases unmanaged resources and performs other cleanup operations before the

src/Renci.SshNet/PasswordAuthenticationMethod.cs

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ namespace Renci.SshNet
1313
/// </summary>
1414
public class PasswordAuthenticationMethod : AuthenticationMethod
1515
{
16-
private AuthenticationResult _authenticationResult = AuthenticationResult.Failure;
17-
private Session _session;
18-
private EventWaitHandle _authenticationCompleted = new AutoResetEvent(false);
16+
private Session _session;
1917
private Exception _exception;
2018
private readonly RequestMessage _requestMessage;
2119
private readonly byte[] _password;
@@ -54,6 +52,7 @@ internal byte[] Password
5452
public PasswordAuthenticationMethod(string username, string password)
5553
: this(username, Encoding.UTF8.GetBytes(password))
5654
{
55+
_authenticationCompleted = new AutoResetEvent(false);
5756
}
5857

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

7271
_password = password;
7372
_requestMessage = new RequestMessagePassword(ServiceName.Connection, Username, _password);
73+
_authenticationCompleted = new AutoResetEvent(false);
7474
}
7575

7676
/// <summary>
@@ -161,39 +161,6 @@ private void Session_UserAuthenticationPasswordChangeRequiredReceived(object sen
161161

162162
#region IDisposable Members
163163

164-
private bool _isDisposed;
165-
166-
/// <summary>
167-
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
168-
/// </summary>
169-
public override void Dispose()
170-
{
171-
Dispose(true);
172-
GC.SuppressFinalize(this);
173-
}
174-
175-
/// <summary>
176-
/// Releases unmanaged and - optionally - managed resources
177-
/// </summary>
178-
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
179-
protected virtual void Dispose(bool disposing)
180-
{
181-
if (_isDisposed)
182-
return;
183-
184-
if (disposing)
185-
{
186-
var authenticationCompleted = _authenticationCompleted;
187-
if (authenticationCompleted != null)
188-
{
189-
authenticationCompleted.Dispose();
190-
_authenticationCompleted = null;
191-
}
192-
193-
_isDisposed = true;
194-
}
195-
}
196-
197164
/// <summary>
198165
/// Releases unmanaged resources and performs other cleanup operations before the
199166
/// <see cref="PasswordAuthenticationMethod"/> is reclaimed by garbage collection.

src/Renci.SshNet/PrivateKeyAuthenticationMethod.cs

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ namespace Renci.SshNet
1313
/// </summary>
1414
public class PrivateKeyAuthenticationMethod : AuthenticationMethod
1515
{
16-
private AuthenticationResult _authenticationResult = AuthenticationResult.Failure;
17-
private EventWaitHandle _authenticationCompleted = new ManualResetEvent(false);
18-
private bool _isSignatureRequired;
16+
private bool _isSignatureRequired;
1917

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

4543
KeyFiles = new Collection<PrivateKeyFile>(keyFiles);
44+
_authenticationCompleted = new ManualResetEvent(false);
4645
}
4746

4847
/// <summary>
@@ -149,39 +148,6 @@ private void Session_UserAuthenticationPublicKeyReceived(object sender, MessageE
149148

150149
#region IDisposable Members
151150

152-
private bool _isDisposed;
153-
154-
/// <summary>
155-
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
156-
/// </summary>
157-
public override void Dispose()
158-
{
159-
Dispose(true);
160-
GC.SuppressFinalize(this);
161-
}
162-
163-
/// <summary>
164-
/// Releases unmanaged and - optionally - managed resources
165-
/// </summary>
166-
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
167-
protected virtual void Dispose(bool disposing)
168-
{
169-
if (_isDisposed)
170-
return;
171-
172-
if (disposing)
173-
{
174-
var authenticationCompleted = _authenticationCompleted;
175-
if (authenticationCompleted != null)
176-
{
177-
_authenticationCompleted = null;
178-
authenticationCompleted.Dispose();
179-
}
180-
181-
_isDisposed = true;
182-
}
183-
}
184-
185151
/// <summary>
186152
/// Releases unmanaged resources and performs other cleanup operations before the
187153
/// <see cref="PasswordConnectionInfo"/> is reclaimed by garbage collection.

0 commit comments

Comments
 (0)