activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jgo...@apache.org
Subject svn commit: r760994 - /activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transport/Tcp/TcpTransportFactory.cs
Date Wed, 01 Apr 2009 18:18:43 GMT
Author: jgomes
Date: Wed Apr  1 18:18:42 2009
New Revision: 760994

URL: http://svn.apache.org/viewvc?rev=760994&view=rev
Log:
Prefer IPv6 connections, and then fallback to IPv4 and other protocol address types.
Fixes [AMQNET-155]. (See https://issues.apache.org/activemq/browse/AMQNET-155)

Modified:
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transport/Tcp/TcpTransportFactory.cs

Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transport/Tcp/TcpTransportFactory.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transport/Tcp/TcpTransportFactory.cs?rev=760994&r1=760993&r2=760994&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transport/Tcp/TcpTransportFactory.cs
(original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transport/Tcp/TcpTransportFactory.cs
Wed Apr  1 18:18:42 2009
@@ -177,21 +177,24 @@
 
 		private Socket ConnectSocket(IPAddress address, int port)
 		{
-			try
+			if(null != address)
 			{
-				Socket socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
-
-				if(null != socket)
+				try
 				{
-					socket.Connect(new IPEndPoint(address, port));
-					if(socket.Connected)
+					Socket socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
+
+					if(null != socket)
 					{
-						return socket;
+						socket.Connect(new IPEndPoint(address, port));
+						if(socket.Connected)
+						{
+							return socket;
+						}
 					}
 				}
-			}
-			catch
-			{
+				catch
+				{
+				}
 			}
 
 			return null;
@@ -215,6 +218,22 @@
 #endif
 		}
 
+		private static IPAddress GetIPAddress(IPHostEntry hostEntry, AddressFamily addressFamily)
+		{
+			if(null != hostEntry)
+			{
+				foreach(IPAddress address in hostEntry.AddressList)
+				{
+					if(address.AddressFamily == addressFamily)
+					{
+						return address;
+					}
+				}
+			}
+
+			return null;
+		}
+
 		protected Socket Connect(string host, int port)
 		{
 			Socket socket = null;
@@ -227,15 +246,36 @@
 			else
 			{
 				// Looping through the AddressList allows different type of connections to be tried
-				// (IPv4, IPv6 and whatever else may be available).
+				// (IPv6, IPv4 and whatever else may be available).
 				IPHostEntry hostEntry = GetIPHostEntry(host);
 
-				foreach(IPAddress address in hostEntry.AddressList)
+				// Prefer IPv6 first.
+				ipaddress = GetIPAddress(hostEntry, AddressFamily.InterNetworkV6);
+				socket = ConnectSocket(ipaddress, port);
+				if(null == socket)
 				{
-					socket = ConnectSocket(address, port);
-					if(null != socket)
+					// Try IPv4 next.
+					ipaddress = GetIPAddress(hostEntry, AddressFamily.InterNetwork);
+					socket = ConnectSocket(ipaddress, port);
+					if(null == socket)
 					{
-						break;
+						// Try whatever else there is.
+						foreach(IPAddress address in hostEntry.AddressList)
+						{
+							if(AddressFamily.InterNetworkV6 == address.AddressFamily
+								|| AddressFamily.InterNetwork == address.AddressFamily)
+							{
+								// Already tried these protocols.
+								continue;
+							}
+
+							socket = ConnectSocket(address, port);
+							if(null != socket)
+							{
+								ipaddress = address;
+								break;
+							}
+						}
 					}
 				}
 			}
@@ -245,6 +285,7 @@
 				throw new SocketException();
 			}
 
+			Tracer.DebugFormat("Connected to {0}:{1} using {0} protocol.", host, port, ipaddress.AddressFamily.ToString());
 			return socket;
 		}
 



Mime
View raw message