-
Notifications
You must be signed in to change notification settings - Fork 849
Expand file tree
/
Copy pathRemoteControlClient.cs
More file actions
987 lines (858 loc) · 29 KB
/
RemoteControlClient.cs
File metadata and controls
987 lines (858 loc) · 29 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
#nullable enable
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Common;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.WebSockets;
using System.Runtime.InteropServices.JavaScript;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Uno.Extensions;
using Uno.Foundation;
using Uno.Foundation.Logging;
using Uno.UI.RemoteControl.Helpers;
using Uno.UI.RemoteControl.HotReload;
using Uno.UI.RemoteControl.HotReload.Messages;
using Uno.UI.RemoteControl.Messages;
using Windows.Networking.Sockets;
using Windows.Storage;
using static Uno.UI.RemoteControl.RemoteControlStatus;
namespace Uno.UI.RemoteControl;
public partial class RemoteControlClient : IRemoteControlClient, IAsyncDisposable
{
private readonly string? _additionalServerProcessorsDiscoveryPath;
private readonly bool _autoRegisterAppIdentity;
public delegate void RemoteControlFrameReceivedEventHandler(object sender, ReceivedFrameEventArgs args);
public delegate void RemoteControlClientEventEventHandler(object sender, ClientEventEventArgs args);
public delegate void SendMessageFailedEventHandler(object sender, SendMessageFailedEventArgs args);
public static RemoteControlClient? Instance
{
get => _instance;
private set
{
_instance = value;
if (value is { })
{
while (Interlocked.Exchange(ref _waitingList, null) is { } waitingList)
{
foreach (var action in waitingList)
{
action(value);
}
}
}
}
}
private static IReadOnlyCollection<Action<RemoteControlClient>>? _waitingList;
/// <summary>
/// Add a callback to be called when the Instance is available.
/// </summary>
/// <remarks>
/// Will be called synchronously if the instance is already available, no need to check for it before.
/// </remarks>
public static void OnRemoteControlClientAvailable(Action<RemoteControlClient> action)
{
if (Instance is { })
{
action(Instance);
}
else
{
// Thread-safe way to add the action to a waiting list for the client to be available
while (true)
{
var waitingList = _waitingList;
IReadOnlyCollection<Action<RemoteControlClient>> newList = waitingList is null
? [action]
: [.. waitingList, action];
if (Instance is { } i) // Last chance to avoid the waiting list
{
action(i);
break;
}
if (ReferenceEquals(Interlocked.CompareExchange(ref _waitingList, newList, waitingList), waitingList))
{
break;
}
}
}
}
/// <summary>
/// Initializes the remote control client for the current application.
/// </summary>
/// <param name="appType">The type of the application entry point (usually your App type).</param>
/// <returns>The initialized RemoteControlClient singleton instance.</returns>
/// <remarks>
/// This is the primary initialization entry point used by applications. It is invoked by generated code
/// in debug builds (see Uno XAML source generator) and relies on discovery of the dev-server endpoint via
/// environment variables or assembly attributes emitted at build time by the IDE integration.
/// </remarks>
public static RemoteControlClient Initialize(Type appType)
=> Instance = new RemoteControlClient(appType);
/// <summary>
/// Initializes the remote control client with explicit server endpoints.
/// </summary>
/// <param name="appType">The type of the application entry point.</param>
/// <param name="endpoints">Optional list of fallback endpoints to try when connecting to the dev-server.</param>
/// <returns>The initialized RemoteControlClient singleton instance.</returns>
/// <remarks>
/// This overload is internal and mainly intended for tests and advanced scenarios. It allows providing explicit
/// endpoints that will be used as a fallback in addition to values coming from environment variables or
/// assembly-level attributes. Typical application code should call <see cref="Initialize(Type)"/>.
/// </remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
internal static RemoteControlClient Initialize(Type appType, ServerEndpointAttribute[]? endpoints)
=> Instance = new RemoteControlClient(appType, endpoints);
/// <summary>
/// Initializes the remote control client with explicit server endpoints and an additional processors discovery path.
/// </summary>
/// <param name="appType">The type of the application entry point.</param>
/// <param name="endpoints">Optional list of fallback endpoints to try when connecting to the dev-server.</param>
/// <param name="additionalServerProcessorsDiscoveryPath">An optional absolute or relative path used to discover additional server processors.</param>
/// <param name="autoRegisterAppIdentity">Whether to automatically register the app identity (mvid - platform...) with the dev-server.</param>
/// <returns>The initialized RemoteControlClient singleton instance.</returns>
/// <remarks>
/// This overload is internal and primarily used by tests to inject additional server processors or assemblies
/// during discovery, and to control the endpoints to connect to. Application code should use <see cref="Initialize(Type)"/>.
/// </remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
internal static RemoteControlClient Initialize(
Type appType,
ServerEndpointAttribute[]? endpoints,
string? additionalServerProcessorsDiscoveryPath,
bool autoRegisterAppIdentity = true)
=> Instance = new RemoteControlClient(appType, endpoints, additionalServerProcessorsDiscoveryPath, autoRegisterAppIdentity);
public event RemoteControlFrameReceivedEventHandler? FrameReceived;
public event RemoteControlClientEventEventHandler? ClientEvent;
public event SendMessageFailedEventHandler? SendMessageFailed;
/// <summary>
/// Application type used to initialize this client.
/// </summary>
public Type AppType { get; }
/// <summary>
/// Gets the minimum interval between re-connection attempts.
/// </summary>
/// <remarks>This applies only if a connection has been established once and has been lost by then.</remarks>
public TimeSpan ConnectionRetryInterval { get; } = TimeSpan.FromMilliseconds(_connectionRetryInterval);
private const int _connectionRetryInterval = 5_000;
private readonly StatusSink _status;
private static readonly TimeSpan _keepAliveInterval = TimeSpan.FromSeconds(30);
private static RemoteControlClient? _instance;
private readonly (string endpoint, int port)[]? _serverAddresses;
private readonly Dictionary<string, IClientProcessor> _processors = new();
private readonly List<IRemoteControlPreProcessor> _preprocessors = new();
private readonly Lock _connectionGate = new();
private Task<Connection?> _connection; // null if no server, socket only null if connection was established once but lost since then
private Timer? _keepAliveTimer;
private KeepAliveMessage _ping = new();
private record Connection(RemoteControlClient Owner, Uri EndPoint, Stopwatch Since, WebSocket? Socket)
: IAsyncDisposable
{
private static class States
{
public const int Ready = 0;
public const int Active = 1;
public const int Disposed = 255;
}
private readonly CancellationTokenSource _ct = new();
private int _state = Socket is null ? States.Disposed : States.Ready;
public void EnsureActive()
{
if (Interlocked.CompareExchange(ref _state, States.Active, States.Ready) is States.Ready)
{
DevServerDiagnostics.Current = Owner._status;
_ = Owner.ProcessMessages(Socket!, _ct.Token);
}
}
/// <inheritdoc />
public async ValueTask DisposeAsync()
{
_state = States.Disposed;
await _ct.CancelAsync();
_ct.Dispose();
if (Socket is not null)
{
try
{
await Socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Client disconnected", CancellationToken.None);
}
catch { }
Socket.Dispose();
}
}
}
private RemoteControlClient(Type appType,
ServerEndpointAttribute[]? endpoints = null,
string? additionalServerProcessorsDiscoveryPath = null,
bool autoRegisterAppIdentity = true)
{
AppType = appType;
_additionalServerProcessorsDiscoveryPath = additionalServerProcessorsDiscoveryPath;
_autoRegisterAppIdentity = autoRegisterAppIdentity;
_status = new StatusSink(this);
var error = default(ConnectionError?);
// Environment variables are the first priority as they are used by runtime tests engine to test hot-reload.
// They should be considered as the default values and in any case they must take precedence over the assembly-provided values.
if (Environment.GetEnvironmentVariable("UNO_DEV_SERVER_HOST") is { Length: > 0 } host
&& Environment.GetEnvironmentVariable("UNO_DEV_SERVER_PORT") is { Length: > 0 } portRaw
&& int.TryParse(portRaw, out var port))
{
_serverAddresses = new[] { (host, port) };
}
// Get the addresses from the assembly attributes set by the code-gen in debug (i.e. from the IDE)
if (_serverAddresses is null or { Length: 0 }
&& appType.Assembly.GetCustomAttributes(typeof(ServerEndpointAttribute), false) is ServerEndpointAttribute[] { Length: > 0 } embeddedEndpoints)
{
IEnumerable<(string endpoint, int port)> GetAddresses()
{
foreach (var endpoint in embeddedEndpoints)
{
if (endpoint.Port is 0 && !Uri.TryCreate(endpoint.Endpoint, UriKind.Absolute, out _))
{
this.Log().LogInfo($"Failed to get dev-server port from the IDE for endpoint {endpoint.Endpoint}.");
}
else
{
yield return (endpoint.Endpoint, endpoint.Port);
}
}
}
_serverAddresses = GetAddresses().ToArray();
if (_serverAddresses is { Length: 0 })
{
error = ConnectionError.EndpointWithoutPort;
this.Log().LogError(
"Some endpoint for uno's dev-server has been configured in your application, but all are invalid (port is missing?). "
+ "This can usually be fixed with a **rebuild** of your application. "
+ "If not, make sure you have the latest version of the uno's extensions installed in your IDE and restart your IDE.");
}
else
{
// For WASM and desktop platforms, add loopback address for better reliability and airplane mode support
// Mobile platforms (iOS, Android) should not use loopback as they connect to a different machine
if ((OperatingSystem.IsBrowser()
|| OperatingSystem.IsWindows()
|| OperatingSystem.IsLinux()
|| OperatingSystem.IsMacOS()))
{
var serverPort = _serverAddresses.Select(addr => addr.port).Where(p => p > 0).FirstOrDefault();
if (serverPort > 0)
{
var loopbackAddress = IPAddress.Loopback.ToString().ToLowerInvariant();
var hasLoopback = _serverAddresses.Any(addr => addr.endpoint.Equals(loopbackAddress, StringComparison.OrdinalIgnoreCase) && addr.port == serverPort);
if (!hasLoopback)
{
// Prepend loopback address to give it priority
_serverAddresses = new[] { (loopbackAddress, serverPort) }.Concat(_serverAddresses).ToArray();
}
}
}
}
}
if (_serverAddresses is null or { Length: 0 })
{
_serverAddresses = endpoints
?.Select(ep => (ep.Endpoint, ep.Port))
.ToArray();
}
// Enable hot-reload
// Note: We register the HR processor even if we _serverAddresses is empty. This is to make sure to create the HR indicator.
RegisterProcessor(new HotReload.ClientHotReloadProcessor(this));
_status.RegisterRequiredServerProcessor("Uno.UI.RemoteControl.Host.HotReload.ServerHotReloadProcessor", VersionHelper.GetVersion(typeof(ClientHotReloadProcessor)));
if (_serverAddresses is null or { Length: 0 })
{
if (error is null)
{
error = ConnectionError.NoEndpoint;
this.Log().LogError(
"Failed to get any valid dev-server endpoint from the IDE."
+ "Make sure you have the latest version of the uno's extensions installed in your IDE and restart your IDE.");
}
_connection = Task.FromResult<Connection?>(null);
_status.Report(ConnectionState.NoServer, error);
return;
}
_connection = StartConnection();
}
public IEnumerable<object> Processors
=> _processors.Values;
internal IClientProcessor[] RegisteredProcessors
=> _processors.Values.ToArray();
internal Task WaitForConnection()
=> WaitForConnection(CancellationToken.None);
public Task WaitForConnection(CancellationToken ct)
=> _connection;
private void RegisterProcessor(IClientProcessor processor)
=> _processors[processor.Scope] = processor;
public void RegisterPreProcessor(IRemoteControlPreProcessor preprocessor)
=> _preprocessors.Add(preprocessor);
private async ValueTask<WebSocket?> GetActiveSocket()
{
var connectionTask = _connection;
var connection = await connectionTask;
if (connection is ({ Socket: null } or { Socket.State: not WebSocketState.Open }) and { Since.ElapsedMilliseconds: >= _connectionRetryInterval })
{
// We have a socket (and uri) but we lost the connection, try to reconnect (only if more than 5 sec since last attempt)
lock (_connectionGate)
{
_status.Report(ConnectionState.Reconnecting);
if (connectionTask == _connection)
{
_connection = Connect(connection.EndPoint, CancellationToken.None)!;
}
}
connection = await _connection;
connection?.EnsureActive();
}
_status.ReportActiveConnection(connection);
return connection?.Socket;
}
private async Task<Connection?> StartConnection()
{
try
{
if (_serverAddresses is null or { Length: 0 })
{
if (this.Log().IsEnabled(LogLevel.Warning))
{
this.Log().LogWarning("No server addresses provided, skipping.");
}
_status.Report(ConnectionState.NoServer);
return default;
}
var isHttps = false;
if (OperatingSystem.IsBrowser())
{
isHttps = string.Equals(WebAssemblyImports.EvalString("window.location.protocol"), "https:", StringComparison.OrdinalIgnoreCase);
}
_status.Report(ConnectionState.Connecting);
const string lastEndpointKey = "__UNO__" + nameof(RemoteControlClient) + "__last_endpoint";
string? preferred;
try
{
preferred =
ApplicationData.Current.LocalSettings.Values.TryGetValue(lastEndpointKey, out var lastValue) &&
lastValue is string lastEp
? _serverAddresses
.FirstOrDefault(srv => srv.endpoint.Equals(lastEp, StringComparison.OrdinalIgnoreCase))
.endpoint
: default;
}
catch
{
preferred = default;
}
var pending = _serverAddresses
.Select(srv =>
{
if (TryParse(srv.endpoint, srv.port, isHttps, out var serverUri))
{
// Note: If we have a preferred endpoint (last known to be successful), we delay a bit the connection to other endpoints.
// This is to reduce the number of (task cancelled / socket) exceptions at startup by giving a chance to the preferred endpoint to succeed first.
var cts = new CancellationTokenSource();
var delay = preferred is null || preferred.Equals(srv.endpoint, StringComparison.OrdinalIgnoreCase) ? 0 : 3000;
var task = Connect(serverUri, delay, cts.Token);
return (task, srv.endpoint, cts);
}
return default;
})
.Where(c => c.task is not null)
.ToDictionary(c => c.task as Task);
var timeout = Task.Delay(30000);
// Wait for the first connection to succeed
Connection? connection = default;
while (connection is null && pending is { Count: > 0 })
{
var task = await Task.WhenAny([.. pending.Keys, timeout]);
if (task == timeout)
{
if (this.Log().IsEnabled(LogLevel.Error))
{
this.Log().LogError("Failed to connect to the server (timeout).");
}
_status.Report(ConnectionState.ConnectionTimeout);
AbortPending();
return null;
}
var (_, endpoint, _) = pending[task];
// Remove the completed task from the pending list, no matter its completion status
pending.Remove(task);
// If the connection is successful, break the loop
if (task is Task<Connection?> { IsCompleted: true, Result: { Socket: not null } successfulConnection })
{
try
{
ApplicationData.Current.LocalSettings.Values[lastEndpointKey] = endpoint;
}
catch
{
// best effort here
}
connection = successfulConnection;
break;
}
}
// Abort all other pending connections
AbortPending();
_status.ReportActiveConnection(connection);
if (connection is null)
{
if (this.Log().IsEnabled(LogLevel.Error))
{
this.Log().LogError("Failed to connect to the server (all endpoints failed).");
}
return null;
}
else
{
if (this.Log().IsEnabled(LogLevel.Debug))
{
this.Log().LogDebug($"Connected to {connection!.EndPoint}");
}
// Ensure we're processing incoming messages for the connection
connection.EnsureActive();
return connection;
}
void AbortPending()
{
foreach (var connection in pending.Values)
{
connection.cts.Cancel(throwOnFirstException: false);
if (connection is { task.Status: TaskStatus.RanToCompletion, task.Result.Socket: { } socket })
{
_ = socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
}
}
}
}
catch (Exception ex)
{
if (this.Log().IsEnabled(LogLevel.Warning))
{
if (this.Log().IsEnabled(LogLevel.Debug))
{
this.Log().LogDebug($"Failed to connect to the server ({ex})", ex);
}
else
{
this.Log().LogWarning($"The remote control client failed to initialize ({ex.Message}). This generally means that XAML Hot Reload will not be available for this session.");
}
}
return null;
}
}
private bool TryParse(string endpoint, int port, bool isHttps, [NotNullWhen(true)] out Uri? serverUri)
{
try
{
if (Uri.TryCreate(endpoint, UriKind.Absolute, out var fullUri))
{
var wsScheme = fullUri.Scheme switch
{
"http" => "ws",
"https" => "wss",
_ => throw new InvalidOperationException($"Unsupported remote host scheme ({fullUri})"),
};
serverUri = new Uri($"{wsScheme}://{fullUri.Authority}/rc");
}
else if (port == 443)
{
if (OperatingSystem.IsBrowser())
{
if (endpoint.EndsWith("gitpod.io", StringComparison.Ordinal))
{
var originParts = endpoint.Split('-');
var currentHost = WebAssemblyImports.EvalString("window.location.hostname");
var targetParts = currentHost.Split('-');
endpoint = string.Concat(originParts[0].AsSpan(), "-", currentHost.AsSpan().Slice(targetParts[0].Length + 1));
}
}
serverUri = new Uri($"wss://{endpoint}/rc");
}
else if (port is not 0)
{
var scheme = isHttps ? "wss" : "ws";
serverUri = new Uri($"{scheme}://{endpoint}:{port}/rc");
}
else
{
serverUri = default;
return false;
}
return true;
}
catch (Exception e)
{
if (this.Log().IsEnabled(LogLevel.Trace))
{
this.Log().Trace($"Connecting to [{endpoint}:{port}] failed: {e.Message}");
}
serverUri = default;
return false;
}
}
private Task<Connection> Connect(Uri serverUri, CancellationToken ct)
=> Connect(serverUri, 0, ct);
private async Task<Connection> Connect(Uri serverUri, int delay, CancellationToken ct)
{
// Note: This method **MUST NOT** throw any exception as it being used for re-connection
var watch = Stopwatch.StartNew();
try
{
if (delay > 0)
{
// We don't use the CT to make sure to NOT throw an exception here
await Task.Delay(delay, CancellationToken.None);
}
if (ct.IsCancellationRequested)
{
return new(this, serverUri, watch, null);
}
if (this.Log().IsEnabled(LogLevel.Trace))
{
this.Log().Trace($"Connecting to [{serverUri}]");
}
var client = new ClientWebSocket();
await client.ConnectAsync(serverUri, ct);
return new(this, serverUri, watch, client);
}
catch (Exception e)
{
if (this.Log().IsEnabled(LogLevel.Trace))
{
var innerMessage = e.InnerException is { } ie ? $" ({ie.Message})" : "";
this.Log().Trace($"Connecting to [{serverUri}] failed: {e.Message}{innerMessage}");
}
return new(this, serverUri, watch, null);
}
}
private async Task ProcessMessages(WebSocket socket, CancellationToken ct)
{
_ = InitializeServerProcessors();
foreach (var processor in _processors)
{
try
{
await processor.Value.Initialize();
}
catch (Exception error)
{
if (this.Log().IsEnabled(LogLevel.Error))
{
this.Log().LogError($"Failed to initialize processor '{processor}'.", error);
}
}
}
StartKeepAliveTimer();
while (await WebSocketHelper.ReadFrame(socket, ct) is { } frame)
{
try
{
// Central handling for some cross-cutting frames
if (frame is { Scope: WellKnownScopes.HotReload, Name: HotReloadStatusMessage.Name })
{
if (frame.TryGetContent(out HotReloadStatusMessage? hrStatus))
{
// Report any server initialization error to the global client status sink.
// If ServerError is empty/null, this will clear any previously reported fatal server error
// and allow the UI to transition back to a normal state when the server recovers.
_status.ReportHotReloadServerError(string.IsNullOrEmpty(hrStatus.ServerError) ? null : hrStatus.ServerError);
}
}
if (frame.Scope == WellKnownScopes.DevServerChannel)
{
if (frame.Name == KeepAliveMessage.Name)
{
ProcessPong(frame);
}
else if (frame.Name == ProcessorsDiscoveryResponse.Name)
{
await ProcessServerProcessorsDiscovered(frame);
}
}
else
{
if (_processors.TryGetValue(frame.Scope, out var processor))
{
if (this.Log().IsEnabled(LogLevel.Trace))
{
this.Log().Trace($"Received frame [{frame.Scope}/{frame.Name}]");
}
var skipProcessing = false;
foreach (var preProcessor in _preprocessors)
{
try
{
if (await preProcessor.SkipProcessingFrame(frame))
{
skipProcessing = true;
break;
}
}
catch (Exception error)
{
if (this.Log().IsEnabled(LogLevel.Error))
{
this.Log().LogError($"Error while **PRE**processing frame [{frame.Scope}/{frame.Name}] be pre-processor {preProcessor}", error);
}
}
}
if (!skipProcessing)
{
try
{
await processor.ProcessFrame(frame);
}
catch (Exception e)
{
if (this.Log().IsEnabled(LogLevel.Error))
{
this.Log().LogError($"Error while processing frame [{frame.Scope}/{frame.Name}] by processor {processor}", e);
}
}
}
}
else
{
if (this.Log().IsEnabled(LogLevel.Trace))
{
this.Log().Trace($"Unknown Frame scope {frame.Scope}");
}
}
}
try
{
FrameReceived?.Invoke(this, new ReceivedFrameEventArgs(frame));
}
catch (Exception error)
{
if (this.Log().IsEnabled(LogLevel.Error))
{
this.Log().LogError($"Error while notifying frame received {frame.Scope}/{frame.Name}", error);
}
}
}
catch (Exception error)
{
if (this.Log().IsEnabled(LogLevel.Error))
{
this.Log().LogError($"Error while processing frame {frame.Scope}/{frame.Name}", error);
}
}
}
}
private bool _appIdentitySent;
public async Task SendAppIdentityAsync()
{
if (_appIdentitySent)
{
return;
}
try
{
var asm = AppType.Assembly;
var mvid = ApplicationInfoHelper.GetMvid(asm);
var platform = ApplicationInfoHelper.GetTargetPlatform(asm);
var isDebug = Debugger.IsAttached;
await SendMessage(new AppLaunchMessage { Mvid = mvid, Platform = platform, IsDebug = isDebug, Step = AppLaunchStep.Connected });
_appIdentitySent = true;
if (this.Log().IsEnabled(LogLevel.Debug))
{
this.Log().LogDebug($"AppIdentity sent to server (MVID={mvid}, Platform={platform}, Debug={isDebug}).");
}
}
catch (Exception e)
{
if (this.Log().IsEnabled(LogLevel.Trace))
{
this.Log().Trace($"Failed to send AppIdentityMessage: {e.Message}");
}
}
}
private void ProcessPong(Frame frame)
{
if (frame.TryGetContent(out KeepAliveMessage? pong))
{
_status.ReportPong(pong);
if (pong.AssemblyVersion != _ping.AssemblyVersion && this.Log().IsEnabled(LogLevel.Warning))
{
this.Log().Trace(
$"Server pong frame (a.k.a. KeepAlive), but version differs from client (server: {pong.AssemblyVersion} | client: {_ping.AssemblyVersion})."
+ $"This usually indicates that an old instance of the dev-server is being re-used or a partial deployment of the application."
+ "Some feature like hot-reload are most likely to fail. To fix this, you might have to restart Visual Studio.");
}
else if (this.Log().IsEnabled(LogLevel.Trace))
{
this.Log().Trace($"Server pong frame (a.k.a. KeepAlive) with valid version ({pong.AssemblyVersion}).");
}
}
else
{
_status.ReportPong(null);
if (this.Log().IsEnabled(LogLevel.Warning))
{
this.Log().Trace(
"Server pong frame (a.k.a. KeepAlive), but failed to deserialize it's content. "
+ $"This usually indicates a version mismatch between client and server (client: {_ping.AssemblyVersion})."
+ "Some feature like hot-reload are most likely to fail. To fix this, you might have to restart Visual Studio.");
}
}
}
private async Task ProcessServerProcessorsDiscovered(Frame frame)
{
if (frame.TryGetContent(out ProcessorsDiscoveryResponse? response))
{
_status.ReportServerProcessors(response);
if (this.Log().IsEnabled(LogLevel.Debug))
{
this.Log().Debug($"Server loaded processors: \r\n{response.Processors.Select(p => $"\t- {p.Type} v {p.Version} (from {p.AssemblyPath})").JoinBy("\r\n")}.");
}
if (_autoRegisterAppIdentity)
{
await SendAppIdentityAsync();
}
}
}
private void StartKeepAliveTimer()
{
if (_keepAliveTimer is not null)
{
return;
}
Timer? timer = default;
timer = new Timer(async _ =>
{
try
{
if (this.Log().IsEnabled(LogLevel.Trace))
{
this.Log().Trace($"Sending Keepalive frame from client");
}
_ping = _ping.Next();
_status.ReportPing(_ping);
await SendMessage(_ping);
}
catch (Exception error)
{
if (this.Log().IsEnabled(LogLevel.Trace))
{
this.Log().Trace("Keepalive failed");
}
_status.ReportKeepAliveAborted(error);
Interlocked.CompareExchange(ref _keepAliveTimer, null, timer);
timer?.Dispose();
}
});
if (Interlocked.CompareExchange(ref _keepAliveTimer, timer, null) is null)
{
timer.Change(TimeSpan.Zero, _keepAliveInterval);
}
}
private async Task InitializeServerProcessors()
{
var anyDiscoveryRequested = false;
if (_additionalServerProcessorsDiscoveryPath is not null)
{
anyDiscoveryRequested = true;
await SendMessage(new ProcessorsDiscovery(_additionalServerProcessorsDiscoveryPath));
}
if (AppType.Assembly.GetCustomAttributes(typeof(ServerProcessorsConfigurationAttribute), false) is ServerProcessorsConfigurationAttribute[] { Length: > 0 } configs)
{
var config = configs.First();
if (this.Log().IsEnabled(LogLevel.Debug))
{
this.Log().LogDebug($"{nameof(ServerProcessorsConfigurationAttribute)} ProcessorsPath={config.ProcessorsPath}");
}
anyDiscoveryRequested = true;
await SendMessage(new ProcessorsDiscovery(config.ProcessorsPath));
}
else
{
if (this.Log().IsEnabled(LogLevel.Debug))
{
this.Log().LogDebug($"Unable to find any [{nameof(ServerProcessorsConfigurationAttribute)}]");
}
}
// If there is nothing to discover, send the AppIdentity message now.
if (!anyDiscoveryRequested && _autoRegisterAppIdentity)
{
await SendAppIdentityAsync();
}
}
public async Task SendMessage(IMessage message)
{
if (this.Log().IsEnabled(LogLevel.Trace))
{
this.Log().Trace($"Sending message: {message} {message.Name}");
}
var socket = await GetActiveSocket();
if (socket is null)
{
if (this.Log().IsEnabled(LogLevel.Error))
{
this.Log().LogError("Unable send message, no connection available");
}
SendMessageFailed?.Invoke(this, new SendMessageFailedEventArgs(message));
return;
}
await WebSocketHelper.SendFrame(
socket,
HotReload.Messages.Frame.Create(
1,
message.Scope,
message.Name,
message
),
CancellationToken.None);
}
internal void NotifyOfEvent(string eventName, string eventDetails)
{
ClientEvent?.Invoke(this, new ClientEventEventArgs(eventName, eventDetails));
}
public async ValueTask DisposeAsync()
{
var connectionTask = _connection;
_connection = Task.FromResult<Connection?>(null); // Prevent any re-connection
if (await connectionTask is { } connection)
{
await connection.DisposeAsync();
}
foreach (var processor in _processors.Values)
{
try
{
if (processor is IDisposable disposable)
{
disposable.Dispose();
}
else if (processor is IAsyncDisposable asyncDisposable)
{
await asyncDisposable.DisposeAsync();
}
}
catch (Exception error)
{
if (this.Log().IsEnabled(LogLevel.Error))
{
this.Log().LogError($"Failed to dispose processor '{processor}'.", error);
}
}
}
_processors.Clear();
// Stop the keep alive timer
Interlocked.Exchange(ref _keepAliveTimer, null)?.Dispose();
// Remove the instance if it's the current one (should not happen in regular usage)
if (ReferenceEquals(Instance, this))
{
Instance = null;
}
}
}