-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathVmixTcpSubscriber.cs
More file actions
91 lines (79 loc) · 3.05 KB
/
Copy pathVmixTcpSubscriber.cs
File metadata and controls
91 lines (79 loc) · 3.05 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
using System;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
namespace vMixController.Classes
{
internal sealed class VmixTcpSubscriber : IDisposable
{
public event EventHandler ActsReceived;
private CancellationTokenSource _cts;
private Task _loopTask;
private Timer _debounce;
private const int TcpPort = 8099;
private const int DebounceMs = 300;
public void Start(string host)
{
Stop();
_cts = new CancellationTokenSource();
var token = _cts.Token;
_loopTask = Task.Run(() => LoopAsync(host, token));
}
public void Stop()
{
_cts?.Cancel();
_loopTask = null;
_cts?.Dispose();
_cts = null;
}
private async Task LoopAsync(string host, CancellationToken ct)
{
while (!ct.IsCancellationRequested)
{
try
{
using (var client = new TcpClient())
{
client.ReceiveTimeout = 2000;
await client.ConnectAsync(host, TcpPort).ConfigureAwait(false);
using (var stream = client.GetStream())
using (var writer = new StreamWriter(stream) { AutoFlush = true, NewLine = "\r\n" })
using (var reader = new StreamReader(stream))
{
await writer.WriteLineAsync("SUBSCRIBE ACTS").ConfigureAwait(false);
while (!ct.IsCancellationRequested)
{
string line;
try
{
line = await reader.ReadLineAsync().ConfigureAwait(false);
}
catch (IOException) { break; }
if (line == null) break;
// "SUBSCRIBE OK ACTS" is the subscription confirmation — skip it
// actual events arrive as "ACTS OK <function> ..."
if (line.StartsWith("ACTS ", StringComparison.Ordinal))
FireDebounced();
}
}
}
}
catch (OperationCanceledException) { break; }
catch { /* connection failed — retry after delay */ }
try { await Task.Delay(3000, ct).ConfigureAwait(false); }
catch (OperationCanceledException) { break; }
}
}
private void FireDebounced()
{
_debounce?.Dispose();
_debounce = new Timer(_ => ActsReceived?.Invoke(this, EventArgs.Empty), null, DebounceMs, Timeout.Infinite);
}
public void Dispose()
{
Stop();
_debounce?.Dispose();
}
}
}