@@ -84,29 +84,20 @@ public SocketFrameHandler(AmqpTcpEndpoint endpoint,
84
84
int connectionTimeout , int readTimeout , int writeTimeout )
85
85
{
86
86
Endpoint = endpoint ;
87
- m_socket = null ;
88
- if ( Socket . OSSupportsIPv6 && endpoint . AddressFamily != AddressFamily . InterNetwork )
87
+
88
+ if ( ShouldTryIPv6 ( endpoint ) )
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 )
90
+ try {
91
+ m_socket = ConnectUsingIPv6 ( endpoint , socketFactory , connectionTimeout ) ;
92
+ } catch ( ConnectFailureException )
102
93
{
103
94
m_socket = null ;
104
95
}
105
96
}
97
+
106
98
if ( m_socket == null && endpoint . AddressFamily != AddressFamily . InterNetworkV6 )
107
99
{
108
- m_socket = socketFactory ( AddressFamily . InterNetwork ) ;
109
- Connect ( m_socket , endpoint , connectionTimeout ) ;
100
+ m_socket = ConnectUsingIPv4 ( endpoint , socketFactory , connectionTimeout ) ;
110
101
}
111
102
112
103
Stream netstream = m_socket . GetStream ( ) ;
@@ -164,12 +155,10 @@ public int ReadTimeout
164
155
m_socket . ReceiveTimeout = value ;
165
156
}
166
157
}
167
- #pragma warning disable 0168
168
- catch ( SocketException _ )
158
+ catch ( SocketException )
169
159
{
170
160
// means that the socket is already closed
171
161
}
172
- #pragma warning restore 0168
173
162
}
174
163
}
175
164
@@ -190,13 +179,6 @@ public void Close()
190
179
{
191
180
try
192
181
{
193
- try
194
- {
195
-
196
- } catch ( ArgumentException )
197
- {
198
- // ignore, we are closing anyway
199
- } ;
200
182
m_socket . Close ( ) ;
201
183
}
202
184
catch ( Exception )
@@ -273,14 +255,48 @@ public void Flush()
273
255
}
274
256
}
275
257
276
- private void Connect ( ITcpClient socket , AmqpTcpEndpoint endpoint , int timeout )
258
+ private bool ShouldTryIPv6 ( AmqpTcpEndpoint endpoint )
259
+ {
260
+ return ( Socket . OSSupportsIPv6 && endpoint . AddressFamily != AddressFamily . InterNetwork ) ;
261
+ }
262
+
263
+ private ITcpClient ConnectUsingIPv6 ( AmqpTcpEndpoint endpoint ,
264
+ Func < AddressFamily , ITcpClient > socketFactory ,
265
+ int timeout )
266
+ {
267
+ return ConnectUsingAddressFamily ( endpoint , socketFactory , timeout , AddressFamily . InterNetworkV6 ) ;
268
+ }
269
+
270
+ private ITcpClient ConnectUsingIPv4 ( AmqpTcpEndpoint endpoint ,
271
+ Func < AddressFamily , ITcpClient > socketFactory ,
272
+ int timeout )
273
+ {
274
+ return ConnectUsingAddressFamily ( endpoint , socketFactory , timeout , AddressFamily . InterNetwork ) ;
275
+ }
276
+
277
+ private ITcpClient ConnectUsingAddressFamily ( AmqpTcpEndpoint endpoint ,
278
+ Func < AddressFamily , ITcpClient > socketFactory ,
279
+ int timeout , AddressFamily family )
280
+ {
281
+ ITcpClient socket = socketFactory ( family ) ;
282
+ try {
283
+ ConnectOrFail ( socket , endpoint , timeout ) ;
284
+ return socket ;
285
+ } catch ( ConnectFailureException e ) {
286
+ socket . Dispose ( ) ;
287
+ throw e ;
288
+ }
289
+ }
290
+
291
+ private void ConnectOrFail ( ITcpClient socket , AmqpTcpEndpoint endpoint , int timeout )
277
292
{
278
293
try
279
294
{
280
295
socket . ConnectAsync ( endpoint . HostName , endpoint . Port )
281
296
. TimeoutAfter ( timeout )
282
297
. ConfigureAwait ( false )
283
- . GetAwaiter ( ) //this ensures exceptions aren't wrapped in an AggregateException
298
+ // this ensures exceptions aren't wrapped in an AggregateException
299
+ . GetAwaiter ( )
284
300
. GetResult ( ) ;
285
301
}
286
302
catch ( ArgumentException e )
0 commit comments