Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 2 additions & 9 deletions src/Renci.SshNet/BaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -448,17 +448,10 @@ protected virtual void Dispose(bool disposing)
/// <summary>
/// Check if the current instance is disposed.
/// </summary>
/// <exception cref="ObjectDisposedException">THe current instance is disposed.</exception>
/// <exception cref="ObjectDisposedException">The current instance is disposed.</exception>
protected void CheckDisposed()
{
#if NET7_0_OR_GREATER
ObjectDisposedException.ThrowIf(_isDisposed, this);
#else
if (_isDisposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
#endif // NET7_0_OR_GREATER
ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this);
}

/// <summary>
Expand Down
10 changes: 1 addition & 9 deletions src/Renci.SshNet/Common/ChannelInputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,7 @@ public override void Write(byte[] buffer, int offset, int count)
throw new ArgumentOutOfRangeException(nameof(offset), "offset or count is negative.");
}

if (_isDisposed)
{
throw CreateObjectDisposedException();
}
ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this);

if (count == 0)
{
Expand Down Expand Up @@ -208,10 +205,5 @@ public override long Position
get { return _totalPosition; }
set { throw new NotSupportedException(); }
}

private ObjectDisposedException CreateObjectDisposedException()
{
return new ObjectDisposedException(GetType().FullName);
}
}
}
14 changes: 1 addition & 13 deletions src/Renci.SshNet/Common/PipeStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public override void Write(byte[] buffer, int offset, int count)
{
lock (_sync)
{
ThrowIfDisposed();
ThrowHelper.ThrowObjectDisposedIf(_disposed, this);

AssertValid();

Expand Down Expand Up @@ -232,17 +232,5 @@ public override long Position
get { return 0; }
set { throw new NotSupportedException(); }
}

private void ThrowIfDisposed()
{
#if NET7_0_OR_GREATER
ObjectDisposedException.ThrowIf(_disposed, this);
#else
if (_disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
#endif // NET7_0_OR_GREATER
}
}
}
25 changes: 25 additions & 0 deletions src/Renci.SshNet/Common/ThrowHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#nullable enable
using System;

namespace Renci.SshNet.Common
{
internal static class ThrowHelper
{
public static void ThrowObjectDisposedIf(bool condition, object instance)
{
#if NET7_0_OR_GREATER
ObjectDisposedException.ThrowIf(condition, instance);
#else
if (condition)
{
Throw(instance);

static void Throw(object? instance)
{
throw new ObjectDisposedException(instance?.GetType().FullName);
}
}
#endif
}
}
}
9 changes: 1 addition & 8 deletions src/Renci.SshNet/ForwardedPortDynamic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,7 @@ protected override void StopPort(TimeSpan timeout)
/// <exception cref="ObjectDisposedException">The current instance is disposed.</exception>
protected override void CheckDisposed()
{
#if NET7_0_OR_GREATER
ObjectDisposedException.ThrowIf(_isDisposed, this);
#else
if (_isDisposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
#endif // NET7_0_OR_GREATER
ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this);
}

/// <summary>
Expand Down
9 changes: 1 addition & 8 deletions src/Renci.SshNet/ForwardedPortLocal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,7 @@ protected override void StopPort(TimeSpan timeout)
/// <exception cref="ObjectDisposedException">The current instance is disposed.</exception>
protected override void CheckDisposed()
{
#if NET7_0_OR_GREATER
ObjectDisposedException.ThrowIf(_isDisposed, this);
#else
if (_isDisposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
#endif // NET7_0_OR_GREATER
ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this);
}

/// <summary>
Expand Down
9 changes: 1 addition & 8 deletions src/Renci.SshNet/ForwardedPortRemote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,7 @@ protected override void StopPort(TimeSpan timeout)
/// <exception cref="ObjectDisposedException">The current instance is disposed.</exception>
protected override void CheckDisposed()
{
#if NET7_0_OR_GREATER
ObjectDisposedException.ThrowIf(_isDisposed, this);
#else
if (_isDisposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
#endif // NET7_0_OR_GREATER
ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this);
}

private void Session_ChannelOpening(object sender, MessageEventArgs<ChannelOpenMessage> e)
Expand Down
2 changes: 1 addition & 1 deletion src/Renci.SshNet/Renci.SshNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<AssemblyName>Renci.SshNet</AssemblyName>
<Product>SSH.NET</Product>
<AssemblyTitle>SSH.NET</AssemblyTitle>
<TargetFrameworks>net462;netstandard2.0;netstandard2.1;net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net462;netstandard2.0;netstandard2.1;net6.0;net8.0</TargetFrameworks>
</PropertyGroup>

