@@ -395,55 +395,9 @@ private async Task<TcpServerConnection> createServerConnection(string remoteHost
395395 tcpServerSocket . SetSocketOption ( SocketOptionLevel . Socket , SocketOptionName . ReuseAddress , true ) ;
396396 }
397397
398- Task connectTask ;
399- if ( socks )
400- {
401- var clientSocket = ( ProxySocket . ProxySocket ) tcpServerSocket ;
402-
403- IAsyncResult BeginConnect ( IPAddress address , int port , AsyncCallback requestCallback ,
404- object state )
405- {
406- return clientSocket . BeginConnect ( address , port , requestCallback , state ) ;
407- }
408-
409- void EndConnect ( IAsyncResult asyncResult )
410- {
411- var s = clientSocket ;
412- if ( s == null )
413- {
414- // Dispose nulls out the client socket field.
415- throw new ObjectDisposedException ( GetType ( ) . Name ) ;
416- }
417-
418- s . EndConnect ( asyncResult ) ;
419- }
420-
421- connectTask = Task . Factory . FromAsync ( BeginConnect , EndConnect , ipAddress , port , state : this ) ;
422- }
423- else
424- {
425- var clientSocket = tcpServerSocket ;
426-
427- IAsyncResult BeginConnect ( IPAddress address , int port , AsyncCallback requestCallback ,
428- object state )
429- {
430- return clientSocket . BeginConnect ( address , port , requestCallback , state ) ;
431- }
432-
433- void EndConnect ( IAsyncResult asyncResult )
434- {
435- var s = clientSocket ;
436- if ( s == null )
437- {
438- // Dispose nulls out the client socket field.
439- throw new ObjectDisposedException ( GetType ( ) . Name ) ;
440- }
441-
442- s . EndConnect ( asyncResult ) ;
443- }
444-
445- connectTask = Task . Factory . FromAsync ( BeginConnect , EndConnect , ipAddress , port , state : this ) ;
446- }
398+ var connectTask = socks
399+ ? ProxySocketConnectionTaskFactory . CreateTask ( ( ProxySocket . ProxySocket ) tcpServerSocket , ipAddress , port )
400+ : SocketConnectionTaskFactory . CreateTask ( tcpServerSocket , ipAddress , port ) ;
447401
448402 await Task . WhenAny ( connectTask , Task . Delay ( proxyServer . ConnectTimeOutSeconds * 1000 , cancellationToken ) ) ;
449403 if ( ! connectTask . IsCompleted || ! tcpServerSocket . Connected )
@@ -767,5 +721,43 @@ public void Dispose()
767721 }
768722 }
769723 }
724+
725+ static class SocketConnectionTaskFactory
726+ {
727+ static IAsyncResult beginConnect ( IPAddress address , int port , AsyncCallback requestCallback ,
728+ object state )
729+ {
730+ return ( ( Socket ) state ) . BeginConnect ( address , port , requestCallback , state ) ;
731+ }
732+
733+ static void endConnect ( IAsyncResult asyncResult )
734+ {
735+ ( ( Socket ) asyncResult . AsyncState ) . EndConnect ( asyncResult ) ;
736+ }
737+
738+ public static Task CreateTask ( Socket socket , IPAddress ipAddress , int port )
739+ {
740+ return Task . Factory . FromAsync ( beginConnect , endConnect , ipAddress , port , state : socket ) ;
741+ }
742+ }
743+
744+ static class ProxySocketConnectionTaskFactory
745+ {
746+ static IAsyncResult beginConnect ( IPAddress address , int port , AsyncCallback requestCallback ,
747+ object state )
748+ {
749+ return ( ( ProxySocket . ProxySocket ) state ) . BeginConnect ( address , port , requestCallback , state ) ;
750+ }
751+
752+ static void endConnect ( IAsyncResult asyncResult )
753+ {
754+ ( ( ProxySocket . ProxySocket ) asyncResult . AsyncState ) . EndConnect ( asyncResult ) ;
755+ }
756+
757+ public static Task CreateTask ( ProxySocket . ProxySocket socket , IPAddress ipAddress , int port )
758+ {
759+ return Task . Factory . FromAsync ( beginConnect , endConnect , ipAddress , port , state : socket ) ;
760+ }
761+ }
770762 }
771763}
0 commit comments