Skip to content

Commit 3e6fc4f

Browse files
authored
Use BCL Timeout (#1353)
1 parent b3c68ba commit 3e6fc4f

21 files changed

+50
-64
lines changed

src/Renci.SshNet/BaseClient.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public TimeSpan KeepAliveInterval
108108
return;
109109
}
110110

111-
if (value == SshNet.Session.InfiniteTimeSpan)
111+
if (value == Timeout.InfiniteTimeSpan)
112112
{
113113
// stop the timer when the value is -1 milliseconds
114114
StopKeepAliveTimer();
@@ -196,7 +196,7 @@ private protected BaseClient(ConnectionInfo connectionInfo, bool ownsConnectionI
196196
ConnectionInfo = connectionInfo;
197197
_ownsConnectionInfo = ownsConnectionInfo;
198198
_serviceFactory = serviceFactory;
199-
_keepAliveInterval = SshNet.Session.InfiniteTimeSpan;
199+
_keepAliveInterval = Timeout.InfiniteTimeSpan;
200200
}
201201

202202
/// <summary>
@@ -508,7 +508,7 @@ private void SendKeepAliveMessage()
508508
/// </remarks>
509509
private void StartKeepAliveTimer()
510510
{
511-
if (_keepAliveInterval == SshNet.Session.InfiniteTimeSpan)
511+
if (_keepAliveInterval == Timeout.InfiniteTimeSpan)
512512
{
513513
return;
514514
}

src/Renci.SshNet/Connection/ConnectorBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ protected async Task<Socket> SocketConnectAsync(string host, int port, Cancellat
103103
protected static byte SocketReadByte(Socket socket)
104104
{
105105
var buffer = new byte[1];
106-
_ = SocketRead(socket, buffer, 0, 1, Session.InfiniteTimeSpan);
106+
_ = SocketRead(socket, buffer, 0, 1, Timeout.InfiniteTimeSpan);
107107
return buffer[0];
108108
}
109109

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

134134
/// <summary>

src/Renci.SshNet/NetConfClient.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Diagnostics.CodeAnalysis;
33
using System.Net;
4+
using System.Threading;
45
using System.Xml;
56

67
using Renci.SshNet.Common;
@@ -149,7 +150,7 @@ private NetConfClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo)
149150
internal NetConfClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFactory)
150151
: base(connectionInfo, ownsConnectionInfo, serviceFactory)
151152
{
152-
_operationTimeout = SshNet.Session.Infinite;
153+
_operationTimeout = Timeout.Infinite;
153154
AutomaticMessageIdHandling = true;
154155
}
155156

src/Renci.SshNet/ScpClient.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using System.Net;
77
using System.Text.RegularExpressions;
8+
using System.Threading;
89

910
using Renci.SshNet.Channels;
1011
using Renci.SshNet.Common;
@@ -211,7 +212,7 @@ private ScpClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo)
211212
internal ScpClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFactory)
212213
: base(connectionInfo, ownsConnectionInfo, serviceFactory)
213214
{
214-
OperationTimeout = SshNet.Session.InfiniteTimeSpan;
215+
OperationTimeout = Timeout.InfiniteTimeSpan;
215216
BufferSize = 1024 * 16;
216217
_remotePathTransformation = serviceFactory.CreateRemotePathDoubleQuoteTransformation();
217218
}

src/Renci.SshNet/Session.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,6 @@ public class Session : ISession
3030
internal const byte CarriageReturn = 0x0d;
3131
internal const byte LineFeed = 0x0a;
3232

33-
/// <summary>
34-
/// Specifies an infinite waiting period.
35-
/// </summary>
36-
/// <remarks>
37-
/// The value of this field is <c>-1</c>.
38-
/// </remarks>
39-
internal const int Infinite = -1;
40-
4133
/// <summary>
4234
/// Specifies maximum packet size defined by the protocol.
4335
/// </summary>
@@ -74,14 +66,6 @@ public class Session : ISession
7466
/// </remarks>
7567
private const int LocalChannelDataPacketSize = 1024 * 64;
7668

77-
/// <summary>
78-
/// Specifies an infinite waiting period.
79-
/// </summary>
80-
/// <remarks>
81-
/// The value of this field is <c>-1</c> millisecond.
82-
/// </remarks>
83-
internal static readonly TimeSpan InfiniteTimeSpan = new TimeSpan(0, 0, 0, 0, -1);
84-
8569
/// <summary>
8670
/// Holds the factory to use for creating new services.
8771
/// </summary>
@@ -1895,7 +1879,7 @@ private bool IsSocketConnected()
18951879
/// <exception cref="SocketException">The read failed.</exception>
18961880
private static int TrySocketRead(Socket socket, byte[] buffer, int offset, int length)
18971881
{
1898-
return SocketAbstraction.Read(socket, buffer, offset, length, InfiniteTimeSpan);
1882+
return SocketAbstraction.Read(socket, buffer, offset, length, Timeout.InfiniteTimeSpan);
18991883
}
19001884

