Winify – Diff between revs 80 and 83

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 80 Rev 83
Line 56... Line 56...
56 private readonly Stopwatch _webSocketsClientPingStopWatch; 56 private readonly Stopwatch _webSocketsClientPingStopWatch;
57 private readonly ScheduledContinuation _webSocketsServerResponseScheduledContinuation; 57 private readonly ScheduledContinuation _webSocketsServerResponseScheduledContinuation;
Line 58... Line 58...
58 58
59 private Task _retrievePastMessagesTask; 59 private Task _retrievePastMessagesTask;
-   60 private static JsonSerializer _jsonSerializer;
-   61 private readonly CancellationToken _programCancellationToken;
-   62 private CancellationTokenSource _localCancellationTokenSource;
Line 60... Line 63...
60 private static JsonSerializer _jsonSerializer; 63 private CancellationToken _localCancellationToken;
Line 61... Line 64...
61   64  
Line 116... Line 119...
116   119  
117 _tplWebSocketsBufferBlockTransformLink = _webSocketMessageBufferBlock.LinkTo(webSocketActionBlock, 120 _tplWebSocketsBufferBlockTransformLink = _webSocketMessageBufferBlock.LinkTo(webSocketActionBlock,
118 new DataflowLinkOptions { PropagateCompletion = true }); 121 new DataflowLinkOptions { PropagateCompletion = true });
Line 119... Line 122...
119 } 122 }
120   123  
121 public GotifyConnection(Server server, Configuration.Configuration configuration) : this() 124 public GotifyConnection(Server server, Configuration.Configuration configuration, CancellationToken cancellationToken) : this()
122 { 125 {
-   126 _server = server;
Line 123... Line 127...
123 _server = server; 127 _configuration = configuration;
124 _configuration = configuration; 128 _programCancellationToken = cancellationToken;
125   129  
126 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 130 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Line 170... Line 174...
170 _webSocketsUri = combinedUri; 174 _webSocketsUri = combinedUri;
171 } 175 }
Line 172... Line 176...
172   176  
173 public void Dispose() 177 public void Dispose()
174 { 178 {
175 if (_cancellationTokenSource != null) 179 if (_localCancellationTokenSource != null)
176 { 180 {
177 _cancellationTokenSource.Dispose(); 181 _localCancellationTokenSource.Dispose();
178 _cancellationTokenSource = null; 182 _localCancellationTokenSource = null;
Line 179... Line 183...
179 } 183 }
180   184  
181 if (_tplWebSocketsBufferBlockTransformLink != null) 185 if (_tplWebSocketsBufferBlockTransformLink != null)
Line 205... Line 209...
205   209  
Line 206... Line 210...
206 #endregion 210 #endregion
Line -... Line 211...
-   211  
-   212 #region Public Methods
-   213  
-   214 public async Task Stop()
-   215 {
-   216 _localCancellationTokenSource.Cancel();
-   217  
-   218 if (_heartBeatTask != null)
-   219 {
-   220 await _heartBeatTask;
-   221 }
-   222  
-   223 if (_retrievePastMessagesTask != null)
-   224 {
-   225 await _retrievePastMessagesTask;
-   226 }
-   227  
-   228 if (_webSocketSharp != null)
-   229 {
-   230 _webSocketSharp.OnMessage -= WebSocketSharp_OnMessage;
-   231 _webSocketSharp.OnError -= WebSocketSharp_OnError;
-   232 _webSocketSharp.OnOpen -= WebSocketSharp_OnOpen;
-   233 _webSocketSharp.OnClose -= WebSocketSharp_OnClose;
-   234  
-   235 _webSocketSharp.Close();
-   236 _webSocketSharp = null;
207   237 }
208 #region Public Methods 238 }
209   239  
210 public void Start() 240 public void Start()
211 { 241 {
212 if (_webSocketsUri == null || _httpUri == null) 242 if (_webSocketsUri == null || _httpUri == null)
213 { 243 {
Line -... Line 244...
-   244 Log.Error("Could not start connection to server due to unreadable URLs");
214 Log.Error("Could not start connection to server due to unreadable URLs"); 245 return;
-   246 }
-   247  
-   248  
215 return; 249 _localCancellationTokenSource = new CancellationTokenSource();
Line 216... Line 250...
216 } 250 _localCancellationToken = _localCancellationTokenSource.Token;
217   251  
218 _cancellationTokenSource = new CancellationTokenSource(); 252 _cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(new [] { _programCancellationToken, _localCancellationToken });
Line 267... Line 301...
267 } 301 }
268 } 302 }
Line 269... Line 303...
269   303  
270 private async void WebSocketSharp_OnClose(object sender, CloseEventArgs e) 304 private async void WebSocketSharp_OnClose(object sender, CloseEventArgs e)
271 { -  
272 Log.Information( 305 {
Line -... Line 306...
-   306 Log.Information($"WebSockets connection to server {_webSocketsUri.AbsoluteUri} closed with reason {e.Reason}");
-   307  
273 $"WebSockets connection to server {_webSocketsUri.AbsoluteUri} closed with reason {e.Reason}"); 308 try
Line 274... Line 309...
274   309 {
Line 275... Line 310...
275 await Task.Delay(TimeSpan.FromSeconds(1), _cancellationToken); 310 await Task.Delay(TimeSpan.FromSeconds(1), _programCancellationToken);
-   311  
-   312 Log.Information($"Reconnecting to websocket server {_webSocketsUri.AbsoluteUri}");
-   313  
-   314 await Stop().ContinueWith(task => Start(), _programCancellationToken);
-   315 }
276   316 catch (Exception exception)
Line 277... Line 317...
277 Log.Information($"Reconnecting to websocket server {_webSocketsUri.AbsoluteUri}"); 317 {
278   318 Log.Error(exception, "Error restarting WebSockets connection on connection closed.");
279 await Stop().ContinueWith(task => Start(), CancellationToken.None); 319 }
Line 280... Line 320...
280 } 320 }
281   321  
Line 282... Line 322...
282 private void WebSocketSharp_OnOpen(object sender, EventArgs e) 322 private void WebSocketSharp_OnOpen(object sender, EventArgs e)
283 { 323 {
284 Log.Information($"WebSockets connection to server {_webSocketsUri.AbsoluteUri} is now open"); 324 Log.Information($"WebSockets connection to server {_webSocketsUri.AbsoluteUri} is now open");
Line -... Line 325...
-   325  
-   326 _webSocketsServerResponseScheduledContinuation.Schedule(TimeSpan.FromMinutes(1), OnServerNotResponding, _cancellationToken);
285   327 }
Line 286... Line 328...
286 _webSocketsServerResponseScheduledContinuation.Schedule(TimeSpan.FromMinutes(1), OnUnresponsiveServer, _cancellationToken); 328  
Line 287... Line 329...
287 } 329 private async void OnServerNotResponding()
-   330 {
-   331 Log.Warning($"Server {_server.Name} has not responded in a long while...");
-   332  
-   333 try
-   334 {
288   335 await Task.Delay(TimeSpan.FromSeconds(1), _programCancellationToken);
Line 289... Line 336...
289 private async void OnUnresponsiveServer() 336  
290 { 337 Log.Information($"Reconnecting to websocket server {_webSocketsUri.AbsoluteUri}");
291 Log.Warning($"Server {_server.Name} has not responded in a long while..."); 338  
292   339 await Stop().ContinueWith(task => Start(), _programCancellationToken);
293 await Task.Delay(TimeSpan.FromSeconds(1), _cancellationToken); 340 }
Line -... Line 341...
-   341 catch (Exception exception)
-   342 {
294   343 Log.Error(exception, "Error restarting WebSockets connection on connection closed.");
Line 295... Line 344...
295 Log.Information($"Reconnecting to websocket server {_webSocketsUri.AbsoluteUri}"); 344 }
Line 296... Line 345...
296   345 }
-   346  
-   347 private async void WebSocketSharp_OnError(object sender, ErrorEventArgs e)
-   348 {
-   349 Log.Error(
-   350 $"Connection to WebSockets server {_webSocketsUri.AbsoluteUri} terminated unexpectedly with message {e.Message}",
297 await Stop().ContinueWith(task => Start(), CancellationToken.None); 351 e.Exception);
Line 298... Line 352...
298 } 352  
299   353 try
300 private async void WebSocketSharp_OnError(object sender, ErrorEventArgs e) 354 {
301 { 355 await Task.Delay(TimeSpan.FromSeconds(1), _programCancellationToken);
302 Log.Error( 356  
Line 303... Line 357...
303 $"Connection to WebSockets server {_webSocketsUri.AbsoluteUri} terminated unexpectedly with message {e.Message}", 357 Log.Information($"Reconnecting to websocket server {_webSocketsUri.AbsoluteUri}");
304 e.Exception); 358  
305   359 await Stop().ContinueWith(task => Start(), _programCancellationToken);
Line 306... Line 360...
306 await Task.Delay(TimeSpan.FromSeconds(1), _cancellationToken); 360 }
307   361 catch (Exception exception)
Line 308... Line -...
308 Log.Information($"Reconnecting to websocket server {_webSocketsUri.AbsoluteUri}"); -  
309   -  
310 await Stop().ContinueWith(task => Start(), CancellationToken.None); -  
311 } -  
312   -  
313 private async void WebSocketSharp_OnMessage(object sender, MessageEventArgs e) -  
314 { -  
315 if (e.IsPing) -  
316 { -  
317 Log.Information($"Server {_server.Name} sent PING message"); -  
318   -  
319 _webSocketsServerResponseScheduledContinuation.Schedule(TimeSpan.FromMinutes(1), OnUnresponsiveServer, _cancellationToken); -  
320 return; -  
321 } -  
322   -  
323 await _webSocketMessageBufferBlock.SendAsync(new GotifyConnectionData(e.RawData, _server), _cancellationToken); -  
324 } -  
325   -  
326 public async Task Stop() -  
327 { -  
328   -  
329 if (_webSocketSharp == null || _cancellationTokenSource == null) -  
330 { -  
331 return; -  
332 } -  
333   -  
334 _cancellationTokenSource.Cancel(); -  
335   -  
336 if (_heartBeatTask != null) -  
337 { -  
338 await _heartBeatTask; -  
339 } -  
340   -  
341 if (_retrievePastMessagesTask != null) -  
342 { -  
343 await _retrievePastMessagesTask; 362 {
Line 344... Line 363...
344 } 363 Log.Error(exception, "Error restarting WebSockets connection on connection closed.");
Line 345... Line 364...
345   364 }
Line 459... Line 478...
459   478  
Line 460... Line 479...
460 var delta = _webSocketsClientPingStopWatch.ElapsedMilliseconds; 479 var delta = _webSocketsClientPingStopWatch.ElapsedMilliseconds;
Line 461... Line 480...
461   480  
Line 462... Line 481...
462 Log.Information($"PING response latency for {_server.Name} is {delta}ms"); 481 Log.Information($"PING response latency for {_server.Name} is {delta}ms");
463   482  
464 _webSocketsServerResponseScheduledContinuation.Schedule(TimeSpan.FromMinutes(1), OnUnresponsiveServer, _cancellationToken); 483 _webSocketsServerResponseScheduledContinuation.Schedule(TimeSpan.FromMinutes(1), OnServerNotResponding, _cancellationToken);
465   484