Skip to content

Commit 4cab7e5

Browse files
authored
enable nullable on BaseClient and SftpClient (#1339)
* enable nullable on BaseClient and SftpClient * SftpClient: use nameof for ArgumentException
1 parent 17f4030 commit 4cab7e5

File tree

4 files changed

+67
-59
lines changed

4 files changed

+67
-59
lines changed

src/Renci.SshNet/BaseClient.cs

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
#nullable enable
2+
using System;
23
using System.Net.Sockets;
34
using System.Threading;
45
using System.Threading.Tasks;
@@ -22,7 +23,7 @@ public abstract class BaseClient : IBaseClient
2223
private readonly IServiceFactory _serviceFactory;
2324
private readonly object _keepAliveLock = new object();
2425
private TimeSpan _keepAliveInterval;
25-
private Timer _keepAliveTimer;
26+
private Timer? _keepAliveTimer;
2627
private ConnectionInfo _connectionInfo;
2728
private bool _isDisposed;
2829

@@ -32,7 +33,7 @@ public abstract class BaseClient : IBaseClient
3233
/// <value>
3334
/// The current session.
3435
/// </value>
35-
internal ISession Session { get; private set; }
36+
internal ISession? Session { get; private set; }
3637

3738
/// <summary>
3839
/// Gets the factory for creating new services.
@@ -142,17 +143,17 @@ public TimeSpan KeepAliveInterval
142143
/// <summary>
143144
/// Occurs when an error occurred.
144145
/// </summary>
145-
public event EventHandler<ExceptionEventArgs> ErrorOccurred;
146+
public event EventHandler<ExceptionEventArgs>? ErrorOccurred;
146147

147148
/// <summary>
148149
/// Occurs when host key received.
149150
/// </summary>
150-
public event EventHandler<HostKeyEventArgs> HostKeyReceived;
151+
public event EventHandler<HostKeyEventArgs>? HostKeyReceived;
151152

152153
/// <summary>
153154
/// Occurs when server identification received.
154155
/// </summary>
155-
public event EventHandler<SshIdentificationEventArgs> ServerIdentificationReceived;
156+
public event EventHandler<SshIdentificationEventArgs>? ServerIdentificationReceived;
156157

157158
/// <summary>
158159
/// Initializes a new instance of the <see cref="BaseClient"/> class.
@@ -193,7 +194,7 @@ private protected BaseClient(ConnectionInfo connectionInfo, bool ownsConnectionI
193194
throw new ArgumentNullException(nameof(serviceFactory));
194195
}
195196

196-
ConnectionInfo = connectionInfo;
197+
_connectionInfo = connectionInfo;
197198
_ownsConnectionInfo = ownsConnectionInfo;
198199
_serviceFactory = serviceFactory;
199200
_keepAliveInterval = Timeout.InfiniteTimeSpan;
@@ -391,17 +392,17 @@ protected virtual void OnDisconnected()
391392
{
392393
}
393394

394-
private void Session_ErrorOccured(object sender, ExceptionEventArgs e)
395+
private void Session_ErrorOccured(object? sender, ExceptionEventArgs e)
395396
{
396397
ErrorOccurred?.Invoke(this, e);
397398
}
398399

399-
private void Session_HostKeyReceived(object sender, HostKeyEventArgs e)
400+
private void Session_HostKeyReceived(object? sender, HostKeyEventArgs e)
400401
{
401402
HostKeyReceived?.Invoke(this, e);
402403
}
403404

404-
private void Session_ServerIdentificationReceived(object sender, SshIdentificationEventArgs e)
405+
private void Session_ServerIdentificationReceived(object? sender, SshIdentificationEventArgs e)
405406
{
406407
ServerIdentificationReceived?.Invoke(this, e);
407408
}
@@ -432,14 +433,12 @@ protected virtual void Dispose(bool disposing)
432433

433434
Disconnect();
434435

435-
if (_ownsConnectionInfo && _connectionInfo is not null)
436+
if (_ownsConnectionInfo)
436437
{
437438
if (_connectionInfo is IDisposable connectionInfoDisposable)
438439
{
439440
connectionInfoDisposable.Dispose();
440441
}
441-
442-
_connectionInfo = null;
443442
}
444443

445444
_isDisposed = true;

src/Renci.SshNet/IBaseClient.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
#nullable enable
2+
using System;
23
using System.Net.Sockets;
34
using System.Threading;
45
using System.Threading.Tasks;

src/Renci.SshNet/ISftpClient.cs

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
#nullable enable
2+
using System;
23
using System.Collections.Generic;
34
using System.IO;
45
using System.Text;
@@ -192,7 +193,7 @@ public interface ISftpClient : IBaseClient
192193
/// <remarks>
193194
/// Method calls made by this method to <paramref name="output" />, may under certain conditions result in exceptions thrown by the stream.
194195
/// </remarks>
195-
IAsyncResult BeginDownloadFile(string path, Stream output, AsyncCallback asyncCallback);
196+
IAsyncResult BeginDownloadFile(string path, Stream output, AsyncCallback? asyncCallback);
196197

197198
/// <summary>
198199
/// Begins an asynchronous file downloading into the stream.
@@ -211,7 +212,7 @@ public interface ISftpClient : IBaseClient
211212
/// <remarks>
212213
/// Method calls made by this method to <paramref name="output" />, may under certain conditions result in exceptions thrown by the stream.
213214
/// </remarks>
214-
IAsyncResult BeginDownloadFile(string path, Stream output, AsyncCallback asyncCallback, object state, Action<ulong> downloadCallback = null);
215+
IAsyncResult BeginDownloadFile(string path, Stream output, AsyncCallback? asyncCallback, object? state, Action<ulong>? downloadCallback = null);
215216

216217
/// <summary>
217218
/// Begins an asynchronous operation of retrieving list of files in remote directory.
@@ -224,7 +225,7 @@ public interface ISftpClient : IBaseClient
224225
/// An <see cref="IAsyncResult" /> that references the asynchronous operation.
225226
/// </returns>
226227
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
227-
IAsyncResult BeginListDirectory(string path, AsyncCallback asyncCallback, object state, Action<int> listCallback = null);
228+
IAsyncResult BeginListDirectory(string path, AsyncCallback? asyncCallback, object? state, Action<int>? listCallback = null);
228229

229230
/// <summary>
230231
/// Begins the synchronize directories.
@@ -240,7 +241,7 @@ public interface ISftpClient : IBaseClient
240241
/// <exception cref="ArgumentNullException"><paramref name="sourcePath"/> is <see langword="null"/>.</exception>
241242
/// <exception cref="ArgumentException"><paramref name="destinationPath"/> is <see langword="null"/> or contains only whitespace.</exception>
242243
/// <exception cref="SshException">If a problem occurs while copying the file.</exception>
243-
IAsyncResult BeginSynchronizeDirectories(string sourcePath, string destinationPath, string searchPattern, AsyncCallback asyncCallback, object state);
244+
IAsyncResult BeginSynchronizeDirectories(string sourcePath, string destinationPath, string searchPattern, AsyncCallback? asyncCallback, object? state);
244245

245246
/// <summary>
246247
/// Begins an asynchronous uploading the stream into remote file.
@@ -289,7 +290,7 @@ public interface ISftpClient : IBaseClient
289290
/// If the remote file already exists, it is overwritten and truncated.
290291
/// </para>
291292
/// </remarks>
292-
IAsyncResult BeginUploadFile(Stream input, string path, AsyncCallback asyncCallback);
293+
IAsyncResult BeginUploadFile(Stream input, string path, AsyncCallback? asyncCallback);
293294

294295
/// <summary>
295296
/// Begins an asynchronous uploading the stream into remote file.
@@ -316,7 +317,7 @@ public interface ISftpClient : IBaseClient
316317
/// If the remote file already exists, it is overwritten and truncated.
317318
/// </para>
318319
/// </remarks>
319-
IAsyncResult BeginUploadFile(Stream input, string path, AsyncCallback asyncCallback, object state, Action<ulong> uploadCallback = null);
320+
IAsyncResult BeginUploadFile(Stream input, string path, AsyncCallback? asyncCallback, object? state, Action<ulong>? uploadCallback = null);
320321

321322
/// <summary>
322323
/// Begins an asynchronous uploading the stream into remote file.
@@ -343,7 +344,7 @@ public interface ISftpClient : IBaseClient
343344
/// <see cref="SshException"/>.
344345
/// </para>
345346
/// </remarks>
346-
IAsyncResult BeginUploadFile(Stream input, string path, bool canOverride, AsyncCallback asyncCallback, object state, Action<ulong> uploadCallback = null);
347+
IAsyncResult BeginUploadFile(Stream input, string path, bool canOverride, AsyncCallback? asyncCallback, object? state, Action<ulong>? uploadCallback = null);
347348

348349
/// <summary>
349350
/// Changes remote directory to path.
@@ -522,7 +523,7 @@ public interface ISftpClient : IBaseClient
522523
/// <remarks>
523524
/// Method calls made by this method to <paramref name="output" />, may under certain conditions result in exceptions thrown by the stream.
524525
/// </remarks>
525-
void DownloadFile(string path, Stream output, Action<ulong> downloadCallback = null);
526+
void DownloadFile(string path, Stream output, Action<ulong>? downloadCallback = null);
526527

527528
/// <summary>
528529
/// Ends an asynchronous file downloading into the stream.
@@ -698,7 +699,7 @@ public interface ISftpClient : IBaseClient
698699
/// <exception cref="SftpPermissionDeniedException">Permission to list the contents of the directory was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>
699700
/// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the remote host.</exception>
700701
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
701-
IEnumerable<ISftpFile> ListDirectory(string path, Action<int> listCallback = null);
702+
IEnumerable<ISftpFile> ListDirectory(string path, Action<int>? listCallback = null);
702703

703704
/// <summary>
704705
/// Asynchronously enumerates the files in remote directory.
@@ -1004,7 +1005,7 @@ public interface ISftpClient : IBaseClient
10041005
/// <remarks>
10051006
/// Method calls made by this method to <paramref name="input" />, may under certain conditions result in exceptions thrown by the stream.
10061007
/// </remarks>
1007-
void UploadFile(Stream input, string path, Action<ulong> uploadCallback = null);
1008+
void UploadFile(Stream input, string path, Action<ulong>? uploadCallback = null);
10081009

10091010
/// <summary>
10101011
/// Uploads stream into remote file.
@@ -1022,7 +1023,7 @@ public interface ISftpClient : IBaseClient
10221023
/// <remarks>
10231024
/// Method calls made by this method to <paramref name="input" />, may under certain conditions result in exceptions thrown by the stream.
10241025
/// </remarks>
1025-
void UploadFile(Stream input, string path, bool canOverride, Action<ulong> uploadCallback = null);
1026+
void UploadFile(Stream input, string path, bool canOverride, Action<ulong>? uploadCallback = null);
10261027

10271028
/// <summary>
10281029
/// Writes the specified byte array to the specified file, and closes the file.

0 commit comments

Comments
 (0)