-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOsc.cs
More file actions
171 lines (148 loc) · 4.76 KB
/
Copy pathOsc.cs
File metadata and controls
171 lines (148 loc) · 4.76 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
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
168
169
170
171
// Osc.cs - A minimal OSC receiver implementation for Unity.
// https://github.com/keijiro/unity-osc
using System;
//ネームスペースの定義
namespace Osc
{
using MessageQueue = System.Collections.Generic.Queue<Message>;
public struct Message
{
public string path;
public object[] data;
public override string ToString ()
{
var temp = path + ":";
foreach (var o in data) {
temp += o + ":";
}
return temp;
}
}
public class Parser
{
#region General private members
MessageQueue messageBuffer;
#endregion
#region Temporary read buffer
Byte[] readBuffer;
int readPoint;
#endregion
#region Public members
public int MessageCount {
get { return messageBuffer.Count; }
}
public Parser ()
{
messageBuffer = new MessageQueue ();
}
public Message PopMessage ()
{
return messageBuffer.Dequeue ();
}
public void FeedData (Byte[] data)
{
readBuffer = data;
readPoint = 0;
ReadMessage ();
readBuffer = null;
}
#endregion
#region Private methods
void ReadMessage ()
{
var path = ReadString ();
if (path == "#bundle") {
ReadInt64 ();
while (true) {
if (readPoint >= readBuffer.Length) {
return;
}
var peek = readBuffer [readPoint];
if (peek == '/' || peek == '#') {
ReadMessage ();
return;
}
var bundleEnd = readPoint + ReadInt32 ();
while (readPoint < bundleEnd) {
ReadMessage ();
}
}
}
var temp = new Message ();
temp.path = path;
var types = ReadString ();
temp.data = new object[types.Length - 1];
for (var i = 0; i < types.Length - 1; i++) {
switch (types [i + 1]) {
case 'f':
temp.data [i] = ReadFloat32 ();
break;
case 'i':
temp.data [i] = ReadInt32 ();
break;
case 's':
temp.data [i] = ReadString ();
break;
case 'b':
temp.data [i] = ReadBlob ();
break;
}
}
messageBuffer.Enqueue (temp);
}
float ReadFloat32 ()
{
Byte[] temp = {
readBuffer [readPoint + 3],
readBuffer [readPoint + 2],
readBuffer [readPoint + 1],
readBuffer [readPoint]
};
readPoint += 4;
return BitConverter.ToSingle (temp, 0);
}
int ReadInt32 ()
{
int temp =
(readBuffer [readPoint + 0] << 24) +
(readBuffer [readPoint + 1] << 16) +
(readBuffer [readPoint + 2] << 8) +
(readBuffer [readPoint + 3]);
readPoint += 4;
return temp;
}
long ReadInt64 ()
{
long temp =
((long)readBuffer [readPoint + 0] << 56) +
((long)readBuffer [readPoint + 1] << 48) +
((long)readBuffer [readPoint + 2] << 40) +
((long)readBuffer [readPoint + 3] << 32) +
((long)readBuffer [readPoint + 4] << 24) +
((long)readBuffer [readPoint + 5] << 16) +
((long)readBuffer [readPoint + 6] << 8) +
((long)readBuffer [readPoint + 7]);
readPoint += 8;
return temp;
}
string ReadString ()
{
var offset = 0;
while (readBuffer[readPoint + offset] != 0) {
offset++;
}
var s = System.Text.Encoding.UTF8.GetString (readBuffer, readPoint, offset);
readPoint += (offset + 4) & ~3;
return s;
}
Byte[] ReadBlob ()
{
var length = ReadInt32 ();
var temp = new Byte[length];
Array.Copy (readBuffer, readPoint, temp, 0, length);
readPoint += (length + 3) & ~3;
return temp;
}
#endregion
}
}