-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBluetoothControlFrameProcessor.cs
More file actions
88 lines (75 loc) · 3.43 KB
/
Copy pathBluetoothControlFrameProcessor.cs
File metadata and controls
88 lines (75 loc) · 3.43 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
using SwitchifyPc.Core.Control;
using SwitchifyPc.Protocol;
namespace SwitchifyPc.Core.Bluetooth;
public sealed record BluetoothControlFrameResult(
bool MessageComplete,
string? ErrorReason,
IReadOnlyList<BluetoothFrame> ResponseFrames)
{
public static BluetoothControlFrameResult Incomplete(string? reason = null) => new(false, reason, []);
public static BluetoothControlFrameResult Complete(IReadOnlyList<BluetoothFrame> responseFrames) => new(true, null, responseFrames);
public static BluetoothControlFrameResult Error(string reason) => new(true, reason, []);
}
public sealed class BluetoothControlFrameProcessor
{
private readonly ControlSession controlSession;
private readonly int maxResponseFramePayloadBytes;
private readonly int maxMessageBytes;
private readonly double partialTimeoutMs;
private readonly Func<double> now;
private readonly Dictionary<string, BluetoothFrameReassembler> reassemblers = new(StringComparer.Ordinal);
public BluetoothControlFrameProcessor(
ControlSession controlSession,
int maxResponseFramePayloadBytes = BluetoothFrameCodec.DefaultBluetoothFramePayloadBytes,
int maxMessageBytes = BluetoothFrameCodec.DefaultBluetoothMaxMessageBytes,
double partialTimeoutMs = BluetoothFrameCodec.DefaultBluetoothPartialTimeoutMs,
Func<double>? now = null)
{
this.controlSession = controlSession;
this.maxResponseFramePayloadBytes = maxResponseFramePayloadBytes;
this.maxMessageBytes = maxMessageBytes;
this.partialTimeoutMs = partialTimeoutMs;
this.now = now ?? (() => DateTimeOffset.UtcNow.ToUnixTimeMilliseconds());
}
public async Task<BluetoothControlFrameResult> AcceptAsync(
string connectionId,
BluetoothFrame frame,
CancellationToken cancellationToken = default)
{
BluetoothFrameReassembler reassembler = ReassemblerFor(connectionId);
BluetoothFrameReassemblyResult reassembly = reassembler.Accept(frame);
if (!reassembly.Ok)
{
return reassembly.Reason == "incomplete"
? BluetoothControlFrameResult.Incomplete(reassembly.Reason)
: BluetoothControlFrameResult.Error(reassembly.Reason ?? "invalid_frame");
}
ControlSessionResult sessionResult = await controlSession.ProcessMessageAsync(reassembly.Message ?? "", cancellationToken).ConfigureAwait(false);
if (!sessionResult.HasResponse)
{
return BluetoothControlFrameResult.Complete([]);
}
IReadOnlyList<BluetoothFrame> responseFrames = BluetoothFrameCodec.CreateFrames(
sessionResult.ResponseJson!,
maxPayloadBytes: maxResponseFramePayloadBytes,
maxMessageBytes: maxMessageBytes);
return BluetoothControlFrameResult.Complete(responseFrames);
}
public void RemoveConnection(string connectionId)
{
reassemblers.Remove(connectionId);
}
public int ClearExpired()
{
return reassemblers.Values.Sum(reassembler => reassembler.ClearExpired());
}
private BluetoothFrameReassembler ReassemblerFor(string connectionId)
{
if (!reassemblers.TryGetValue(connectionId, out BluetoothFrameReassembler? reassembler))
{
reassembler = new BluetoothFrameReassembler(maxMessageBytes, partialTimeoutMs, now);
reassemblers[connectionId] = reassembler;
}
return reassembler;
}
}