Skip to content

Commit c335ce2

Browse files
authored
Remove calls to Socket.Poll and use SocketShutdown.Both (#1706)
The message loop currently sits in a call to Poll until the socket has data to read or it is closed. This is unnecessary - it can equally just sit in the call to Receive. The call to Poll in Session.IsConnected is also unnecessary - we can instead just call Socket.Connected. This only returns the connection state as of the last operation, but we are always performing operations in the message loop (or else we are not connected), so it should work equally well while being cheaper. Lastly, when shutting down the socket, shut down both sides rather than just the sending side (SocketShutdown.Both rather than SocketShutdown.Send) - at this point we do not care about reading anything else. This makes it (more) certain that we will break out of the Receive call in the message loop, as has been noted in #355 for whatever remaining issues still exist there.
1 parent 081d305 commit c335ce2

File tree

6 files changed

+113
-312
lines changed

6 files changed

+113
-312
lines changed

src/Renci.SshNet/Abstractions/SocketAbstraction.cs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,6 @@ namespace Renci.SshNet.Abstractions
1212
{
1313
internal static partial class SocketAbstraction
1414
{
15-
public static bool CanRead(Socket socket)
16-
{
17-
if (socket.Connected)
18-
{
19-
return socket.Poll(-1, SelectMode.SelectRead) && socket.Available > 0;
20-
}
21-
22-
return false;
23-
}
24-
25-
/// <summary>
26-
/// Returns a value indicating whether the specified <see cref="Socket"/> can be used
27-
/// to send data.
28-
/// </summary>
29-
/// <param name="socket">The <see cref="Socket"/> to check.</param>
30-
/// <returns>
31-
/// <see langword="true"/> if <paramref name="socket"/> can be written to; otherwise, <see langword="false"/>.
32-
/// </returns>
33-
public static bool CanWrite(Socket socket)
34-
{
35-
if (socket != null && socket.Connected)
36-
{
37-
return socket.Poll(-1, SelectMode.SelectWrite);
38-
}
39-
40-
return false;
41-
}
42-
4315
public static Socket Connect(IPEndPoint remoteEndpoint, TimeSpan connectTimeout)
4416
{
4517
var socket = new Socket(remoteEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp) { NoDelay = true };

src/Renci.SshNet/Common/Extensions.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using System.Runtime.CompilerServices;
1111
using System.Threading;
1212

13-
using Renci.SshNet.Abstractions;
1413
using Renci.SshNet.Messages;
1514

1615
namespace Renci.SshNet.Common
@@ -319,16 +318,6 @@ public static byte[] Concat(this byte[] first, byte[] second)
319318
return concat;
320319
}
321320

322-
internal static bool CanRead(this Socket socket)
323-
{
324-
return SocketAbstraction.CanRead(socket);
325-
}
326-
327-
internal static bool CanWrite(this Socket socket)
328-
{
329-
return SocketAbstraction.CanWrite(socket);
330-
}
331-
332321
internal static bool IsConnected(this Socket socket)
333322
{
334323
if (socket is null)

0 commit comments

Comments
 (0)