-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisionClient.cs
88 lines (74 loc) · 2.69 KB
/
VisionClient.cs
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 System;
using System.Buffers;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
namespace RobocupSSLController
{
public class VisionClient : IDisposable
{
public static readonly IPEndPoint DefaultSslVisionListenEndpoint = IPEndPoint.Parse("224.5.23.2:10006");
public class Builder
{
public IPEndPoint ListenEndpoint { get; set; } = DefaultSslVisionListenEndpoint;
public int MaxPacketSize { get; set; } = 4096;
public VisionClient Build(CancellationToken cancellationToken = default(CancellationToken))
{
var r = new VisionClient
{
_cancellationTokenSource = new CancellationTokenSource()
};
r._cancellationToken = r._cancellationTokenSource.Token;
r._workerThread = new Thread(() => r.Worker(ListenEndpoint, MaxPacketSize));
r._workerThread.Start();
return r;
}
}
private UdpClient _udpClient;
private Thread _workerThread;
private CancellationTokenSource _cancellationTokenSource;
private CancellationToken _cancellationToken;
private VisionClient()
{
}
private void OnPacket(byte[] buffer, Task<int> task)
{
int gotBytes = task.Result;
var wrapperPacket = SSL_WrapperPacket.Parser.ParseFrom(buffer, 0, gotBytes);
Console.WriteLine(wrapperPacket);
}
private void Worker(IPEndPoint ep, int maxSize)
{
try
{
_udpClient = new UdpClient();
_udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, ep.Port));
_udpClient.JoinMulticastGroup(ep.Address);
var buffer = new byte[maxSize];
while (!_cancellationToken.IsCancellationRequested)
{
var task = _udpClient.Client.ReceiveAsync(buffer, SocketFlags.None, _cancellationToken)
// ReSharper disable once MethodSupportsCancellation
.AsTask().ContinueWith(t => OnPacket(buffer, t));
task.Wait(_cancellationToken);
}
}
catch (OperationCanceledException)
{
}
}
public void Stop()
{
_cancellationTokenSource.Cancel();
_workerThread.Join();
}
public void Dispose()
{
if (_workerThread?.IsAlive ?? false)
Stop();
}
}
}