Skip to content

Commit

Permalink
Merge bug23837 (ipv6 support)
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon MacMullen committed Mar 16, 2011
2 parents 4474f3b + b54fa2f commit faed96f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

using System;
using System.Collections;
using System.Text.RegularExpressions;
using RabbitMQ.Client.Impl;

namespace RabbitMQ.Client
Expand Down Expand Up @@ -243,9 +244,24 @@ public override int GetHashCode()
/// entire string is used as the hostname, and the port-number
/// is set to -1 (meaning the default number for the protocol
/// variant specified).
/// Hostnames provided as IPv6 must appear in square brackets ([]).
///</remarks>
public static AmqpTcpEndpoint Parse(IProtocol protocol, string address) {
int index = address.IndexOf(':');
Match match = Regex.Match(address, @"^\s*\[([%:0-9A-Fa-f]+)\](:(.*))?\s*$");
if (match.Success)
{
GroupCollection groups = match.Groups;
int portNum = -1;
if (groups[2].Success)
{
string portStr = groups[3].Value;
portNum = (portStr.Length == 0) ? -1 : int.Parse(portStr);
}
return new AmqpTcpEndpoint(protocol,
match.Groups[1].Value,
portNum);
}
int index = address.LastIndexOf(':');
if (index == -1) {
return new AmqpTcpEndpoint(protocol, address, -1);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,24 @@ public class SocketFrameHandler_0_9 : IFrameHandler
public SocketFrameHandler_0_9(AmqpTcpEndpoint endpoint)
{
m_endpoint = endpoint;
m_socket = new TcpClient();
m_socket.Connect(endpoint.HostName, endpoint.Port);
m_socket = null;
if (Socket.OSSupportsIPv6)
{
try
{
m_socket = new TcpClient(AddressFamily.InterNetworkV6);
m_socket.Connect(endpoint.HostName, endpoint.Port);
}
catch(SocketException)
{
m_socket = null;
}
}
if (m_socket == null)
{
m_socket = new TcpClient(AddressFamily.InterNetwork);
m_socket.Connect(endpoint.HostName, endpoint.Port);
}
// disable Nagle's algorithm, for more consistently low latency
m_socket.NoDelay = true;

Expand All @@ -88,12 +104,12 @@ public AmqpTcpEndpoint Endpoint

public int Timeout
{
set
{
if (m_socket.Connected)
{
m_socket.ReceiveTimeout = value;
}
set
{
if (m_socket.Connected)
{
m_socket.ReceiveTimeout = value;
}
}
}

Expand Down
18 changes: 18 additions & 0 deletions projects/client/Unit/src/unit/TestAmqpTcpEndpointParsing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,23 @@ public void TestMultipleTwoMultipleCommas()
Assert.AreEqual("other", es[1].HostName);
Assert.AreEqual(2345, es[1].Port);
}

[Test]
public void TestIpv6WithPort()
{
AmqpTcpEndpoint e = AmqpTcpEndpoint.Parse(Protocols.DefaultProtocol, "[::1]:1234");
Assert.AreEqual(Protocols.DefaultProtocol, e.Protocol);
Assert.AreEqual("::1", e.HostName);
Assert.AreEqual(1234, e.Port);
}

[Test]
public void TestIpv6WithoutPort()
{
AmqpTcpEndpoint e = AmqpTcpEndpoint.Parse(Protocols.DefaultProtocol, "[::1]");
Assert.AreEqual(Protocols.DefaultProtocol, e.Protocol);
Assert.AreEqual("::1", e.HostName);
Assert.AreEqual(Protocols.DefaultProtocol.DefaultPort, e.Port);
}
}
}

0 comments on commit faed96f

Please sign in to comment.