-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOSCPacket.cs
277 lines (233 loc) · 7.76 KB
/
OSCPacket.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#region licence/info
// OSC.NET - Open Sound Control for .NET
// http://luvtechno.net/
//
// Copyright (c) 2006, Yoshinori Kawasaki
// All rights reserved.
//
// Changes and improvements:
// Copyright (c) 2005-2008 Martin Kaltenbrunner <mkalten@iua.upf.edu>
// As included with
// http://reactivision.sourceforge.net/
//
// Further implementations and specifications:
// Copyright (c) 2013 Marko Ritter <marko@intolight.de>
// As included with https://github.com/vvvv/vvvv-sdk///
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
// * Neither the name of "luvtechno.net" nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
// WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// adapted by François Zajéga - 2014nov11 - <frankie@frankiezafe.org>
// http://frankiezafe.org
#endregion licence/info
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
namespace VVVV_OSC
{
/// <summary>
/// OSCPacket
/// </summary>
abstract public class OSCPacket
{
public static readonly Encoding ASCIIEncoding8Bit;
static OSCPacket()
{
ASCIIEncoding8Bit = Encoding.GetEncoding(1252);
}
public OSCPacket()
{
this.values = new ArrayList();
}
protected static void addBytes(ArrayList data, byte[] bytes)
{
foreach(byte b in bytes)
{
data.Add(b);
}
}
protected static void padNull(ArrayList data)
{
byte zero = 0;
int pad = 4 - (data.Count % 4);
for (int i = 0; i < pad; i++)
{
data.Add(zero);
}
}
internal static byte[] swapEndian(byte[] data)
{
byte[] swapped = new byte[data.Length];
for(int i = data.Length - 1, j = 0 ; i >= 0 ; i--, j++)
{
swapped[j] = data[i];
}
return swapped;
}
protected static byte[] packInt(int value)
{
byte[] data = BitConverter.GetBytes(value);
if(BitConverter.IsLittleEndian) data = swapEndian(data);
return data;
}
protected static byte[] packLong(long value)
{
byte[] data = BitConverter.GetBytes(value);
if(BitConverter.IsLittleEndian) data = swapEndian(data);
return data;
}
protected static byte[] packFloat(float value)
{
byte[] data = BitConverter.GetBytes(value);
if(BitConverter.IsLittleEndian) data = swapEndian(data);
return data;
}
protected static byte[] packDouble(double value)
{
byte[] data = BitConverter.GetBytes(value);
if(BitConverter.IsLittleEndian) data = swapEndian(data);
return data;
}
protected static byte[] packString(string value)
{
return ASCIIEncoding8Bit.GetBytes(value);
}
protected static byte[] packChar(char value)
{
byte[] data = BitConverter.GetBytes(value);
if (BitConverter.IsLittleEndian) data = swapEndian(data);
return data;
}
protected static byte[] packBlob( Stream value )
{
var mem = new MemoryStream();
value.Seek( 0, SeekOrigin.Begin );
Debug.LogError( "packBlob > fix \"value.CopyTo( (Stream) mem\"!" );
// value.CopyTo( (Stream) mem );
byte[] valueData = mem.ToArray();
var lData = new ArrayList();
var length = packInt(valueData.Length);
lData.AddRange(length);
lData.AddRange(valueData);
return (byte[])lData.ToArray(typeof(byte));
}
protected static byte[] packTimeTag(DateTime value)
{
var tag = new OscTimeTag();
tag.Set(value);
return tag.ToByteArray(); ;
}
abstract protected void pack();
protected byte[] binaryData;
public byte[] BinaryData
{
get
{
pack();
return binaryData;
}
}
protected static int unpackInt(byte[] bytes, ref int start)
{
byte[] data = new byte[4];
for(int i = 0 ; i < 4 ; i++, start++) data[i] = bytes[start];
if(BitConverter.IsLittleEndian) data = swapEndian(data);
return BitConverter.ToInt32(data, 0);
}
protected static long unpackLong(byte[] bytes, ref int start)
{
byte[] data = new byte[8];
for(int i = 0 ; i < 8 ; i++, start++) data[i] = bytes[start];
if(BitConverter.IsLittleEndian) data = swapEndian(data);
return BitConverter.ToInt64(data, 0);
}
protected static float unpackFloat(byte[] bytes, ref int start)
{
byte[] data = new byte[4];
for(int i = 0 ; i < 4 ; i++, start++) data[i] = bytes[start];
if(BitConverter.IsLittleEndian) data = swapEndian(data);
return BitConverter.ToSingle(data, 0);
}
protected static double unpackDouble(byte[] bytes, ref int start)
{
byte[] data = new byte[8];
for(int i = 0 ; i < 8 ; i++, start++) data[i] = bytes[start];
if(BitConverter.IsLittleEndian) data = swapEndian(data);
return BitConverter.ToDouble(data, 0);
}
protected static string unpackString(byte[] bytes, ref int start)
{
int count= 0;
for (int index = start; bytes[index] != 0; index++, count++);
string s = ASCIIEncoding8Bit.GetString(bytes, start, count);
start += count+1;
start = (start + 3) / 4 * 4;
return s;
}
protected static char unpackChar(byte[] bytes, ref int start)
{
byte[] data = {bytes[start]};
return BitConverter.ToChar(data, 0);
}
protected static Stream unpackBlob(byte[] bytes, ref int start)
{
int length = unpackInt(bytes, ref start);
byte[] buffer = new byte[length];
Array.Copy(bytes, start, buffer, 0, length);
start += length;
start = (start + 3) / 4 * 4;
return new MemoryStream(buffer);
}
protected static DateTime unpackTimeTag(byte[] bytes, ref int start)
{
byte[] data = new byte[8];
for (int i = 0; i < 8; i++, start++) data[i] = bytes[start];
var tag = new OscTimeTag(data);
return tag.DateTime;
}
public static OSCPacket Unpack(byte[] bytes)
{
int start = 0;
return Unpack(bytes, ref start, bytes.Length);
}
public static OSCPacket Unpack(byte[] bytes, ref int start, int end)
{
if(bytes[start] == '#') {
return OSCBundle.Unpack(bytes, ref start, end);
} else {
return OSCMessage.Unpack(bytes, ref start);
}
}
protected string address;
public string Address
{
get { return address; }
set
{
// TODO: validate
address = value;
}
}
protected ArrayList values;
public ArrayList Values
{
get { return (ArrayList)values.Clone(); }
}
abstract public void Append(object value);
abstract public bool IsBundle();
}
}