@@ -84,29 +84,15 @@ public SocketFrameHandler(AmqpTcpEndpoint endpoint,
84
84
int connectionTimeout , int readTimeout , int writeTimeout )
85
85
{
86
86
Endpoint = endpoint ;
87
- m_socket = null ;
87
+
88
88
if ( Socket . OSSupportsIPv6 && endpoint . AddressFamily != AddressFamily . InterNetwork )
89
89
{
90
- try
91
- {
92
- m_socket = socketFactory ( AddressFamily . InterNetworkV6 ) ;
93
- Connect ( m_socket , endpoint , connectionTimeout ) ;
94
- }
95
- catch ( ConnectFailureException ) // could not connect using IPv6
96
- {
97
- m_socket = null ;
98
- }
99
- // Mono might raise a SocketException when using IPv4 addresses on
100
- // an OS that supports IPv6
101
- catch ( SocketException )
102
- {
103
- m_socket = null ;
104
- }
90
+ m_socket = ConnectUsingIPv6 ( endpoint , socketFactory , connectionTimeout ) ;
105
91
}
92
+
106
93
if ( m_socket == null && endpoint . AddressFamily != AddressFamily . InterNetworkV6 )
107
94
{
108
- m_socket = socketFactory ( AddressFamily . InterNetwork ) ;
109
- Connect ( m_socket , endpoint , connectionTimeout ) ;
95
+ m_socket = ConnectUsingIPv4 ( endpoint , socketFactory , connectionTimeout ) ;
110
96
}
111
97
112
98
Stream netstream = m_socket . GetStream ( ) ;
@@ -273,14 +259,52 @@ public void Flush()
273
259
}
274
260
}
275
261
276
- private void Connect ( ITcpClient socket , AmqpTcpEndpoint endpoint , int timeout )
262
+ private ITcpClient ConnectUsingIPv6 ( AmqpTcpEndpoint endpoint ,
263
+ Func < AddressFamily , ITcpClient > socketFactory ,
264
+ int timeout )
265
+ {
266
+ return ConnectUsingAddressFamily ( endpoint , socketFactory , timeout , AddressFamily . InterNetworkV6 ) ;
267
+ }
268
+
269
+ private ITcpClient ConnectUsingIPv4 ( AmqpTcpEndpoint endpoint ,
270
+ Func < AddressFamily , ITcpClient > socketFactory ,
271
+ int timeout )
272
+ {
273
+ return ConnectUsingAddressFamily ( endpoint , socketFactory , timeout , AddressFamily . InterNetwork ) ;
274
+ }
275
+
276
+ private ITcpClient ConnectUsingAddressFamily ( AmqpTcpEndpoint endpoint ,
277
+ Func < AddressFamily , ITcpClient > socketFactory ,
278
+ int timeout , AddressFamily family )
279
+ {
280
+ ITcpClient socket ;
281
+ try
282
+ {
283
+ socket = socketFactory ( family ) ;
284
+ ConnectOrFail ( socket , endpoint , timeout ) ;
285
+ return socket ;
286
+ }
287
+ catch ( ConnectFailureException ) // could not connect using IPv6
288
+ {
289
+ return null ;
290
+ }
291
+ // Mono might raise a SocketException when using IPv4 addresses on
292
+ // an OS that supports IPv6
293
+ catch ( SocketException )
294
+ {
295
+ return null ;
296
+ }
297
+ }
298
+
299
+ private void ConnectOrFail ( ITcpClient socket , AmqpTcpEndpoint endpoint , int timeout )
277
300
{
278
301
try
279
302
{
280
303
socket . ConnectAsync ( endpoint . HostName , endpoint . Port )
281
304
. TimeoutAfter ( timeout )
282
305
. ConfigureAwait ( false )
283
- . GetAwaiter ( ) //this ensures exceptions aren't wrapped in an AggregateException
306
+ // this ensures exceptions aren't wrapped in an AggregateException
307
+ . GetAwaiter ( )
284
308
. GetResult ( ) ;
285
309
}
286
310
catch ( ArgumentException e )
0 commit comments