forked from Figure53/F53OSC
-
Notifications
You must be signed in to change notification settings - Fork 7
/
F53OSCParser.m
executable file
·342 lines (303 loc) · 13.6 KB
/
F53OSCParser.m
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//
// F53OSCParser.m
//
// Created by Christopher Ashworth on 1/30/13.
//
// Copyright (c) 2013 Figure 53 LLC, http://figure53.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "F53OSCParser.h"
#import "F53OSCMessage.h"
#import "F53OSCSocket.h"
#import "F53OSCFoundationAdditions.h"
#define END 0300 /* indicates end of packet */
#define ESC 0333 /* indicates byte stuffing */
#define ESC_END 0334 /* ESC ESC_END means END data byte */
#define ESC_ESC 0335 /* ESC ESC_ESC means ESC data byte */
@interface F53OSCParser (Private)
+ (void) processMessageData:(NSData *)data forDestination:(id <F53OSCPacketDestination>)destination replyToSocket:(F53OSCSocket *)socket;
+ (void) processBundleData:(NSData *)data forDestination:(id <F53OSCPacketDestination>)destination replyToSocket:(F53OSCSocket *)socket;
@end
@implementation F53OSCParser (private)
+ (void) processMessageData:(NSData *)data forDestination:(id <F53OSCPacketDestination>)destination replyToSocket:(F53OSCSocket *)socket;
{
NSUInteger length = [data length];
const char *buffer = [data bytes];
NSUInteger lengthOfRemainingBuffer = length;
NSUInteger dataLength = 0;
NSString *addressPattern = [NSString stringWithOSCStringBytes:buffer maxLength:lengthOfRemainingBuffer length:&dataLength];
if ( addressPattern == nil || dataLength == 0 || dataLength > length )
{
NSLog( @"Error: Unable to parse OSC method address." );
return;
}
buffer += dataLength;
lengthOfRemainingBuffer -= dataLength;
NSMutableArray *args = [NSMutableArray array];
BOOL hasArguments = (lengthOfRemainingBuffer > 0);
if ( hasArguments && buffer[0] == ',' )
{
NSString *typeTag = [NSString stringWithOSCStringBytes:buffer maxLength:lengthOfRemainingBuffer length:&dataLength];
if ( typeTag == nil )
{
NSLog( @"Error: Unable to parse type tag for OSC method %@", addressPattern );
return;
}
buffer += dataLength;
lengthOfRemainingBuffer -= dataLength;
if ( [[NSUserDefaults standardUserDefaults] boolForKey:@"debugIncomingOSC"] )
{
NSLog( @"Incoming OSC message:" );
NSLog( @" %@", addressPattern );
}
NSInteger numArgs = [typeTag length] - 1;
if ( numArgs > 0 )
{
if ( [[NSUserDefaults standardUserDefaults] boolForKey:@"debugIncomingOSC"] )
NSLog( @" arguments:" );
for ( int i = 1; i < numArgs + 1; i++ )
{
NSString *stringArg = nil;
NSData *dataArg = nil;
NSNumber *numberArg = nil;
char type = [typeTag characterAtIndex:i]; // (index starts at 1 because first char is ",")
switch ( type )
{
case 's':
stringArg = [NSString stringWithOSCStringBytes:buffer maxLength:lengthOfRemainingBuffer length:&dataLength];
if ( stringArg )
{
[args addObject:stringArg];
buffer += dataLength;
lengthOfRemainingBuffer -= dataLength;
if ( [[NSUserDefaults standardUserDefaults] boolForKey:@"debugIncomingOSC"] )
NSLog( @" string: \"%@\"", stringArg );
}
else
{
NSLog( @"Error: Unable to parse string argument for OSC method %@", addressPattern );
return;
}
break;
case 'b':
dataArg = [NSData dataWithOSCBlobBytes:buffer maxLength:lengthOfRemainingBuffer length:&dataLength];
if ( dataArg )
{
[args addObject:dataArg];
buffer += dataLength + 4;
lengthOfRemainingBuffer -= (dataLength + 4);
if ( [[NSUserDefaults standardUserDefaults] boolForKey:@"debugIncomingOSC"] )
NSLog( @" blob: %@", dataArg );
}
else
{
NSLog( @"Error: Unable to parse blob argument for OSC method %@", addressPattern );
return;
}
break;
case 'i':
numberArg = [NSNumber numberWithOSCIntBytes:buffer maxLength:lengthOfRemainingBuffer];
if ( numberArg )
{
[args addObject:numberArg];
buffer += 4;
lengthOfRemainingBuffer -= 4;
if ( [[NSUserDefaults standardUserDefaults] boolForKey:@"debugIncomingOSC"] )
NSLog( @" int: %@", numberArg );
}
else
{
NSLog( @"Error: Unable to parse int argument for OSC method %@", addressPattern );
return;
}
break;
case 'f':
numberArg = [NSNumber numberWithOSCFloatBytes:buffer maxLength:lengthOfRemainingBuffer];
if ( numberArg )
{
[args addObject:numberArg];
buffer += 4;
lengthOfRemainingBuffer -= 4;
if ( [[NSUserDefaults standardUserDefaults] boolForKey:@"debugIncomingOSC"] )
NSLog( @" float: %@", numberArg );
}
else
{
NSLog( @"Error: Unable to parse float argument for OSC method %@", addressPattern );
return;
}
break;
default:
NSLog( @"Error: Unrecognized type '%c' found in type tag for OSC method %@", type, addressPattern );
return;
}
}
}
}
[destination takeMessage:[F53OSCMessage messageWithAddressPattern:addressPattern arguments:args replySocket:socket]];
}
+ (void) processBundleData:(NSData *)data forDestination:(id <F53OSCPacketDestination>)destination replyToSocket:(F53OSCSocket *)socket;
{
NSUInteger length = [data length];
const char *buffer = [data bytes];
NSUInteger lengthOfRemainingBuffer = length;
NSUInteger dataLength = 0;
NSString *bundlePrefix = [NSString stringWithOSCStringBytes:buffer maxLength:lengthOfRemainingBuffer length:&dataLength];
if ( bundlePrefix == nil || dataLength == 0 || dataLength > length )
{
NSLog( @"Error: Unable to parse OSC bundle prefix." );
return;
}
if ( [bundlePrefix isEqualToString:@"#bundle"] )
{
buffer += dataLength;
lengthOfRemainingBuffer -= dataLength;
if ( lengthOfRemainingBuffer > 8 )
{
//F53OSCTimeTag *timetag = [F53OSCTimeTag timeTagWithOSCTimeBytes:buffer];
buffer += 8; // We're not currently using the time tag so we just skip it.
lengthOfRemainingBuffer -= 8;
while ( lengthOfRemainingBuffer > sizeof( UInt32 ) )
{
UInt32 elementLength = *((UInt32 *)buffer);
elementLength = OSSwapBigToHostInt32( elementLength );
buffer += sizeof( UInt32 );
lengthOfRemainingBuffer -= sizeof( UInt32 );
if ( elementLength > lengthOfRemainingBuffer )
{
NSLog( @"Error: A message in the OSC bundle claimed to be larger than the bundle itself." );
return;
}
if ( buffer[0] == '/' ) // OSC message
{
[self processMessageData:[NSData dataWithBytesNoCopy:(void *)buffer length:elementLength freeWhenDone:NO]
forDestination:destination
replyToSocket:socket];
}
else if ( buffer[0] == '#' ) // OSC bundle
{
[self processBundleData:[NSData dataWithBytesNoCopy:(void *)buffer length:elementLength freeWhenDone:NO]
forDestination:destination
replyToSocket:socket];
}
else
{
NSLog( @"Error: Bundle contained unrecognized OSC message of length %u.", (unsigned int)elementLength );
return;
}
buffer += elementLength;
lengthOfRemainingBuffer -= elementLength;
}
}
else
{
NSLog( @"Warning: Received an empty OSC bundle message." );
}
}
else
{
NSLog( @"Error: Received an invalid OSC bundle message." );
}
}
@end
@implementation F53OSCParser
+ (void) processOscData:(NSData *)data forDestination:(id <F53OSCPacketDestination, NSObject>)destination replyToSocket:(F53OSCSocket *)socket
{
if ( data == nil || destination == nil )
return;
NSUInteger length = [data length];
if ( length == 0 )
return;
const char *buffer = [data bytes];
if ( buffer[0] == '/' ) // OSC message
{
[self processMessageData:data forDestination:destination replyToSocket:socket];
}
else if ( buffer[0] == '#' ) // OSC bundle
{
[self processBundleData:data forDestination:destination replyToSocket:socket];
}
else
{
NSLog( @"Error: Unrecognized OSC message of length %lu.", (unsigned long)length );
}
}
+ (void) translateSlipData:(NSData *)slipData
toData:(NSMutableData *)data
withState:(NSMutableDictionary *)state
destination:(id <F53OSCPacketDestination>)destination
{
// Incoming OSC messages are framed using the SLIP protocol: http://www.rfc-editor.org/rfc/rfc1055.txt
F53OSCSocket *socket = [state objectForKey:@"socket"];
if ( socket == nil )
{
NSLog( @"Error: F53OSCParser can not translate SLIP data without a socket." );
return;
}
BOOL dangling_ESC = [[state objectForKey:@"dangling_ESC"] boolValue];
Byte end[1] = {END};
Byte esc[1] = {ESC};
NSUInteger length = [slipData length];
const Byte *buffer = [slipData bytes];
for ( NSUInteger index = 0; index < length; index++ )
{
if ( dangling_ESC )
{
dangling_ESC = NO;
[state setObject:@NO forKey:@"dangling_ESC"];
if ( buffer[index] == ESC_END )
[data appendBytes:end length:1];
else if ( buffer[index] == ESC_ESC )
[data appendBytes:esc length:1];
else // Protocol violation. Pass the byte along and hope for the best.
[data appendBytes:&(buffer[index]) length:1];
}
else if ( buffer[index] == END )
{
// The data is now a complete message.
//NSLog( @"socket %p dispatching OSC data of length %lu", sock, [data length] );
[F53OSCParser processOscData:[NSData dataWithData:data] forDestination:destination replyToSocket:socket];
[data setData:[NSData data]];
}
else if ( buffer[index] == ESC )
{
if ( index + 1 < length )
{
index++;
if ( buffer[index] == ESC_END )
[data appendBytes:end length:1];
else if ( buffer[index] == ESC_ESC )
[data appendBytes:esc length:1];
else // Protocol violation. Pass the byte along and hope for the best.
[data appendBytes:&(buffer[index]) length:1];
}
else
{
// The incoming raw data stopped in the middle of an escape sequence.
[state setObject:@YES forKey:@"dangling_ESC"];
}
}
else
{
[data appendBytes:&(buffer[index]) length:1];
}
}
}
@end