<PropertyGroup>
Expand Down
9 changes: 1 addition & 8 deletions src/Renci.SshNet/Sftp/SftpFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,7 @@ public SftpFileReader(byte[] handle, ISftpSession sftpSession, uint chunkSize, i

public byte[] Read()
{
#if NET7_0_OR_GREATER
ObjectDisposedException.ThrowIf(_disposingOrDisposed, this);
#else
if (_disposingOrDisposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
#endif // NET7_0_OR_GREATER
ThrowHelper.ThrowObjectDisposedIf(_disposingOrDisposed, this);

if (_exception is not null)
{
Expand Down
9 changes: 1 addition & 8 deletions src/Renci.SshNet/Sftp/SftpFileStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1364,14 +1364,7 @@ private void SetupWrite()

private void CheckSessionIsOpen()
{
#if NET7_0_OR_GREATER
ObjectDisposedException.ThrowIf(_session is null, this);
#else
if (_session is null)
{
throw new ObjectDisposedException(GetType().FullName);
}
#endif // NET7_0_OR_GREATER
ThrowHelper.ThrowObjectDisposedIf(_session is null, this);

if (!_session.IsOpen)
{
Expand Down
16 changes: 2 additions & 14 deletions src/Renci.SshNet/ShellStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public override bool CanWrite
/// <inheritdoc/>
public override void Flush()
{
ThrowIfDisposed();
ThrowHelper.ThrowObjectDisposedIf(_disposed, this);

Debug.Assert(_writeLength >= 0 && _writeLength <= _writeBuffer.Length);

Expand Down Expand Up @@ -766,18 +766,6 @@ private static void ValidateLookback(int lookback)
}
}

private void ThrowIfDisposed()
{
#if NET7_0_OR_GREATER
ObjectDisposedException.ThrowIf(_disposed, this);
#else
if (_disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
#endif // NET7_0_OR_GREATER
}

/// <summary>
/// Reads all of the text currently available in the shell.
/// </summary>
Expand Down Expand Up @@ -847,7 +835,7 @@ public void Write(string? text)
/// <inheritdoc/>
public override void Write(byte[] buffer, int offset, int count)
{
ThrowIfDisposed();
ThrowHelper.ThrowObjectDisposedIf(_disposed, this);

while (count > 0)
{
Expand Down
9 changes: 1 addition & 8 deletions src/Renci.SshNet/SshCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,7 @@ internal SshCommand(ISession session, string commandText, Encoding encoding)
#pragma warning disable CA1849 // Call async methods when in an async method; PipeStream.DisposeAsync would complete synchronously anyway.
public Task ExecuteAsync(CancellationToken cancellationToken = default)
{
#if NET7_0_OR_GREATER
ObjectDisposedException.ThrowIf(_isDisposed, this);
#else
if (_isDisposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
#endif
ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this);

if (cancellationToken.IsCancellationRequested)
{
Expand Down
18 changes: 3 additions & 15 deletions src/Renci.SshNet/SubsystemSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal IChannelSession Channel
{
get
{
EnsureNotDisposed();
ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this);

return _channel;
}
Expand Down Expand Up @@ -106,7 +106,7 @@ protected SubsystemSession(ISession session, string subsystemName, int operation
/// <exception cref="SshException">The channel session could not be opened, or the subsystem could not be executed.</exception>
public void Connect()
{
EnsureNotDisposed();
ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this);

if (IsOpen)
{
Expand Down Expand Up @@ -166,7 +166,7 @@ public void Disconnect()
/// <param name="data">The data to be sent.</param>
public void SendData(byte[] data)
{
EnsureNotDisposed();
ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this);
EnsureSessionIsOpen();

_channel.SendData(data);
Expand Down Expand Up @@ -544,17 +544,5 @@ protected virtual void Dispose(bool disposing)
{
Dispose(disposing: false);
}

private void EnsureNotDisposed()
{
#if NET7_0_OR_GREATER
ObjectDisposedException.ThrowIf(_isDisposed, this);
#else
if (_isDisposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
#endif // NET7_0_OR_GREATER
}
}
}
2 changes: 1 addition & 1 deletion test/Renci.SshNet.Tests/Renci.SshNet.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net462;net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net462;net6.0;net8.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down