@@ -38,6 +38,7 @@ internal sealed class GdsConnection
38
38
private string _userID ;
39
39
private string _dataSource ;
40
40
private int _portNumber ;
41
+ private int _timeout ;
41
42
private int _packetSize ;
42
43
private Charset _charset ;
43
44
private bool _compression ;
@@ -55,22 +56,24 @@ internal sealed class GdsConnection
55
56
public bool ConnectionBroken => _firebirdNetworkHandlingWrapper ? . IOFailed ?? false ;
56
57
57
58
internal IPAddress IPAddress { get ; private set ; }
59
+ internal int Timeout => _timeout ;
58
60
internal XdrReaderWriter Xdr { get ; private set ; }
59
61
60
62
#endregion
61
63
62
64
#region Constructors
63
65
64
- public GdsConnection ( string dataSource , int port )
65
- : this ( null , null , dataSource , port , 8192 , Charset . DefaultCharset , false , WireCryptOption . Enabled )
66
+ public GdsConnection ( string dataSource , int port , int timeout )
67
+ : this ( null , null , dataSource , port , timeout , 8192 , Charset . DefaultCharset , false , WireCryptOption . Enabled )
66
68
{ }
67
69
68
- public GdsConnection ( string userID , string password , string dataSource , int portNumber , int packetSize , Charset charset , bool compression , WireCryptOption wireCrypt )
70
+ public GdsConnection ( string userID , string password , string dataSource , int portNumber , int timeout , int packetSize , Charset charset , bool compression , WireCryptOption wireCrypt )
69
71
{
70
72
_userID = userID ;
71
73
Password = password ;
72
74
_dataSource = dataSource ;
73
75
_portNumber = portNumber ;
76
+ _timeout = timeout ;
74
77
_packetSize = packetSize ;
75
78
_charset = charset ;
76
79
_compression = compression ;
@@ -93,12 +96,26 @@ public async Task Connect(AsyncWrappingCommonArgs async)
93
96
socket . SetSocketOption ( SocketOptionLevel . Socket , SocketOptionName . SendBuffer , _packetSize ) ;
94
97
socket . SetSocketOption ( SocketOptionLevel . Tcp , SocketOptionName . NoDelay , 1 ) ;
95
98
socket . SetSocketOption ( SocketOptionLevel . Socket , SocketOptionName . KeepAlive , 1 ) ;
99
+
100
+ if ( async . IsAsync )
101
+ {
102
+ using ( var timeoutCts = new CancellationTokenSource ( TimeSpan . FromSeconds ( _timeout ) ) )
103
+ {
104
+ using ( var combinedCts = CancellationTokenSource . CreateLinkedTokenSource ( timeoutCts . Token , async. CancellationToken ) )
105
+ {
106
+ await ConnectAsyncHelper ( socket ) ( endPoint , combinedCts . Token ) . ConfigureAwait ( false ) ;
107
+ }
108
+ }
96
109
#if NET48 || NETSTANDARD2_0 || NETSTANDARD2_1
97
- static Func < IPEndPoint , CancellationToken , Task > ConnectHelper ( Socket socket ) => ( e , ct ) => Task . Factory . FromAsync ( socket . BeginConnect , socket . EndConnect , e , null ) ;
110
+ static Func< IPEndPoint, CancellationToken, Task> ConnectAsyncHelper ( Socket socket) => ( e , ct ) => Task . Factory . FromAsync ( socket . BeginConnect , socket . EndConnect , e , null ) ;
98
111
#else
99
- static Func < IPEndPoint , CancellationToken , Task > ConnectHelper ( Socket socket ) => ( e , ct ) => SocketTaskExtensions . ConnectAsync ( socket , e , ct ) . AsTask ( ) ;
112
+ static Func < IPEndPoint , CancellationToken , Task > ConnectAsyncHelper ( Socket socket ) => ( e , ct ) => SocketTaskExtensions . ConnectAsync ( socket , e , ct ) . AsTask ( ) ;
100
113
#endif
101
- await async . AsyncSyncCall ( ConnectHelper ( socket) , socket . Connect, endPoint ) . ConfigureAwait( false ) ;
114
+ }
115
+ else
116
+ {
117
+ socket . Connect ( endPoint ) ;
118
+ }
102
119
103
120
_networkStream = new NetworkStream ( socket , true ) ;
104
121
_firebirdNetworkHandlingWrapper = new FirebirdNetworkHandlingWrapper ( new DataProviderStreamWrapper ( _networkStream ) ) ;
0 commit comments