Skip to content

Commit 6f6f652

Browse files
Extract connection methods, generalise for different address families
1 parent bee6c27 commit 6f6f652

File tree

1 file changed

+44
-20
lines changed

1 file changed

+44
-20
lines changed

projects/client/RabbitMQ.Client/src/client/impl/SocketFrameHandler.cs

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,29 +84,15 @@ public SocketFrameHandler(AmqpTcpEndpoint endpoint,
8484
int connectionTimeout, int readTimeout, int writeTimeout)
8585
{
8686
Endpoint = endpoint;
87-
m_socket = null;
87+
8888
if (Socket.OSSupportsIPv6 && endpoint.AddressFamily != AddressFamily.InterNetwork)
8989
{
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);
10591
}
92+
10693
if (m_socket == null && endpoint.AddressFamily != AddressFamily.InterNetworkV6)
10794
{
108-
m_socket = socketFactory(AddressFamily.InterNetwork);
109-
Connect(m_socket, endpoint, connectionTimeout);
95+
m_socket = ConnectUsingIPv4(endpoint, socketFactory, connectionTimeout);
11096
}
11197

11298
Stream netstream = m_socket.GetStream();
@@ -273,14 +259,52 @@ public void Flush()
273259
}
274260
}
275261

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)
277300
{
278301
try
279302
{
280303
socket.ConnectAsync(endpoint.HostName, endpoint.Port)
281304
.TimeoutAfter(timeout)
282305
.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()
284308
.GetResult();
285309
}
286310
catch (ArgumentException e)

0 commit comments

Comments
 (0)