File tree Expand file tree Collapse file tree 3 files changed +59
-9
lines changed
RabbitMQ.Client/src/client Expand file tree Collapse file tree 3 files changed +59
-9
lines changed Original file line number Diff line number Diff line change 4040
4141using System ;
4242using System . Collections ;
43+ using System . Text . RegularExpressions ;
4344using RabbitMQ . Client . Impl ;
4445
4546namespace RabbitMQ . Client
@@ -243,9 +244,24 @@ public override int GetHashCode()
243244 /// entire string is used as the hostname, and the port-number
244245 /// is set to -1 (meaning the default number for the protocol
245246 /// variant specified).
247+ /// Hostnames provided as IPv6 must appear in square brackets ([]).
246248 ///</remarks>
247249 public static AmqpTcpEndpoint Parse ( IProtocol protocol , string address ) {
248- int index = address . IndexOf ( ':' ) ;
250+ Match match = Regex . Match ( address , @"^\s*\[([%:0-9A-Fa-f]+)\](:(.*))?\s*$" ) ;
251+ if ( match . Success )
252+ {
253+ GroupCollection groups = match . Groups ;
254+ int portNum = - 1 ;
255+ if ( groups [ 2 ] . Success )
256+ {
257+ string portStr = groups [ 3 ] . Value ;
258+ portNum = ( portStr . Length == 0 ) ? - 1 : int . Parse ( portStr ) ;
259+ }
260+ return new AmqpTcpEndpoint ( protocol ,
261+ match . Groups [ 1 ] . Value ,
262+ portNum ) ;
263+ }
264+ int index = address . LastIndexOf ( ':' ) ;
249265 if ( index == - 1 ) {
250266 return new AmqpTcpEndpoint ( protocol , address , - 1 ) ;
251267 } else {
Original file line number Diff line number Diff line change @@ -60,8 +60,24 @@ public class SocketFrameHandler_0_9 : IFrameHandler
6060 public SocketFrameHandler_0_9 ( AmqpTcpEndpoint endpoint )
6161 {
6262 m_endpoint = endpoint ;
63- m_socket = new TcpClient ( ) ;
64- m_socket . Connect ( endpoint . HostName , endpoint . Port ) ;
63+ m_socket = null ;
64+ if ( Socket . OSSupportsIPv6 )
65+ {
66+ try
67+ {
68+ m_socket = new TcpClient ( AddressFamily . InterNetworkV6 ) ;
69+ m_socket . Connect ( endpoint . HostName , endpoint . Port ) ;
70+ }
71+ catch ( SocketException )
72+ {
73+ m_socket = null ;
74+ }
75+ }
76+ if ( m_socket == null )
77+ {
78+ m_socket = new TcpClient ( AddressFamily . InterNetwork ) ;
79+ m_socket . Connect ( endpoint . HostName , endpoint . Port ) ;
80+ }
6581 // disable Nagle's algorithm, for more consistently low latency
6682 m_socket . NoDelay = true ;
6783
@@ -88,12 +104,12 @@ public AmqpTcpEndpoint Endpoint
88104
89105 public int Timeout
90106 {
91- set
92- {
93- if ( m_socket . Connected )
94- {
95- m_socket . ReceiveTimeout = value ;
96- }
107+ set
108+ {
109+ if ( m_socket . Connected )
110+ {
111+ m_socket . ReceiveTimeout = value ;
112+ }
97113 }
98114 }
99115
Original file line number Diff line number Diff line change @@ -145,5 +145,23 @@ public void TestMultipleTwoMultipleCommas()
145145 Assert . AreEqual ( "other" , es [ 1 ] . HostName ) ;
146146 Assert . AreEqual ( 2345 , es [ 1 ] . Port ) ;
147147 }
148+
149+ [ Test ]
150+ public void TestIpv6WithPort ( )
151+ {
152+ AmqpTcpEndpoint e = AmqpTcpEndpoint . Parse ( Protocols . DefaultProtocol , "[::1]:1234" ) ;
153+ Assert . AreEqual ( Protocols . DefaultProtocol , e . Protocol ) ;
154+ Assert . AreEqual ( "::1" , e . HostName ) ;
155+ Assert . AreEqual ( 1234 , e . Port ) ;
156+ }
157+
158+ [ Test ]
159+ public void TestIpv6WithoutPort ( )
160+ {
161+ AmqpTcpEndpoint e = AmqpTcpEndpoint . Parse ( Protocols . DefaultProtocol , "[::1]" ) ;
162+ Assert . AreEqual ( Protocols . DefaultProtocol , e . Protocol ) ;
163+ Assert . AreEqual ( "::1" , e . HostName ) ;
164+ Assert . AreEqual ( Protocols . DefaultProtocol . DefaultPort , e . Port ) ;
165+ }
148166 }
149167}
You can’t perform that action at this time.
0 commit comments