-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOscListener.cs
49 lines (40 loc) · 1.2 KB
/
OscListener.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
using OscCore;
using System;
using System.Collections.Concurrent;
using System.Net;
using System.Net.Sockets;
public class OscListener : IDisposable {
private UdpClient client;
private ConcurrentQueue<OscPacket> packets;
public OscListener(int port) {
var endpoint = new IPEndPoint(IPAddress.Any, 19190);
client = new(endpoint);
packets = new();
var callback = new AsyncCallback(ReceiveCallback);
client.BeginReceive(callback, null);
}
public bool TryGetPacket(out OscPacket packet) {
return packets.TryDequeue(out packet);
}
public void Dispose() {
client.Dispose();
}
private void ReceiveCallback(IAsyncResult result) {
byte[] data;
IPEndPoint ip = null;
try {
data = client.EndReceive(result, ref ip);
} catch (ObjectDisposedException) {
return;
}
var callback = new AsyncCallback(ReceiveCallback);
client.BeginReceive(callback, null);
try {
var packet = OscPacket.Read(data, 0, data.Length);
packets.Enqueue(packet);
} catch (OscException) {
// TODO: Log
return;
}
}
}