Winify

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 79  →  ?path2? @ 80
/trunk/Winify/Gotify/GotifyConnection.cs
@@ -87,7 +87,7 @@
 
gotifyMessage.Server = gotifyConnectionData.Server;
 
using var imageStream = await RetrieveGotifyApplicationImage(gotifyMessage.AppId, _cancellationToken);
using var imageStream = await RetrieveGotifyApplicationImage(gotifyMessage.AppId);
 
if (imageStream == null || imageStream.Length == 0)
{
@@ -228,16 +228,25 @@
_webSocketSharp.WaitTime = TimeSpan.FromMinutes(1);
_webSocketSharp.SslConfiguration = new ClientSslConfiguration(_webSocketsUri.Host,
new X509CertificateCollection(new X509Certificate[] { }), SslProtocols.Tls12, false);
 
if (_configuration.Proxy.Enable)
_webSocketSharp.SetProxy(_configuration.Proxy.Url, _configuration.Proxy.Username,
_configuration.Proxy.Password);
{
if (!string.IsNullOrEmpty(_configuration.Proxy.Url))
{
_webSocketSharp.SetProxy(_configuration.Proxy.Url, _configuration.Proxy.Username, _configuration.Proxy.Password);
}
}
 
if (!string.IsNullOrEmpty(_server.Username) && !string.IsNullOrEmpty(_server.Password))
{
_webSocketSharp.SetCredentials(_server.Username, _server.Password, true);
}
 
if (_configuration.IgnoreSelfSignedCertificates)
{
_webSocketSharp.SslConfiguration.ServerCertificateValidationCallback +=
(sender, certificate, chain, errors) => true;
}
 
_webSocketSharp.Log.Output = (logData, s) =>
{
@@ -258,10 +267,16 @@
}
}
 
private void WebSocketSharp_OnClose(object sender, CloseEventArgs e)
private async void WebSocketSharp_OnClose(object sender, CloseEventArgs e)
{
Log.Information(
$"WebSockets connection to server {_webSocketsUri.AbsoluteUri} closed with reason {e.Reason}");
 
await Task.Delay(TimeSpan.FromSeconds(1), _cancellationToken);
 
Log.Information($"Reconnecting to websocket server {_webSocketsUri.AbsoluteUri}");
 
await Stop().ContinueWith(task => Start(), CancellationToken.None);
}
 
private void WebSocketSharp_OnOpen(object sender, EventArgs e)
@@ -271,9 +286,15 @@
_webSocketsServerResponseScheduledContinuation.Schedule(TimeSpan.FromMinutes(1), OnUnresponsiveServer, _cancellationToken);
}
 
private void OnUnresponsiveServer()
private async void OnUnresponsiveServer()
{
Log.Warning($"Server {_server.Name} has not responded in a long while...");
 
await Task.Delay(TimeSpan.FromSeconds(1), _cancellationToken);
 
Log.Information($"Reconnecting to websocket server {_webSocketsUri.AbsoluteUri}");
 
await Stop().ContinueWith(task => Start(), CancellationToken.None);
}
 
private async void WebSocketSharp_OnError(object sender, ErrorEventArgs e)
@@ -364,7 +385,7 @@
 
if (gotifyMessageQuery.Messages == null)
{
Log.Warning("Invalid application messages deserialized deserialized.");
Log.Warning("Invalid application messages deserialized.");
 
return;
}
@@ -371,13 +392,12 @@
 
