@@ -274,7 +274,7 @@ private static string RawQuery(string query, EndPoint server, IQueryOptions opti
274
274
try
275
275
{
276
276
// Async connect
277
- var tcpClientTask = options . TcpConnector . ConnectAsync ( server . Host , server . Port ) ;
277
+ var tcpClientTask = ConnectAsync ( server , options ) ;
278
278
tcpClientTask . ConfigureAwait ( false ) ;
279
279
280
280
// Wait at most timeout
@@ -318,7 +318,7 @@ private static string RawQuery(string query, EndPoint server, IQueryOptions opti
318
318
{
319
319
cbRead = s . Read ( readBuff , 0 , readBuff . Length ) ;
320
320
res . Append ( options . Encoding . GetString ( readBuff , 0 , cbRead ) ) ;
321
- if ( cbRead > 0 )
321
+ if ( cbRead > 0 )
322
322
Thread . Sleep ( 100 ) ;
323
323
} while ( cbRead > 0 ) ;
324
324
@@ -393,8 +393,7 @@ private static async Task<string> RawQueryAsync(string query, EndPoint server, I
393
393
// Async connect
394
394
try
395
395
{
396
- tcpClient = await options . TcpConnector . ConnectAsync ( server . Host , server . Port , token )
397
- . ConfigureAwait ( false ) ;
396
+ tcpClient = await ConnectAsync ( server , options , token ) . ConfigureAwait ( false ) ;
398
397
}
399
398
catch ( SocketException )
400
399
{
@@ -426,7 +425,7 @@ private static async Task<string> RawQueryAsync(string query, EndPoint server, I
426
425
{
427
426
cbRead = await s . ReadAsync ( readBuff , 0 , buffSize , token ) . ConfigureAwait ( false ) ;
428
427
res . Append ( options . Encoding . GetString ( readBuff , 0 , cbRead ) ) ;
429
- if ( cbRead > 0 )
428
+ if ( cbRead > 0 )
430
429
await Task . Delay ( 100 , token ) . ConfigureAwait ( false ) ;
431
430
} while ( cbRead > 0 ) ;
432
431
@@ -448,5 +447,27 @@ private static async Task<string> RawQueryAsync(string query, EndPoint server, I
448
447
tcpClient . Close ( ) ;
449
448
}
450
449
}
450
+
451
+ private static readonly Func < TcpConnectionArgs , Task < TcpClient > > DefaultConnectAsync = async args =>
452
+ {
453
+ if ( string . IsNullOrWhiteSpace ( args . Host ) ) throw new ArgumentException ( "Value cannot be null or whitespace." , nameof ( args . Host ) ) ;
454
+ if ( args . Port <= 0 || args . Port > ushort . MaxValue ) throw new ArgumentOutOfRangeException ( nameof ( args . Port ) ) ;
455
+
456
+ var tcpClient = new TcpClient ( ) ;
457
+ await tcpClient . ConnectAsync ( args . Host , args . Port ) . ConfigureAwait ( false ) ;
458
+ return tcpClient ;
459
+ } ;
460
+
461
+ private static async Task < TcpClient > ConnectAsync ( EndPoint server , IQueryOptions options , CancellationToken cancellationToken = default )
462
+ {
463
+ var tcpConnectionArgs = new TcpConnectionArgs
464
+ {
465
+ Host = server . Host ,
466
+ Port = server . Port ,
467
+ CancellationToken = CancellationToken . None
468
+ } ;
469
+ var connectAsync = options . ConnectAsync != null ? options . ConnectAsync : DefaultConnectAsync ;
470
+ return await connectAsync . Invoke ( tcpConnectionArgs ) . ConfigureAwait ( false ) ;
471
+ }
451
472
}
452
473
}
0 commit comments