/
ClientOptions.cs
281 lines (222 loc) · 9.25 KB
/
ClientOptions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using System;
using System.Threading;
using IO.Ably;
using IO.Ably.Rest;
using IO.Ably.Transport;
namespace IO.Ably
{
public class ClientOptions : AuthOptions
{
private string _clientId;
private Func<DateTimeOffset> _nowFunc;
private bool _useBinaryProtocol = false;
private string[] _fallbackHosts;
public bool AutoConnect { get; set; } = true;
/// <summary>
/// The id of the client represented by this instance. The clientId is relevant
/// to presence operations, where the clientId is the principal identifier of the
/// client in presence update messages. The clientId is also relevant to
/// authentication; a token issued for a specific client may be used to authenticate
/// the bearer of that token to the service.
/// </summary>
public string ClientId
{
get => _clientId;
set
{
if (value == "*")
{
throw new InvalidOperationException("Wildcard clientIds are not support in ClientOptions");
}
_clientId = value;
}
}
public TokenParams DefaultTokenParams { get; set; }
internal string GetClientId()
{
if (ClientId.IsNotEmpty())
{
return ClientId;
}
return DefaultTokenParams?.ClientId;
}
/// <summary>
/// If <c>false</c>, this disables the default behaviour whereby the library queues messages on a
/// connection in the disconnected or connecting states. The default behaviour allows applications
/// to submit messages immediately upon instancing the library, without having to wait for the
/// connection to be established. Applications may use this option to disable that behaviour if they
/// wish to have application-level control over the queueing under those conditions.
/// </summary>
public bool QueueMessages { get; set; } = true;
/// <summary>
/// If <c>false</c>, prevents messages originating from this connection being echoed back on the same
/// connection.
/// </summary>
public bool EchoMessages { get; set; } = true;
/// <summary>
/// A connection recovery string, specified by a client when initializing the library
/// with the intention of inheriting the state of an earlier connection. See the Ably
/// Realtime API documentation for further information on connection state recovery.
/// </summary>
public string Recover { get; set; }
/// <summary>
/// For development environments only. Allows a non default host for the realtime service
/// </summary>
public string RealtimeHost { get; set; }
public LogLevel? LogLevel { get; set; }
public ILoggerSink LogHander { get; set; }
/// <summary>
/// For development environments only. Allows a non default host for the rest service
/// </summary>
public string RestHost { get; set; }
/// <summary>
/// Gets or sets an array of custom Fallback hosts to be (optionally) used in place of the defaults.
/// If an empty array is specified, then fallback host functionality is disabled.
/// </summary>
public string[] FallbackHosts
{
get
{
if (_fallbackHosts is null)
{
return Defaults.FallbackHosts;
}
return _fallbackHosts;
}
set => _fallbackHosts = value;
}
/// <summary>
/// Gets or sets a value indicating whether to use default FallbackHosts even when overriding
/// environment or restHost/realtimeHost
/// </summary>
public bool FallbackHostsUseDefault { get; set; }
internal bool IsLiveEnvironment => Environment.IsEmpty() || Environment == "live";
internal bool IsDefaultRestHost => RestHost.IsEmpty() && IsDefaultPort && IsLiveEnvironment;
internal bool IsDefaultRealtimeHost => RealtimeHost.IsEmpty() && IsDefaultPort && IsLiveEnvironment;
internal bool IsDefaultPort => Tls ? Port == 80 : TlsPort == 443;
internal string FullRealtimeHost()
{
if (RealtimeHost.IsEmpty())
{
if (IsLiveEnvironment)
{
return Defaults.RealtimeHost;
}
return Environment.ToString().ToLower() + "-" + Defaults.RealtimeHost;
}
return RealtimeHost;
}
internal string FullRestHost()
{
if (RestHost.IsEmpty())
{
if (IsLiveEnvironment)
{
return Defaults.RestHost;
}
return Environment.ToString().ToLower() + "-" + Defaults.RestHost;
}
return RestHost;
}
/// <summary>
/// For development environments only; allows a non-default Ably port to be specified.
/// </summary>
public int Port { get; set; } = 80;
/// <summary>
/// Encrypted transport: if true, TLS will be used for all connections (whether REST/HTTP
/// or Realtime WebSocket or Comet connections).
/// Default: true
/// </summary>
public bool Tls { get; set; } = true;
public int TlsPort { get; set; } = 443;
/// <summary>
/// If false, forces the library to use the JSON encoding for REST and Realtime operations,
/// If true, the MsgPack binary format is used (if available in the current build
/// Default: true
/// </summary>
public bool UseBinaryProtocol
{
#if MSGPACK
get { return _useBinaryProtocol; }
#else
get { return false; }
#endif
set { _useBinaryProtocol = value; }
}
public TimeSpan DisconnectedRetryTimeout { get; set; } = Defaults.DisconnectedRetryTimeout;
public TimeSpan SuspendedRetryTimeout { get; set; } = TimeSpan.FromSeconds(30);
public TimeSpan ChannelRetryTimeout { get; set; } = TimeSpan.FromSeconds(15);
public TimeSpan HttpOpenTimeout { get; set; } = TimeSpan.FromSeconds(4);
public TimeSpan HttpRequestTimeout { get; set; } = TimeSpan.FromSeconds(10);
public int HttpMaxRetryCount { get; set; } = 3;
public TimeSpan HttpMaxRetryDuration { get; set; } = TimeSpan.FromSeconds(15);
/// <summary>
/// Provides Channels Setting for all Channels created. For more information see <see cref="ChannelOptions"/>
/// </summary>
public ChannelOptions ChannelDefaults { get; internal set; } = new ChannelOptions();
public string Environment { get; set; }
public ITransportFactory TransportFactory { get; set; }
public bool CaptureCurrentSynchronizationContext { get; set; } = true;
public SynchronizationContext CustomContext { get; set; }
public bool IdempotentRestPublishing { get; set; }
internal Func<DateTimeOffset> NowFunc
{
get => _nowFunc ?? (_nowFunc = Defaults.NowFunc());
set => _nowFunc = value;
}
internal ILogger Logger { get; set; } = DefaultLogger.LoggerInstance;
internal AuthMethod Method
{
get
{
if (ClientId.IsNotEmpty() || AuthUrl != null || AuthCallback != null || Token.IsNotEmpty() ||
TokenDetails != null)
{
return Ably.AuthMethod.Token;
}
if (Key.IsNotEmpty())
{
return Ably.AuthMethod.Basic;
}
// default
return Ably.AuthMethod.Token;
}
}
internal bool SkipInternetCheck { get; set; } = false;
internal bool UseSyncForTesting { get; set; } = false;
internal TimeSpan RealtimeRequestTimeout { get; set; } = Defaults.DefaultRealtimeTimeout;
/// <summary>
/// Default constructor for ClientOptions
/// </summary>
public ClientOptions()
{
Init();
}
/// <summary>
/// Construct ClientOptions class and set the Key
/// It automatically parses the key to ensure the correct format is used and sets the KeyId and KeyValue properties
/// </summary>
/// <param name="key">Ably authentication key</param>
public ClientOptions(string key)
: base(key)
{
Init();
}
private void Init()
{
SetIdempotentRestPublishingDefault(Defaults.ProtocolMajorVersion, Defaults.ProtocolMinorVersion);
}
internal void SetIdempotentRestPublishingDefault(int majorVersion, int minorVersion)
{
// (TO3n) idempotentRestPublishing defaults to false for clients with version < 1.1, otherwise true.
if ((majorVersion == 1 && minorVersion >= 1) || majorVersion > 1)
{
IdempotentRestPublishing = true;
}
else
{
IdempotentRestPublishing = false;
}
}
}
}