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 40
40
41
41
using System ;
42
42
using System . Collections ;
43
+ using System . Text . RegularExpressions ;
43
44
using RabbitMQ . Client . Impl ;
44
45
45
46
namespace RabbitMQ . Client
@@ -243,9 +244,24 @@ public override int GetHashCode()
243
244
/// entire string is used as the hostname, and the port-number
244
245
/// is set to -1 (meaning the default number for the protocol
245
246
/// variant specified).
247
+ /// Hostnames provided as IPv6 must appear in square brackets ([]).
246
248
///</remarks>
247
249
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 ( ':' ) ;
249
265
if ( index == - 1 ) {
250
266
return new AmqpTcpEndpoint ( protocol , address , - 1 ) ;
251
267
} else {
Original file line number Diff line number Diff line change @@ -60,8 +60,24 @@ public class SocketFrameHandler_0_9 : IFrameHandler
60
60
public SocketFrameHandler_0_9 ( AmqpTcpEndpoint endpoint )
61
61
{
62
62
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
+ }
65
81
// disable Nagle's algorithm, for more consistently low latency
66
82
m_socket . NoDelay = true ;
67
83
@@ -88,12 +104,12 @@ public AmqpTcpEndpoint Endpoint
88
104
89
105
public int Timeout
90
106
{
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
+ }
97
113
}
98
114
}
99
115
Original file line number Diff line number Diff line change @@ -145,5 +145,23 @@ public void TestMultipleTwoMultipleCommas()
145
145
Assert . AreEqual ( "other" , es [ 1 ] . HostName ) ;
146
146
Assert . AreEqual ( 2345 , es [ 1 ] . Port ) ;
147
147
}
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
+ }
148
166
}
149
167
}
You can’t perform that action at this time.
0 commit comments