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

Use BCL Timeout #1353

Merged
merged 2 commits into from
Mar 14, 2024
Merged
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
6 changes: 3 additions & 3 deletions src/Renci.SshNet/BaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public TimeSpan KeepAliveInterval
return;
}

if (value == SshNet.Session.InfiniteTimeSpan)
if (value == Timeout.InfiniteTimeSpan)
{
// stop the timer when the value is -1 milliseconds
StopKeepAliveTimer();
Expand Down Expand Up @@ -196,7 +196,7 @@ private protected BaseClient(ConnectionInfo connectionInfo, bool ownsConnectionI
ConnectionInfo = connectionInfo;
_ownsConnectionInfo = ownsConnectionInfo;
_serviceFactory = serviceFactory;
_keepAliveInterval = SshNet.Session.InfiniteTimeSpan;
_keepAliveInterval = Timeout.InfiniteTimeSpan;
}

/// <summary>
Expand Down Expand Up @@ -508,7 +508,7 @@ private void SendKeepAliveMessage()
/// </remarks>
private void StartKeepAliveTimer()
{
if (_keepAliveInterval == SshNet.Session.InfiniteTimeSpan)
if (_keepAliveInterval == Timeout.InfiniteTimeSpan)
{
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Renci.SshNet/Connection/ConnectorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ protected async Task<Socket> SocketConnectAsync(string host, int port, Cancellat
protected static byte SocketReadByte(Socket socket)
{
var buffer = new byte[1];
_ = SocketRead(socket, buffer, 0, 1, Session.InfiniteTimeSpan);
_ = SocketRead(socket, buffer, 0, 1, Timeout.InfiniteTimeSpan);
return buffer[0];
}

Expand All @@ -128,7 +128,7 @@ protected static byte SocketReadByte(Socket socket, TimeSpan readTimeout)
/// <exception cref="SocketException">The read failed.</exception>
protected static int SocketRead(Socket socket, byte[] buffer, int offset, int length)
{
return SocketRead(socket, buffer, offset, length, Session.InfiniteTimeSpan);
return SocketRead(socket, buffer, offset, length, Timeout.InfiniteTimeSpan);
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion src/Renci.SshNet/NetConfClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Threading;
using System.Xml;

using Renci.SshNet.Common;
Expand Down Expand Up @@ -149,7 +150,7 @@ private NetConfClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo)
internal NetConfClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFactory)
: base(connectionInfo, ownsConnectionInfo, serviceFactory)
{
_operationTimeout = SshNet.Session.Infinite;
_operationTimeout = Timeout.Infinite;
AutomaticMessageIdHandling = true;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Renci.SshNet/ScpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;

using Renci.SshNet.Channels;
using Renci.SshNet.Common;
Expand Down Expand Up @@ -211,7 +212,7 @@ private ScpClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo)
internal ScpClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFactory)
: base(connectionInfo, ownsConnectionInfo, serviceFactory)
{
OperationTimeout = SshNet.Session.InfiniteTimeSpan;
OperationTimeout = Timeout.InfiniteTimeSpan;
BufferSize = 1024 * 16;
_remotePathTransformation = serviceFactory.CreateRemotePathDoubleQuoteTransformation();
}
Expand Down
18 changes: 1 addition & 17 deletions src/Renci.SshNet/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ public class Session : ISession
internal const byte CarriageReturn = 0x0d;
internal const byte LineFeed = 0x0a;

/// <summary>
/// Specifies an infinite waiting period.
/// </summary>
/// <remarks>
/// The value of this field is <c>-1</c>.
/// </remarks>
internal const int Infinite = -1;

/// <summary>
/// Specifies maximum packet size defined by the protocol.
/// </summary>
Expand Down Expand Up @@ -74,14 +66,6 @@ public class Session : ISession
/// </remarks>
private const int LocalChannelDataPacketSize = 1024 * 64;

/// <summary>
/// Specifies an infinite waiting period.
/// </summary>
/// <remarks>
/// The value of this field is <c>-1</c> millisecond.
/// </remarks>
internal static readonly TimeSpan InfiniteTimeSpan = new TimeSpan(0, 0, 0, 0, -1);

/// <summary>
/// Holds the factory to use for creating new services.
/// </summary>
Expand Down Expand Up @@ -1895,7 +1879,7 @@ private bool IsSocketConnected()
/// <exception cref="SocketException">The read failed.</exception>
private static int TrySocketRead(Socket socket, byte[] buffer, int offset, int length)
{
return SocketAbstraction.Read(socket, buffer, offset, length, InfiniteTimeSpan);
return SocketAbstraction.Read(socket, buffer, offset, length, Timeout.InfiniteTimeSpan);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Renci.SshNet/SftpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private SftpClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo)
internal SftpClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFactory)
: base(connectionInfo, ownsConnectionInfo, serviceFactory)
{
_operationTimeout = SshNet.Session.Infinite;
_operationTimeout = Timeout.Infinite;
_bufferSize = 1024 * 32;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Renci.SshNet/SshCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ internal SshCommand(ISession session, string commandText, Encoding encoding)
_session = session;
CommandText = commandText;
_encoding = encoding;
CommandTimeout = Session.InfiniteTimeSpan;
CommandTimeout = Timeout.InfiniteTimeSpan;
_sessionErrorOccuredWaitHandle = new AutoResetEvent(initialState: false);

_session.Disconnected += Session_Disconnected;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected override void Act()
Thread.Sleep(200);

// disable further keep-alives
_client.KeepAliveInterval = Session.InfiniteTimeSpan;
_client.KeepAliveInterval = Timeout.InfiniteTimeSpan;

// wait until keep-alive has been sent at least once
Assert.IsTrue(_keepAliveSent.WaitOne(500));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void SocketShouldBeClosedAndBindShouldEndWhenForwardedPortSignalsClosingE
_remotePacketSize,
_remoteChannelNumber))));
_ = _sessionMock.Setup(p => p.WaitOnHandle(It.IsAny<EventWaitHandle>()))
.Callback<WaitHandle>(p => p.WaitOne(Session.Infinite));
.Callback<WaitHandle>(p => p.WaitOne());

var localPortEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
using (var localPortListener = new AsyncSocketListener(localPortEndPoint))
Expand Down Expand Up @@ -122,7 +122,7 @@ public void SocketShouldBeClosedAndBindShouldEndWhenOnErrorOccurredIsInvoked()
_remotePacketSize,
_remoteChannelNumber))));
_ = _sessionMock.Setup(p => p.WaitOnHandle(It.IsAny<EventWaitHandle>()))
.Callback<WaitHandle>(p => p.WaitOne(Session.Infinite));
.Callback<WaitHandle>(p => p.WaitOne());

var localPortEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
using (var localPortListener = new AsyncSocketListener(localPortEndPoint))
Expand Down Expand Up @@ -183,7 +183,7 @@ public void SocketShouldBeClosedAndEofShouldBeSentToServerWhenClientShutsDownSoc
_remoteChannelNumber))));
_ = _sessionMock.InSequence(sequence)
.Setup(p => p.WaitOnHandle(It.IsAny<EventWaitHandle>()))
.Callback<WaitHandle>(p => p.WaitOne(Session.Infinite));
.Callback<WaitHandle>(p => p.WaitOne());
_ = _sessionMock.InSequence(sequence)
.Setup(p => p.IsConnected)
.Returns(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void Signal_CurrentCountZero()
public void Wait_TimeoutInfinite_ShouldBlockUntilCountdownEventIsSet()
{
var sleep = TimeSpan.FromMilliseconds(100);
var timeout = Session.InfiniteTimeSpan;
var timeout = Timeout.InfiniteTimeSpan;

var countdownEvent = CreateCountdownEvent(1);
var signalCount = 0;
Expand Down Expand Up @@ -205,7 +205,7 @@ public void Wait_ShouldReturnFalseWhenTimeoutExpiresBeforeCountdownEventIsSet()
Assert.IsFalse(countdownEvent.IsSet);
Assert.IsFalse(countdownEvent.WaitHandle.WaitOne(0));

_ = countdownEvent.Wait(Session.InfiniteTimeSpan);
_ = countdownEvent.Wait(Timeout.InfiniteTimeSpan);
countdownEvent.Dispose();
}

Expand All @@ -225,7 +225,7 @@ public void WaitHandle_ShouldAlwaysReturnSameInstance()
public void WaitHandle_WaitOne_TimeoutInfinite_ShouldBlockUntilCountdownEventIsSet()
{
var sleep = TimeSpan.FromMilliseconds(100);
var timeout = Session.InfiniteTimeSpan;
var timeout = Timeout.InfiniteTimeSpan;

var countdownEvent = CreateCountdownEvent(1);
var signalCount = 0;
Expand Down Expand Up @@ -341,7 +341,7 @@ public void WaitHandle_WaitOne_ShouldReturnFalseWhenTimeoutExpiresBeforeCountdow
Assert.IsFalse(countdownEvent.IsSet);
Assert.IsFalse(countdownEvent.WaitHandle.WaitOne(0));

_ = countdownEvent.Wait(Session.InfiniteTimeSpan);
_ = countdownEvent.Wait(Timeout.InfiniteTimeSpan);
countdownEvent.Dispose();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void ISession_TryWait_WaitHandleAndTimeout_ShouldReturnDisconnected()
var session = (ISession)_session;
var waitHandle = new ManualResetEvent(false);

var result = session.TryWait(waitHandle, Session.InfiniteTimeSpan);
var result = session.TryWait(waitHandle, Timeout.InfiniteTimeSpan);

Assert.AreEqual(WaitResult.Disconnected, result);
}
Expand All @@ -166,7 +166,7 @@ public void ISession_TryWait_WaitHandleAndTimeoutAndException_ShouldReturnDiscon
var session = (ISession)_session;
var waitHandle = new ManualResetEvent(false);

var result = session.TryWait(waitHandle, Session.InfiniteTimeSpan, out var exception);
var result = session.TryWait(waitHandle, Timeout.InfiniteTimeSpan, out var exception);

Assert.AreEqual(WaitResult.Disconnected, result);
Assert.IsNull(exception);
Expand Down Expand Up @@ -242,7 +242,7 @@ public void ISession_WaitOnHandle_WaitHandleAndTimeout_ShouldThrowArgumentNullEx

try
{
session.WaitOnHandle(waitHandle, Session.InfiniteTimeSpan);
session.WaitOnHandle(waitHandle, Timeout.InfiniteTimeSpan);
Assert.Fail();
}
catch (ArgumentNullException ex)
Expand Down
4 changes: 2 additions & 2 deletions test/Renci.SshNet.Tests/Classes/SessionTest_Connected.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public void ISession_TryWait_WaitHandleAndTimeout_ShouldThrowArgumentNullExcepti

try
{
_ = session.TryWait(waitHandle, Session.InfiniteTimeSpan);
_ = session.TryWait(waitHandle, Timeout.InfiniteTimeSpan);
Assert.Fail();
}
catch (ArgumentNullException ex)
Expand Down Expand Up @@ -246,7 +246,7 @@ public void ISession_TryWait_WaitHandleAndTimeoutAndException_ShouldThrowArgumen

try
{
session.TryWait(waitHandle, Session.InfiniteTimeSpan, out exception);
session.TryWait(waitHandle, Timeout.InfiniteTimeSpan, out exception);
Assert.Fail();
}
catch (ArgumentNullException ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void ISession_WaitOnHandle_WaitHandleAndTimeout_ShouldThrowSshConnectionE

try
{
session.WaitOnHandle(waitHandle, Session.InfiniteTimeSpan);
session.WaitOnHandle(waitHandle, Timeout.InfiniteTimeSpan);
Assert.Fail();
}
catch (SshConnectionException ex)
Expand All @@ -177,7 +177,7 @@ public void ISession_TryWait_WaitHandleAndTimeout_ShouldReturnDisconnected()
var session = (ISession) Session;
var waitHandle = new ManualResetEvent(false);

var result = session.TryWait(waitHandle, Session.InfiniteTimeSpan);
var result = session.TryWait(waitHandle, Timeout.InfiniteTimeSpan);

Assert.AreEqual(WaitResult.Disconnected, result);
}
Expand All @@ -188,7 +188,7 @@ public void ISession_TryWait_WaitHandleAndTimeoutAndException_ShouldReturnDiscon
var session = (ISession) Session;
var waitHandle = new ManualResetEvent(false);

var result = session.TryWait(waitHandle, Session.InfiniteTimeSpan, out var exception);
var result = session.TryWait(waitHandle, Timeout.InfiniteTimeSpan, out var exception);

Assert.AreEqual(WaitResult.Disconnected, result);
Assert.IsNull(exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void ISession_WaitOnHandle_WaitHandleAndTimeout_ShouldThrowSshConnectionE

try
{
session.WaitOnHandle(waitHandle, Session.InfiniteTimeSpan);
session.WaitOnHandle(waitHandle, Timeout.InfiniteTimeSpan);
Assert.Fail();
}
catch (SshConnectionException ex)
Expand All @@ -173,7 +173,7 @@ public void ISession_TryWait_WaitHandleAndTimeout_ShouldReturnDisconnected()
var session = (ISession) Session;
var waitHandle = new ManualResetEvent(false);

var result = session.TryWait(waitHandle, Session.InfiniteTimeSpan);
var result = session.TryWait(waitHandle, Timeout.InfiniteTimeSpan);

Assert.AreEqual(WaitResult.Disconnected, result);
}
Expand All @@ -184,7 +184,7 @@ public void ISession_TryWait_WaitHandleAndTimeoutAndException_ShouldReturnDiscon
var session = (ISession) Session;
var waitHandle = new ManualResetEvent(false);

var result = session.TryWait(waitHandle, Session.InfiniteTimeSpan, out var exception);
var result = session.TryWait(waitHandle, Timeout.InfiniteTimeSpan, out var exception);

Assert.AreEqual(WaitResult.Disconnected, result);
Assert.IsNull(exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void ISession_WaitOnHandleAndTimeout_WaitHandle_ShouldThrowSshConnectionE

try
{
session.WaitOnHandle(waitHandle, Session.InfiniteTimeSpan);
session.WaitOnHandle(waitHandle, Timeout.InfiniteTimeSpan);
Assert.Fail();
}
catch (SshConnectionException ex)
Expand All @@ -197,7 +197,7 @@ public void ISession_TryWait_WaitHandleAndTimeout_ShouldReturnDisconnected()
var session = (ISession) Session;
var waitHandle = new ManualResetEvent(false);

var result = session.TryWait(waitHandle, Session.InfiniteTimeSpan);
var result = session.TryWait(waitHandle, Timeout.InfiniteTimeSpan);

Assert.AreEqual(WaitResult.Disconnected, result);
}
Expand All @@ -208,7 +208,7 @@ public void ISession_TryWait_WaitHandleAndTimeoutAndException_ShouldReturnDiscon
var session = (ISession) Session;
var waitHandle = new ManualResetEvent(false);

var result = session.TryWait(waitHandle, Session.InfiniteTimeSpan, out var exception);
var result = session.TryWait(waitHandle, Timeout.InfiniteTimeSpan, out var exception);

Assert.AreEqual(WaitResult.Disconnected, result);
Assert.IsNull(exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public void ISession_WaitOnHandle_WaitHandleAndTimeout_ShouldThrowSshConnectionE

try
{
session.WaitOnHandle(waitHandle, Session.InfiniteTimeSpan);
session.WaitOnHandle(waitHandle, Timeout.InfiniteTimeSpan);
Assert.Fail();
}
catch (SshConnectionException ex)
Expand All @@ -202,7 +202,7 @@ public void ISession_TryWait_WaitHandleAndTimeout_ShouldReturnDisconnected()
var session = (ISession) Session;
var waitHandle = new ManualResetEvent(false);

var result = session.TryWait(waitHandle, Session.InfiniteTimeSpan);
var result = session.TryWait(waitHandle, Timeout.InfiniteTimeSpan);

Assert.AreEqual(WaitResult.Disconnected, result);
}
Expand All @@ -213,7 +213,7 @@ public void ISession_TryWait_WaitHandleAndTimeoutAndException_ShouldReturnDiscon
var session = (ISession) Session;
var waitHandle = new ManualResetEvent(false);

var result = session.TryWait(waitHandle, Session.InfiniteTimeSpan, out var exception);
var result = session.TryWait(waitHandle, Timeout.InfiniteTimeSpan, out var exception);

Assert.AreEqual(WaitResult.Disconnected, result);
Assert.IsNull(exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public void ISession_TryWait_WaitHandleAndTimeout_ShouldReturnDisconnected()
var session = (ISession) Session;
var waitHandle = new ManualResetEvent(false);

var result = session.TryWait(waitHandle, Session.InfiniteTimeSpan);
var result = session.TryWait(waitHandle, Timeout.InfiniteTimeSpan);

Assert.AreEqual(WaitResult.Disconnected, result);
}
Expand All @@ -194,7 +194,7 @@ public void ISession_TryWait_WaitHandleAndTimeoutAndException_ShouldReturnDiscon
var session = (ISession) Session;
var waitHandle = new ManualResetEvent(false);

var result = session.TryWait(waitHandle, Session.InfiniteTimeSpan, out var exception);
var result = session.TryWait(waitHandle, Timeout.InfiniteTimeSpan, out var exception);

Assert.AreEqual(WaitResult.Disconnected, result);
Assert.IsNull(exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public void ISession_TryWait_WaitHandleAndTimeout_ShouldReturnFailed()
var session = (ISession) Session;
var waitHandle = new ManualResetEvent(false);

var result = session.TryWait(waitHandle, Session.InfiniteTimeSpan);
var result = session.TryWait(waitHandle, Timeout.InfiniteTimeSpan);

Assert.AreEqual(WaitResult.Failed, result);
}
Expand All @@ -206,7 +206,7 @@ public void ISession_TryWait_WaitHandleAndTimeoutAndException_ShouldReturnFailed
var session = (ISession) Session;
var waitHandle = new ManualResetEvent(false);

var result = session.TryWait(waitHandle, Session.InfiniteTimeSpan, out var exception);
var result = session.TryWait(waitHandle, Timeout.InfiniteTimeSpan, out var exception);

Assert.AreEqual(WaitResult.Failed, result);
Assert.IsNotNull(exception);
Expand Down
Loading