19011885
/// <summary>

src/Renci.SshNet/SftpClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ private SftpClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo)
259259
internal SftpClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFactory)
260260
: base(connectionInfo, ownsConnectionInfo, serviceFactory)
261261
{
262-
_operationTimeout = SshNet.Session.Infinite;
262+
_operationTimeout = Timeout.Infinite;
263263
_bufferSize = 1024 * 32;
264264
}
265265

src/Renci.SshNet/SshCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ internal SshCommand(ISession session, string commandText, Encoding encoding)
184184
_session = session;
185185
CommandText = commandText;
186186
_encoding = encoding;
187-
CommandTimeout = Session.InfiniteTimeSpan;
187+
CommandTimeout = Timeout.InfiniteTimeSpan;
188188
_sessionErrorOccuredWaitHandle = new AutoResetEvent(initialState: false);
189189

190190
_session.Disconnected += Session_Disconnected;

test/Renci.SshNet.Tests/Classes/BaseClientTest_Connected_KeepAlivesNotSentConcurrently.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ protected override void Act()
7070
Thread.Sleep(200);
7171

7272
// disable further keep-alives
73-
_client.KeepAliveInterval = Session.InfiniteTimeSpan;
73+
_client.KeepAliveInterval = Timeout.InfiniteTimeSpan;
7474

7575
// wait until keep-alive has been sent at least once
7676
Assert.IsTrue(_keepAliveSent.WaitOne(500));

test/Renci.SshNet.Tests/Classes/Channels/ChannelDirectTcpipTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void SocketShouldBeClosedAndBindShouldEndWhenForwardedPortSignalsClosingE
6666
_remotePacketSize,
6767
_remoteChannelNumber))));
6868
_ = _sessionMock.Setup(p => p.WaitOnHandle(It.IsAny<EventWaitHandle>()))
69-
.Callback<WaitHandle>(p => p.WaitOne(Session.Infinite));
69+
.Callback<WaitHandle>(p => p.WaitOne());
7070

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

127127
var localPortEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
128128
using (var localPortListener = new AsyncSocketListener(localPortEndPoint))
@@ -183,7 +183,7 @@ public void SocketShouldBeClosedAndEofShouldBeSentToServerWhenClientShutsDownSoc
183183
_remoteChannelNumber))));
184184
_ = _sessionMock.InSequence(sequence)
185185
.Setup(p => p.WaitOnHandle(It.IsAny<EventWaitHandle>()))
186-
.Callback<WaitHandle>(p => p.WaitOne(Session.Infinite));
186+
.Callback<WaitHandle>(p => p.WaitOne());
187187
_ = _sessionMock.InSequence(sequence)
188188
.Setup(p => p.IsConnected)
189189
.Returns(true);

test/Renci.SshNet.Tests/Classes/Common/CountdownEventTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void Signal_CurrentCountZero()
8989
public void Wait_TimeoutInfinite_ShouldBlockUntilCountdownEventIsSet()
9090
{
9191
var sleep = TimeSpan.FromMilliseconds(100);
92-
var timeout = Session.InfiniteTimeSpan;
92+
var timeout = Timeout.InfiniteTimeSpan;
9393

9494
var countdownEvent = CreateCountdownEvent(1);
9595
var signalCount = 0;
@@ -205,7 +205,7 @@ public void Wait_ShouldReturnFalseWhenTimeoutExpiresBeforeCountdownEventIsSet()
205205
Assert.IsFalse(countdownEvent.IsSet);
206206
Assert.IsFalse(countdownEvent.WaitHandle.WaitOne(0));
207207

208-
_ = countdownEvent.Wait(Session.InfiniteTimeSpan);
208+
_ = countdownEvent.Wait(Timeout.InfiniteTimeSpan);
209209
countdownEvent.Dispose();
210210
}
211211

@@ -225,7 +225,7 @@ public void WaitHandle_ShouldAlwaysReturnSameInstance()
225225
public void WaitHandle_WaitOne_TimeoutInfinite_ShouldBlockUntilCountdownEventIsSet()
226226
{
227227
var sleep = TimeSpan.FromMilliseconds(100);
228-
var timeout = Session.InfiniteTimeSpan;
228+
var timeout = Timeout.InfiniteTimeSpan;
229229

230230
var countdownEvent = CreateCountdownEvent(1);
231231
var signalCount = 0;
@@ -341,7 +341,7 @@ public void WaitHandle_WaitOne_ShouldReturnFalseWhenTimeoutExpiresBeforeCountdow
341341
Assert.IsFalse(countdownEvent.IsSet);
342342
Assert.IsFalse(countdownEvent.WaitHandle.WaitOne(0));
343343

344-
_ = countdownEvent.Wait(Session.InfiniteTimeSpan);
344+
_ = countdownEvent.Wait(Timeout.InfiniteTimeSpan);
345345
countdownEvent.Dispose();
346346
}
347347

0 commit comments

Comments
 (0)