Skip to content

Commit 504d1dc

Browse files
committed
Prefer Socket.Poll(...) over Socket.Select(...).
Fixes #355.
1 parent cdbdefe commit 504d1dc

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

src/Renci.SshNet/Session.cs

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,16 +1784,16 @@ private int SocketRead(byte[] buffer, int offset, int length)
17841784
/// </para>
17851785
/// </remarks>
17861786
#else
1787-
/// <summary>
1788-
/// Gets a value indicating whether the socket is connected.
1789-
/// </summary>
1790-
/// <returns>
1791-
/// <c>true</c> if the socket is connected; otherwise, <c>false</c>.
1792-
/// </returns>
1793-
/// <remarks>
1794-
/// We verify whether <see cref="Socket.Connected"/> is <c>true</c>. However, this only returns the state
1795-
/// of the socket as of the last I/O operation.
1796-
/// </remarks>
1787+
/// <summary>
1788+
/// Gets a value indicating whether the socket is connected.
1789+
/// </summary>
1790+
/// <returns>
1791+
/// <c>true</c> if the socket is connected; otherwise, <c>false</c>.
1792+
/// </returns>
1793+
/// <remarks>
1794+
/// We verify whether <see cref="Socket.Connected"/> is <c>true</c>. However, this only returns the state
1795+
/// of the socket as of the last I/O operation.
1796+
/// </remarks>
17971797
#endif
17981798
private bool IsSocketConnected()
17991799
{
@@ -1920,16 +1920,23 @@ private void SocketDisconnectAndDispose()
19201920
/// </summary>
19211921
private void MessageListener()
19221922
{
1923-
#if FEATURE_SOCKET_SELECT
1924-
var readSockets = new List<Socket> { _socket };
1925-
#endif // FEATURE_SOCKET_SELECT
1926-
19271923
try
19281924
{
19291925
// remain in message loop until socket is shut down or until we're disconnecting
19301926
while (_socket.IsConnected())
19311927
{
1932-
#if FEATURE_SOCKET_SELECT
1928+
#if FEATURE_SOCKET_POLL
1929+
// Block until either data is available or the socket is closed
1930+
var connectionClosedOrDataAvailable = _socket.Poll(-1, SelectMode.SelectRead);
1931+
if (connectionClosedOrDataAvailable && _socket.Available == 0)
1932+
{
1933+
// connection with SSH server was closed or socket was disposed;
1934+
// break out of the message loop
1935+
break;
1936+
}
1937+
#elif FEATURE_SOCKET_SELECT
1938+
var readSockets = new List<Socket> { _socket };
1939+
19331940
// if the socket is already disposed when Select is invoked, then a SocketException
19341941
// stating "An operation was attempted on something that is not a socket" is thrown;
19351942
// we attempt to avoid this exception by having an IsConnected() that can break the
@@ -1963,18 +1970,9 @@ private void MessageListener()
19631970
// break out of the message loop
19641971
break;
19651972
}
1966-
#elif FEATURE_SOCKET_POLL
1967-
// when Socket.Select(IList, IList, IList, Int32) is not available or is buggy, we use
1968-
// Socket.Poll(Int, SelectMode) to block until either data is available or the socket
1969-
// is closed
1970-
_socket.Poll(-1, SelectMode.SelectRead);
1971-
1972-
if (!_socket.IsConnected())
1973-
{
1974-
// connection with SSH server was closed or socket was disposed;
1975-
// break out of the message loop
1976-
break;
1977-
}
1973+
#else
1974+
#error Blocking wait on either socket data to become available or connection to be
1975+
#error closed is not implemented.
19781976
#endif // FEATURE_SOCKET_SELECT
19791977

19801978
var message = ReceiveMessage();
@@ -2459,7 +2457,7 @@ private static SshConnectionException CreateConnectionAbortedByServerException()
24592457
DisconnectReason.ConnectionLost);
24602458
}
24612459

2462-
#region IDisposable implementation
2460+
#region IDisposable implementation
24632461

24642462
private bool _disposed;
24652463

@@ -2550,9 +2548,9 @@ protected virtual void Dispose(bool disposing)
25502548
Dispose(false);
25512549
}
25522550

2553-
#endregion IDisposable implementation
2551+
#endregion IDisposable implementation
25542552

2555-
#region ISession implementation
2553+
#region ISession implementation
25562554

25572555
/// <summary>
25582556
/// Gets or sets the connection info.
@@ -2638,7 +2636,7 @@ bool ISession.TrySendMessage(Message message)
26382636
return TrySendMessage(message);
26392637
}
26402638

2641-
#endregion ISession implementation
2639+
#endregion ISession implementation
26422640
}
26432641

26442642
/// <summary>

0 commit comments

Comments
 (0)