Winify – Diff between revs 24 and 25

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 24 Rev 25
Line 22... Line 22...
22   22  
Line 23... Line 23...
23 #endregion 23 #endregion
Line -... Line 24...
-   24  
-   25 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
24   26  
Line 25... Line 27...
25 #region Private Delegates, Events, Enums, Properties, Indexers and Fields 27 private readonly Server _server;
Line 26... Line -...
26   -  
27 private CancellationToken _cancellationToken; -  
28   28  
Line 29... Line 29...
29 private CancellationTokenSource _cancellationTokenSource; 29 private CancellationToken _cancellationToken;
Line 30... Line 30...
30   30  
Line 31... Line 31...
31 private HttpClient _httpClient; 31 private CancellationTokenSource _cancellationTokenSource;
32   32  
33 private Task _runTask; 33 private Task _runTask;
34   34  
Line 35... Line -...
35 private ClientWebSocket _webSocketClient; -  
36   -  
37 private readonly Server _server; -  
38   -  
39 public GotifyConnection(Server server) 35 #endregion
40 { 36  
41 _server = server; 37 #region Constructors, Destructors and Finalizers
42 } 38  
43   39 public GotifyConnection(Server server)
44 #endregion 40 {
45   41 _server = server;
46 #region Constructors, Destructors and Finalizers -  
47   -  
48 public void Dispose() -  
49 { -  
50 if (_cancellationTokenSource != null) -  
51 { -  
52 _cancellationTokenSource.Dispose(); -  
53 _cancellationTokenSource = null; -  
54 } -  
55   -  
56 if (_webSocketClient != null) -  
57 { -  
58 _webSocketClient.Dispose(); 42 }
Line 59... Line 43...
59 _webSocketClient = null; 43  
Line 60... Line 44...
60 } 44 public void Dispose()
Line 61... Line 45...
61   45 {
62 if (_httpClient != null) 46 if (_cancellationTokenSource != null)
63 { 47 {
64 _httpClient.Dispose(); 48 _cancellationTokenSource.Dispose();
65 _httpClient = null; 49 _cancellationTokenSource = null;
66 } 50 }
Line 67... Line 51...
67 } 51 }
Line 84... Line 68...
84 var webSocketsUri = webSocketsUriBuilder.Uri; 68 var webSocketsUri = webSocketsUriBuilder.Uri;
Line 85... Line 69...
85   69  
86 _cancellationTokenSource = new CancellationTokenSource(); 70 _cancellationTokenSource = new CancellationTokenSource();
Line 87... Line -...
87 _cancellationToken = _cancellationTokenSource.Token; -  
88   -  
89 _httpClient = new HttpClient(); -  
90   -  
91 var auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{username}:{password}")); -  
92   -  
93 _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", auth); 71 _cancellationToken = _cancellationTokenSource.Token;
94   72  
Line 95... Line 73...
95 _runTask = Run(webSocketsUri, httpUri, username, password, _cancellationToken); 73 _runTask = Run(webSocketsUri, httpUri, _server.Username, _server.Password, _cancellationToken);
96 } 74 }
97   75  
98 public void Stop() 76 public void Stop()
99 { 77 {
100 if (_cancellationTokenSource != null) 78 if (_cancellationTokenSource != null)
101 { -  
102 _cancellationTokenSource.Cancel(); -  
103 } 79 {
Line 104... Line 80...
104   80 _cancellationTokenSource.Cancel();
Line 105... Line 81...
105 _runTask.Wait(CancellationToken.None); 81 }
Line 116... Line 92...
116 { 92 {
117 do 93 do
118 { 94 {
119 try 95 try
120 { 96 {
121 using (_webSocketClient = new ClientWebSocket()) 97 using (var webSocketClient = new ClientWebSocket())
122 { 98 {
123 var auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{username}:{password}")); 99 var auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{username}:{password}"));
Line 124... Line 100...
124   100  
Line 125... Line 101...
125 _webSocketClient.Options.SetRequestHeader("Authorization", $"Basic {auth}"); 101 webSocketClient.Options.SetRequestHeader("Authorization", $"Basic {auth}");
Line 126... Line 102...
126   102  
127 await _webSocketClient.ConnectAsync(webSocketsUri, cancellationToken); 103 await webSocketClient.ConnectAsync(webSocketsUri, cancellationToken);
128   104  
Line 129... Line 105...
129 do 105 do
Line 130... Line 106...
130 { 106 {
131 var payload = new ArraySegment<byte>(new byte[1024]); 107 var payload = new ArraySegment<byte>(new byte[1024]);
132   108  
133 var result = await _webSocketClient.ReceiveAsync(payload, cancellationToken); 109 var result = await webSocketClient.ReceiveAsync(payload, cancellationToken);
Line 142... Line 118...
142 continue; 118 continue;
143 } 119 }
Line 144... Line 120...
144   120  
Line 145... Line 121...
145 var message = Encoding.UTF8.GetString(payload.Array, 0, payload.Count); 121 var message = Encoding.UTF8.GetString(payload.Array, 0, payload.Count);
146   122  
147 var gotifyNotification = JsonConvert.DeserializeObject<GotifyNotification>(message); 123 if (!(JsonConvert.DeserializeObject<GotifyNotification>(message) is GotifyNotification
148 if (gotifyNotification == null) 124 gotifyNotification))
149 { -  
Line 150... Line -...
150 continue; -  
151 } -  
152   -  
153 gotifyNotification.Server = _server; -  
154   125 {
155 if (!Uri.TryCreate($"{httpUri}/application", UriKind.Absolute, out var applicationUri)) 126 Log.Warning($"Could not deserialize gotify notification: {message}");
Line 156... Line 127...
156 { 127  
Line 157... Line -...
157 continue; -  
158 } 128 continue;
159   129 }
160 var applications = await _httpClient.GetStringAsync(applicationUri); 130  
161   131 gotifyNotification.Server = _server;
162 var gotifyApplications = 132  
Line 163... Line -...
163 JsonConvert.DeserializeObject<GotifyApplication[]>(applications); -  
164 if (gotifyApplications == null) -  
165 { 133 if (!Uri.TryCreate($"{httpUri}/application", UriKind.Absolute,
166 continue; -  
167 } -  
168   -  
169 foreach (var application in gotifyApplications) -  
170 { -  
171 if (application.Id != gotifyNotification.AppId) 134 out var applicationUri))
172 { -  
173 continue; -  
174 } -  
175   -  
176 if (!Uri.TryCreate($"{httpUri}/{application.Image}", UriKind.Absolute, -  
177 out var applicationImageUri)) -  
178 { -  
179 continue; -  
180 } -  
181   -  
182 var imageBytes = await _httpClient.GetByteArrayAsync(applicationImageUri); -  
183   -  
184 if (imageBytes == null || imageBytes.Length == 0) -  
185 { -  
186 continue; -  
187 } -  
188   -  
189 using (var memoryStream = new MemoryStream(imageBytes)) -  
Line 190... Line -...
190 { -  
191 var image = Image.FromStream(memoryStream); 135 {
192   136 continue;
Line 193... Line 137...
193 GotifyNotification?.Invoke(this, 137 }
194 new GotifyNotificationEventArgs(gotifyNotification, image)); 138  
195 } -  
196   -  
197   -  
198 break; 139 var image = await RetrieveGotifyApplicationImage(gotifyNotification.AppId, httpUri,
199 } -  
200   -  
201 Log.Debug($"Notification message received: {gotifyNotification.Message}"); 140 applicationUri, auth, cancellationToken);
202 } while (!cancellationToken.IsCancellationRequested); 141  
203   142 GotifyNotification?.Invoke(this,
204 await _webSocketClient.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, -  
205 CancellationToken.None); -  
206 } 143 new GotifyNotificationEventArgs(gotifyNotification, image));
207   -  
208 _webSocketClient = null; -  
209 } -  
210 catch (Exception ex) when (ex is WebSocketException || ex is HttpRequestException) 144  
211 { -  
212 Log.Warning($"Unable to connect to gotify server: {ex.Message}"); -  
Line 213... Line 145...
213   145 Log.Debug($"Notification message received: {gotifyNotification.Message}");
214 // Reconnect 146 } while (!cancellationToken.IsCancellationRequested);
215 if (_webSocketClient != null) 147 }
216 { 148 }
Line 228... Line 160...
228 } 160 }
229 catch (Exception ex) 161 catch (Exception ex)
230 { 162 {
231 Log.Warning(ex, "Failure running connection loop."); 163 Log.Warning(ex, "Failure running connection loop.");
232 } 164 }
-   165  
-   166 var e = "o";
-   167 }
-   168  
-   169 private static async Task<Image> RetrieveGotifyApplicationImage(int appId, Uri httpUri, Uri applicationUri,
-   170 string auth,
-   171 CancellationToken cancellationToken)
-   172 {
-   173 using (var httpClient = new HttpClient())
-   174 {
-   175 httpClient.DefaultRequestHeaders.Authorization =
-   176 new AuthenticationHeaderValue("Basic", auth);
-   177  
-   178 var applicationResponse = await httpClient.GetAsync(applicationUri, cancellationToken);
-   179  
-   180 var applications = await applicationResponse.Content.ReadAsStringAsync();
-   181  
-   182 var gotifyApplications =
-   183 JsonConvert.DeserializeObject<GotifyApplication[]>(applications);
-   184  
-   185 if (gotifyApplications == null)
-   186 {
-   187 return null;
-   188 }
-   189  
-   190 foreach (var application in gotifyApplications)
-   191 {
-   192 if (application.Id != appId)
-   193 {
-   194 continue;
-   195 }
-   196  
-   197 if (!Uri.TryCreate($"{httpUri}/{application.Image}", UriKind.Absolute,
-   198 out var applicationImageUri))
-   199 {
-   200 continue;
-   201 }
-   202  
-   203 var imageResponse = await httpClient.GetAsync(applicationImageUri, cancellationToken);
-   204  
-   205 using (var memoryStream = new MemoryStream())
-   206 {
-   207 await imageResponse.Content.CopyToAsync(memoryStream);
-   208  
-   209 return Image.FromStream(memoryStream);
-   210 }
-   211 }
-   212 }
-   213  
-   214 return null;
233 } 215 }
Line 234... Line 216...
234   216  
235 #endregion 217 #endregion
236 } 218 }
237 } 219 }