@@ -49,8 +49,18 @@ public static bool CanWrite(Socket socket)
49
49
50
50
public static Socket Connect ( IPEndPoint remoteEndpoint , TimeSpan connectTimeout )
51
51
{
52
- var socket = new Socket ( remoteEndpoint . AddressFamily , SocketType . Stream , ProtocolType . Tcp ) { NoDelay = true } ;
52
+ var socket = new Socket ( remoteEndpoint . AddressFamily , SocketType . Stream , ProtocolType . Tcp ) { NoDelay = true } ;
53
+ ConnectCore ( socket , remoteEndpoint , connectTimeout , true ) ;
54
+ return socket ;
55
+ }
56
+
57
+ public static void Connect ( Socket socket , IPEndPoint remoteEndpoint , TimeSpan connectTimeout )
58
+ {
59
+ ConnectCore ( socket , remoteEndpoint , connectTimeout , false ) ;
60
+ }
53
61
62
+ private static void ConnectCore ( Socket socket , IPEndPoint remoteEndpoint , TimeSpan connectTimeout , bool ownsSocket )
63
+ {
54
64
#if FEATURE_SOCKET_EAP
55
65
var connectCompleted = new ManualResetEvent ( false ) ;
56
66
var args = new SocketAsyncEventArgs
@@ -66,8 +76,11 @@ public static Socket Connect(IPEndPoint remoteEndpoint, TimeSpan connectTimeout)
66
76
{
67
77
// avoid ObjectDisposedException in ConnectCompleted
68
78
args . Completed -= ConnectCompleted ;
69
- // dispose Socket
70
- socket . Dispose ( ) ;
79
+ if ( ownsSocket )
80
+ {
81
+ // dispose Socket
82
+ socket . Dispose ( ) ;
83
+ }
71
84
// dispose ManualResetEvent
72
85
connectCompleted . Dispose ( ) ;
73
86
// dispose SocketAsyncEventArgs
@@ -86,8 +99,12 @@ public static Socket Connect(IPEndPoint remoteEndpoint, TimeSpan connectTimeout)
86
99
{
87
100
var socketError = ( int ) args . SocketError ;
88
101
89
- // dispose Socket
90
- socket . Dispose ( ) ;
102
+ if ( ownsSocket )
103
+ {
104
+ // dispose Socket
105
+ socket . Dispose ( ) ;
106
+ }
107
+
91
108
// dispose SocketAsyncEventArgs
92
109
args . Dispose ( ) ;
93
110
@@ -96,20 +113,16 @@ public static Socket Connect(IPEndPoint remoteEndpoint, TimeSpan connectTimeout)
96
113
97
114
// dispose SocketAsyncEventArgs
98
115
args . Dispose ( ) ;
99
-
100
- return socket ;
101
116
#elif FEATURE_SOCKET_APM
102
117
var connectResult = socket . BeginConnect ( remoteEndpoint , null , null ) ;
103
118
if ( ! connectResult . AsyncWaitHandle . WaitOne ( connectTimeout , false ) )
104
119
throw new SshOperationTimeoutException ( string . Format ( CultureInfo . InvariantCulture ,
105
120
"Connection failed to establish within {0:F0} milliseconds." , connectTimeout . TotalMilliseconds ) ) ;
106
121
socket . EndConnect ( connectResult ) ;
107
- return socket ;
108
122
#elif FEATURE_SOCKET_TAP
109
123
if ( ! socket . ConnectAsync ( remoteEndpoint ) . Wait ( connectTimeout ) )
110
124
throw new SshOperationTimeoutException ( string . Format ( CultureInfo . InvariantCulture ,
111
125
"Connection failed to establish within {0:F0} milliseconds." , connectTimeout . TotalMilliseconds ) ) ;
112
- return socket ;
113
126
#else
114
127
#error Connecting to a remote endpoint is not implemented.
115
128
#endif
@@ -311,24 +324,28 @@ public static byte[] Read(Socket socket, int size, TimeSpan timeout)
311
324
/// <param name="buffer">An array of type <see cref="byte"/> that is the storage location for the received data. </param>
312
325
/// <param name="offset">The position in <paramref name="buffer"/> parameter to store the received data.</param>
313
326
/// <param name="size">The number of bytes to receive.</param>
314
- /// <param name="timeout">Specifies the amount of time after which the call will time out .</param>
327
+ /// <param name="readTimeout">The maximum time to wait until <paramref name="size"/> bytes have been received .</param>
315
328
/// <returns>
316
329
/// The number of bytes received.
317
330
/// </returns>
318
331
/// <remarks>
332
+ /// <para>
319
333
/// If no data is available for reading, the <see cref="Read(Socket, byte[], int, int, TimeSpan)"/> method will
320
334
/// block until data is available or the time-out value is exceeded. If the time-out value is exceeded, the
321
335
/// <see cref="Read(Socket, byte[], int, int, TimeSpan)"/> call will throw a <see cref="SshOperationTimeoutException"/>.
322
- /// If you are in non-blocking mode, and there is no data available in the in the protocol stack buffer, the
336
+ /// </para>
337
+ /// <para>
338
+ /// If you are in non-blocking mode, and there is no data available in the in the protocol stack buffer, the
323
339
/// <see cref="Read(Socket, byte[], int, int, TimeSpan)"/> method will complete immediately and throw a <see cref="SocketException"/>.
340
+ /// </para>
324
341
/// </remarks>
325
- public static int Read ( Socket socket , byte [ ] buffer , int offset , int size , TimeSpan timeout )
342
+ public static int Read ( Socket socket , byte [ ] buffer , int offset , int size , TimeSpan readTimeout )
326
343
{
327
344
#if FEATURE_SOCKET_SYNC
328
345
var totalBytesRead = 0 ;
329
346
var totalBytesToRead = size ;
330
347
331
- socket . ReceiveTimeout = ( int ) timeout . TotalMilliseconds ;
348
+ socket . ReceiveTimeout = ( int ) readTimeout . TotalMilliseconds ;
332
349
333
350
do
334
351
{
@@ -350,7 +367,7 @@ public static int Read(Socket socket, byte[] buffer, int offset, int size, TimeS
350
367
351
368
if ( ex . SocketErrorCode == SocketError . TimedOut )
352
369
throw new SshOperationTimeoutException ( string . Format ( CultureInfo . InvariantCulture ,
353
- "Socket read operation has timed out after {0:F0} milliseconds." , timeout . TotalMilliseconds ) ) ;
370
+ "Socket read operation has timed out after {0:F0} milliseconds." , readTimeout . TotalMilliseconds ) ) ;
354
371
355
372
throw ;
356
373
}
0 commit comments