foreach (var message in gotifyMessageQuery.Messages)
{
if (message.Date <
DateTime.Now - TimeSpan.FromHours(_configuration.RetrievePastNotificationHours))
if (message.Date < DateTime.Now - TimeSpan.FromHours(_configuration.RetrievePastNotificationHours))
{
continue;
}
 
using var imageStream = await RetrieveGotifyApplicationImage(message.AppId, _cancellationToken);
using var imageStream = await RetrieveGotifyApplicationImage(message.AppId);
if (imageStream == null || imageStream.Length == 0)
{
Log.Warning("Could not find any application image for notification.");
@@ -388,8 +408,7 @@
var image = new Bitmap(imageStream);
message.Server = gotifyConnectionApplication.Server;
 
GotifyMessage?.Invoke(this,
new GotifyMessageEventArgs(message, image));
GotifyMessage?.Invoke(this, new GotifyMessageEventArgs(message, image));
}
}
catch (HttpRequestException exception)
@@ -407,13 +426,14 @@
}
}, new ExecutionDataflowBlockOptions { CancellationToken = cancellationToken });
 
gotifyApplicationBufferBlock.LinkTo(gotifyApplicationActionBlock,
using var _1 = gotifyApplicationBufferBlock.LinkTo(gotifyApplicationActionBlock,
new DataflowLinkOptions { PropagateCompletion = true });
 
var tasks = new ConcurrentBag<Task>();
await foreach (var application in RetrieveGotifyApplications(cancellationToken))
foreach (var application in await RetrieveGotifyApplications())
{
var gotifyConnectionApplication = new GotifyConnectionApplication(_server, application);
 
tasks.Add(gotifyApplicationBufferBlock.SendAsync(gotifyConnectionApplication, cancellationToken));
}
 
@@ -442,6 +462,7 @@
Log.Information($"PING response latency for {_server.Name} is {delta}ms");
 
_webSocketsServerResponseScheduledContinuation.Schedule(TimeSpan.FromMinutes(1), OnUnresponsiveServer, _cancellationToken);
 
} while (!cancellationToken.IsCancellationRequested);
}
catch (Exception exception) when (exception is OperationCanceledException ||
@@ -454,16 +475,15 @@
}
}
 
private async IAsyncEnumerable<GotifyApplication> RetrieveGotifyApplications([EnumeratorCancellation] CancellationToken cancellationToken)
private async Task<GotifyApplication[]> RetrieveGotifyApplications()
{
if (!Uri.TryCreate(_httpUri, "application", out var combinedUri))
{
Log.Error($"No application URL could be built for {_server.Url}.");
 
yield break;
return Array.Empty<GotifyApplication>();
}
 
GotifyApplication[] gotifyApplications;
try
{
using var messageStream = await _httpClient.GetStreamAsync(combinedUri);
@@ -470,29 +490,21 @@
using var streamReader = new StreamReader(messageStream);
using var jsonTextReader = new JsonTextReader(streamReader);
 
gotifyApplications = _jsonSerializer.Deserialize<GotifyApplication[]>(jsonTextReader);
 
if (gotifyApplications == null)
{
throw new ArgumentNullException();
}
return _jsonSerializer.Deserialize<GotifyApplication[]>(jsonTextReader);
}
catch (Exception exception)
{
Log.Warning(exception,"Could not deserialize the list of applications from the server.");
 
yield break;
return Array.Empty<GotifyApplication>();
}
 
foreach (var application in gotifyApplications)
{
yield return application;
}
}
 
private async Task<Stream> RetrieveGotifyApplicationImage(int appId, CancellationToken cancellationToken)
private async Task<Stream> RetrieveGotifyApplicationImage(int appId)
{
await foreach (var application in RetrieveGotifyApplications(cancellationToken))
var memoryStream = new MemoryStream();
 
foreach (var application in await RetrieveGotifyApplications())
{
if (application.Id != appId)
{
@@ -508,10 +520,8 @@
 
try
{
var imageResponse = await _httpClient.GetStreamAsync(applicationImageUri);
using var imageResponse = await _httpClient.GetStreamAsync(applicationImageUri);
 
var memoryStream = new MemoryStream();
 
await imageResponse.CopyToAsync(memoryStream);
 
return memoryStream;
@@ -522,7 +532,7 @@
}
}
 
return new MemoryStream();
return memoryStream;
}
 
#endregion