@@ -24,6 +24,8 @@ public class ConnectionSettingsBuilder
24
24
private uint _maxFrameSize = Consts . DefaultMaxFrameSize ;
25
25
private SaslMechanism _saslMechanism = Client . SaslMechanism . Anonymous ;
26
26
private IRecoveryConfiguration _recoveryConfiguration = new RecoveryConfiguration ( ) ;
27
+ private TlsSettings ? _tlsSettings = null ;
28
+ private Uri ? _uri ;
27
29
private List < Uri > ? _uris ;
28
30
29
31
public static ConnectionSettingsBuilder Create ( )
@@ -76,10 +78,10 @@ public ConnectionSettingsBuilder VirtualHost(string virtualHost)
76
78
public ConnectionSettingsBuilder MaxFrameSize ( uint maxFrameSize )
77
79
{
78
80
_maxFrameSize = maxFrameSize ;
79
- if ( _maxFrameSize != uint . MinValue && _maxFrameSize < 512 )
81
+ if ( _maxFrameSize != Consts . DefaultMaxFrameSize && _maxFrameSize < 512 )
80
82
{
81
83
throw new ArgumentOutOfRangeException ( nameof ( maxFrameSize ) ,
82
- "maxFrameSize must be greater or equal to 512" ) ;
84
+ "maxFrameSize must be 0 (no limit) or greater than or equal to 512" ) ;
83
85
}
84
86
return this ;
85
87
}
@@ -103,21 +105,63 @@ public ConnectionSettingsBuilder RecoveryConfiguration(IRecoveryConfiguration re
103
105
return this ;
104
106
}
105
107
108
+ public ConnectionSettingsBuilder TlsSettings ( TlsSettings tlsSettings )
109
+ {
110
+ _tlsSettings = tlsSettings ;
111
+ return this ;
112
+ }
113
+
114
+ public ConnectionSettingsBuilder Uri ( Uri uri )
115
+ {
116
+ _uri = uri ;
117
+ ValidateUris ( ) ;
118
+ return this ;
119
+ }
120
+
106
121
public ConnectionSettingsBuilder Uris ( IEnumerable < Uri > uris )
107
122
{
108
123
_uris = uris . ToList ( ) ;
124
+ ValidateUris ( ) ;
109
125
return this ;
110
126
}
111
127
112
128
public ConnectionSettings Build ( )
113
129
{
130
+ ValidateUris ( ) ;
114
131
// TODO this should do something similar to consolidate in the Java code
115
- var c = new ConnectionSettings ( _scheme , _host , _port , _user ,
116
- _password , _virtualHost ,
117
- _containerId , _saslMechanism ,
118
- _recoveryConfiguration ,
119
- _maxFrameSize ) ;
120
- return c ;
132
+ if ( _uri is not null )
133
+ {
134
+ return new ConnectionSettings ( _uri ,
135
+ _containerId , _saslMechanism ,
136
+ _recoveryConfiguration ,
137
+ _maxFrameSize ,
138
+ _tlsSettings ) ;
139
+ }
140
+ else if ( _uris is not null )
141
+ {
142
+ return new ConnectionSettings ( _uris ,
143
+ _containerId , _saslMechanism ,
144
+ _recoveryConfiguration ,
145
+ _maxFrameSize ,
146
+ _tlsSettings ) ;
147
+ }
148
+ else
149
+ {
150
+ return new ConnectionSettings ( _scheme , _host , _port , _user ,
151
+ _password , _virtualHost ,
152
+ _containerId , _saslMechanism ,
153
+ _recoveryConfiguration ,
154
+ _maxFrameSize ,
155
+ _tlsSettings ) ;
156
+ }
157
+ }
158
+
159
+ private void ValidateUris ( )
160
+ {
161
+ if ( _uri is not null && _uris is not null )
162
+ {
163
+ throw new ArgumentOutOfRangeException ( "uris" , "Do not set both Uri and Uris" ) ;
164
+ }
121
165
}
122
166
}
123
167
@@ -126,23 +170,22 @@ public ConnectionSettings Build()
126
170
// </summary>
127
171
public class ConnectionSettings : IEquatable < ConnectionSettings >
128
172
{
129
- private readonly Address _address ;
173
+ private readonly Address _address = new ( "amqp://localhost:5672" ) ;
130
174
private readonly List < Address > _addresses = new ( ) ;
131
175
private readonly string _virtualHost = Consts . DefaultVirtualHost ;
132
- private readonly string _containerId = "" ;
176
+ private readonly string _containerId = string . Empty ;
133
177
private readonly uint _maxFrameSize = Consts . DefaultMaxFrameSize ;
134
178
private readonly TlsSettings ? _tlsSettings ;
135
179
private readonly SaslMechanism _saslMechanism = SaslMechanism . Plain ;
136
180
private readonly IRecoveryConfiguration _recoveryConfiguration = new RecoveryConfiguration ( ) ;
137
181
138
- /*
139
- * TODO: support these:
140
- SaslMechanism saslMechanism,
141
- IRecoveryConfiguration recoveryConfiguration,
142
- uint maxFrameSize = Consts.DefaultMaxFrameSize ,
182
+ public ConnectionSettings ( Uri uri ,
183
+ string ? containerId = null ,
184
+ SaslMechanism ? saslMechanism = null ,
185
+ IRecoveryConfiguration ? recoveryConfiguration = null ,
186
+ uint ? maxFrameSize = null ,
143
187
TlsSettings ? tlsSettings = null )
144
- */
145
- public ConnectionSettings ( Uri uri )
188
+ : this ( containerId , saslMechanism , recoveryConfiguration , maxFrameSize , tlsSettings )
146
189
{
147
190
( string ? user , string ? password ) = ProcessUserInfo ( uri ) ;
148
191
@@ -162,14 +205,13 @@ public ConnectionSettings(Uri uri)
162
205
}
163
206
}
164
207
165
- /*
166
- * TODO: support these:
167
- SaslMechanism saslMechanism,
168
- IRecoveryConfiguration recoveryConfiguration,
169
- uint maxFrameSize = Consts.DefaultMaxFrameSize ,
208
+ public ConnectionSettings ( IEnumerable < Uri > uris ,
209
+ string ? containerId = null ,
210
+ SaslMechanism ? saslMechanism = null ,
211
+ IRecoveryConfiguration ? recoveryConfiguration = null ,
212
+ uint ? maxFrameSize = null ,
170
213
TlsSettings ? tlsSettings = null )
171
- */
172
- public ConnectionSettings ( IEnumerable < Uri > uris )
214
+ : this ( containerId , saslMechanism , recoveryConfiguration , maxFrameSize , tlsSettings )
173
215
{
174
216
string ? tmpVirtualHost = null ;
175
217
@@ -200,43 +242,75 @@ public ConnectionSettings(IEnumerable<Uri> uris)
200
242
}
201
243
202
244
_address = _addresses [ 0 ] ;
245
+
203
246
if ( tmpVirtualHost is not null )
204
247
{
205
248
_virtualHost = tmpVirtualHost ;
206
249
}
207
250
}
208
251
209
- public ConnectionSettings ( string scheme , string host , int port ,
210
- string ? user , string ? password ,
211
- string virtualHost , string containerId ,
212
- SaslMechanism saslMechanism ,
213
- IRecoveryConfiguration recoveryConfiguration ,
214
- uint maxFrameSize = Consts . DefaultMaxFrameSize ,
252
+ public ConnectionSettings ( string scheme ,
253
+ string host ,
254
+ int port ,
255
+ string ? user = null ,
256
+ string ? password = null ,
257
+ string ? virtualHost = null ,
258
+ string containerId = "" ,
259
+ SaslMechanism ? saslMechanism = null ,
260
+ IRecoveryConfiguration ? recoveryConfiguration = null ,
261
+ uint ? maxFrameSize = null ,
215
262
TlsSettings ? tlsSettings = null )
263
+ : this ( containerId , saslMechanism , recoveryConfiguration , maxFrameSize , tlsSettings )
216
264
{
217
265
_address = new Address ( host : host , port : port ,
218
266
user : user , password : password ,
219
267
path : "/" , scheme : scheme ) ;
220
268
_addresses . Add ( _address ) ;
221
- _containerId = containerId ;
222
- _virtualHost = virtualHost ;
223
- _saslMechanism = saslMechanism ;
224
269
225
- _maxFrameSize = maxFrameSize ;
226
- if ( _maxFrameSize != uint . MinValue && _maxFrameSize < 512 )
270
+ if ( virtualHost is not null )
227
271
{
228
- throw new ArgumentOutOfRangeException ( nameof ( maxFrameSize ) ,
229
- "maxFrameSize must be greater or equal to 512" ) ;
272
+ _virtualHost = virtualHost ;
230
273
}
231
274
232
- _tlsSettings = tlsSettings ;
233
-
234
275
if ( _address . UseSsl && _tlsSettings == null )
235
276
{
236
277
_tlsSettings = new TlsSettings ( ) ;
237
278
}
279
+ }
280
+
281
+ private ConnectionSettings (
282
+ string ? containerId = null ,
283
+ SaslMechanism ? saslMechanism = null ,
284
+ IRecoveryConfiguration ? recoveryConfiguration = null ,
285
+ uint ? maxFrameSize = null ,
286
+ TlsSettings ? tlsSettings = null )
287
+ {
288
+ if ( containerId is not null )
289
+ {
290
+ _containerId = containerId ;
291
+ }
238
292
239
- _recoveryConfiguration = recoveryConfiguration ;
293
+ if ( saslMechanism is not null )
294
+ {
295
+ _saslMechanism = saslMechanism ;
296
+ }
297
+
298
+ if ( recoveryConfiguration is not null )
299
+ {
300
+ _recoveryConfiguration = recoveryConfiguration ;
301
+ }
302
+
303
+ if ( maxFrameSize is not null )
304
+ {
305
+ _maxFrameSize = ( uint ) maxFrameSize ;
306
+ if ( _maxFrameSize != Consts . DefaultMaxFrameSize && _maxFrameSize < 512 )
307
+ {
308
+ throw new ArgumentOutOfRangeException ( nameof ( maxFrameSize ) ,
309
+ "maxFrameSize must be 0 (no limit) or greater than or equal to 512" ) ;
310
+ }
311
+ }
312
+
313
+ _tlsSettings = tlsSettings ;
240
314
}
241
315
242
316
public string Host => _address . Host ;
0 commit comments