forked from unoplatform/uno
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteControlServer.cs
More file actions
85 lines (76 loc) · 1.93 KB
/
Copy pathRemoteControlServer.cs
File metadata and controls
85 lines (76 loc) · 1.93 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Uno.UI.RemoteControl.Host.HotReload;
using Uno.UI.RemoteControl;
using Uno.UI.RemoteControl.Helpers;
using Uno.UI.RemoteControl.HotReload.Messages;
using Microsoft.Extensions.Logging;
using Uno.Extensions;
namespace Uno.UI.RemoteControl.Host
{
internal class RemoteControlServer : IRemoteControlServer, IDisposable
{
private WebSocket _socket;
private readonly Dictionary<string, IServerProcessor> _processors = new Dictionary<string, IServerProcessor>();
public RemoteControlServer()
{
if (this.Log().IsEnabled(LogLevel.Debug))
{
this.Log().LogDebug("Starting RemoteControlServer");
}
RegisterProcessor(new HotReload.ServerHotReloadProcessor(this));
}
private void RegisterProcessor(IServerProcessor hotReloadProcessor)
{
_processors[hotReloadProcessor.Scope] = hotReloadProcessor;
}
public async Task Run(WebSocket socket, CancellationToken ct)
{
_socket = socket;
while (await WebSocketHelper.ReadFrame(socket, ct) is Frame frame)
{
if(_processors.TryGetValue(frame.Scope, out var processor))
{
if (this.Log().IsEnabled(LogLevel.Trace))
{
this.Log().LogTrace($"Received Frame [{frame.Scope} / {frame.Name}]");
}
await processor.ProcessFrame(frame);
}
else
{
if (this.Log().IsEnabled(LogLevel.Trace))
{
this.Log().LogTrace($"Unknown Frame [{frame.Scope} / {frame.Name}]");
}
}
}
}
public async Task SendFrame(IMessage message)
{
await WebSocketHelper.SendFrame(
_socket,
Frame.Create(
1,
message.Scope,
message.Name,
message
),
CancellationToken.None);
}
public void Dispose()
{
foreach(var processor in _processors)
{
processor.Value.Dispose();
}
}
}
}