generated from EIA485/NeosTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VRCFTOSC.cs
167 lines (136 loc) · 4.67 KB
/
VRCFTOSC.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace VRCFTModuleWrapper
{
public class VRCFTOSC
{
private Socket _receiver;
private bool _loop = true;
private readonly Thread _thread;
private readonly int _resolvedPort;
private const int DEFAULT_PORT = 8888;
private const int TIMEOUT_MS = 10_000;
public VRCFTOSC(int? port = null)
{
if (_receiver != null)
{
return;
}
_receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
_resolvedPort = port ?? DEFAULT_PORT;
_receiver.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), _resolvedPort));
_receiver.ReceiveTimeout = TIMEOUT_MS;
_loop = true;
_thread = new Thread(new ThreadStart(ListenLoop));
_thread.Start();
}
private struct Msg
{
public string address;
public float value;
public bool success;
}
// https://github.com/benaclejames/SimpleRustOSC/blob/master/src/lib.rs#L54
private Msg ParseOSC(byte[] buffer, int length)
{
Msg msg = new Msg();
msg.success = false;
if (length < 4)
return msg;
int bufferPosition = 0;
string address = ParseString(buffer, length, ref bufferPosition);
if (address == "")
return msg;
msg.address = address;
// checking for ',' char
if (buffer[bufferPosition] != 44)
return msg;
bufferPosition++; // skipping ',' character
char valueType = (char)buffer[bufferPosition]; // unused
bufferPosition++;
float value = ParesFloat(buffer, length, bufferPosition);
msg.value = value;
msg.success = true;
return msg;
}
private string ParseString(byte[] buffer, int length, ref int bufferPosition)
{
string address = "";
// first character must be '/'
if (buffer[0] != 47)
return address;
for (int i = 0; i < length; i++)
{
if (buffer[i] == 0)
{
bufferPosition = i + 1;
if (bufferPosition % 4 != 0)
{
bufferPosition += 4 - (bufferPosition % 4);
}
break;
}
address += (char)buffer[i];
}
return address;
}
private float ParesFloat(byte[] buffer, int length, int bufferPosition)
{
var valueBuffer = new byte[length - bufferPosition];
int j = 0;
for (int i = bufferPosition; i < length; i++)
{
valueBuffer[j] = buffer[i];
j++;
}
float value = bytesToFLoat(valueBuffer);
return value;
}
private float bytesToFLoat(byte[] bytes)
{
if (BitConverter.IsLittleEndian)
{
Array.Reverse(bytes); // Convert big endian to little endian
}
float myFloat = BitConverter.ToSingle(bytes, 0);
return myFloat;
}
private void ListenLoop()
{
var buffer = new byte[4096];
while (_loop)
{
try
{
if (_receiver.IsBound)
{
var length = _receiver.Receive(buffer);
Msg msg = ParseOSC(buffer, length);
if (msg.success && Expressions.VRCFTExpressionMap.ContainsKey2(msg.address))
{
Expressions.VRCFTExpressionMap.SetByKey2(msg.address, msg.value);
}
}
else
{
_receiver.Close();
_receiver.Dispose();
_receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
_receiver.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), _resolvedPort));
_receiver.ReceiveTimeout = TIMEOUT_MS;
}
}
catch (Exception) { }
}
}
public void Teardown()
{
_loop = false;
_receiver.Close();
_receiver.Dispose();
_thread.Join();
}
}
}