-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathOSCInPort.m
270 lines (224 loc) · 5.92 KB
/
OSCInPort.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
//
// OSCInPort.m
// PEAM
//
// Created by Chandrasekhar Ramakrishnan on 07.10.04.
// Copyright 2004 __MyCompanyName__. All rights reserved.
//
#import "OSCInPort.h"
#include "OSC-common.h"
#include "NetworkUDP.h"
#include "NetworkReturnAddress.h"
#include "OSC-receive.h"
#include "OSC-priority-queue.h"
#include "OSC-string-help.h"
#include "OSC-drop.h"
#include "OSC-dispatch.h"
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/time.h>
@interface OSCInPort(OSCInPortPrivate)
- (BOOL)createSocket;
- (BOOL)createOSCAddressSpace;
- (BOOL)initializeOSC;
- (void)cleanup;
@end
void* InitTimeMallocForOSC(int numBytes)
{
return malloc(numBytes);
}
// Nothing I do is really that time critical -- just use malloc at realtime
void* RealTimeMallocForOSC(int numBytes)
{
return malloc(numBytes);
}
@implementation OSCInPort
- (void)dealloc
{
// Need to free OSC memory. Perhaps I should keep track
// of OSC's calls to the provided Malloc function?
[self cleanup];
[super dealloc];
}
- (id)initPort:(short)port {
self = [super init];
if (self) {
_port = port;
if (![self createSocket]) {
[self release];
return nil;
}
if (![self createOSCAddressSpace]) {
[self release];
return nil;
}
if (![self initializeOSC]) {
[self release];
return nil;
}
}
return self;
}
- (BOOL)createSocket
{
struct sockaddr_in serv_addr;
// create a UDP socket
_socket = socket(PF_INET, SOCK_DGRAM, 0);
if (_socket < 0)
return NO;
_ownsSocket = YES;
// bind the socket
bzero((char *)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(_port);
if( bind(_socket, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
NSLog(@"Could not bind UDP socket for OSC");
return NO;
}
// set the socket to non-blocking
fcntl(_socket, F_SETFL, FNDELAY);
return YES;
}
- (BOOL)createOSCAddressSpace
{
// Memory tuner parameters
struct OSCAddressSpaceMemoryTuner tun;
tun.initNumContainers = 8;
tun.initNumMethods = 120;
tun.InitTimeMemoryAllocator = InitTimeMallocForOSC;
tun.RealTimeMemoryAllocator = RealTimeMallocForOSC;
// create top level address space
_topLevelContainer = OSCInitAddressSpace(&tun);
OSCInitContainerQueryResponseInfo(&_cqinfo);
OSCInitMethodQueryResponseInfo(&_mqinfo);
return YES;
}
- (BOOL)initializeOSC
{
struct OSCReceiveMemoryTuner rt;
rt.InitTimeMemoryAllocator = InitTimeMallocForOSC;
rt.RealTimeMemoryAllocator = RealTimeMallocForOSC;
rt.receiveBufferSize = 1000;
rt.numReceiveBuffers = 50;
rt.numQueuedObjects = 100;
rt.numCallbackListNodes = 100;
if (!OSCInitReceive(&rt))
{
NSLog(@"Couldn't start OSC");
return NO;
}
return YES;
}
- (void)receivePacket
{
OSCPacketBuffer pb;
struct NetworkReturnAddressStruct *ra;
int maxclilen = sizeof(struct sockaddr_in);
int n;
int capacity = OSCGetReceiveBufferSize();
BOOL morePackets = YES;
char *buf;
while (morePackets)
{
pb = OSCAllocPacketBuffer();
if (!pb)
{
OSCWarning("Out of memory for packet buffers---had to drop a packet!");
return;
}
buf = OSCPacketBufferGetBuffer(pb);
ra = (struct NetworkReturnAddressStruct *) OSCPacketBufferGetClientAddr(pb);
ra->clilen = maxclilen;
ra->sockfd = _socket;
n = recvfrom(_socket, buf, capacity, 0, (struct sockaddr*)&(ra->cl_addr), &(ra->clilen));
if (n > 0)
{
// accept the packet
int * sizep = OSCPacketBufferGetSize(pb);
*sizep = n;
OSCAcceptPacket(pb);
} else {
OSCFreePacket(pb);
morePackets = NO;
}
}
}
- (void)runListeningLoop:(id)argument
{
fd_set read_fds;
int numReadyDescriptors;
struct timeval tv;
struct timeval* timeout;
// to keep Cocoa happy
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
_keepRunning = YES;
while(_keepRunning) {
[self runListenerIteration: &timeout tv: &tv];
// Set up the select file descriptor lists
FD_ZERO(&read_fds); // clear read_fds
FD_SET(_socket, &read_fds); // read from the OSC socket
// Find out which socket has data available
numReadyDescriptors = select(_socket + 1, &read_fds, (fd_set*)NULL, (fd_set*)NULL, timeout);
if (numReadyDescriptors < 0)
{
if (!_keepRunning)
{
// select failed because the socket got closed... which should happen
break;
}
// if select reported an error
NSLog(@"Select failed with error %i", errno);
break;
}
if(FD_ISSET(_socket, &read_fds)) // if there's a message coming in
[self receivePacket]; // accept the packet
}
[pool release];
}
- (void)runListenerIteration:(struct timeval**)timeout tv:(struct timeval*)tv
{
OSCTimeTag currentTime, timeOfNextEvent;
currentTime = OSCTT_CurrentTime();
OSCInvokeAllMessagesThatAreReady(currentTime);
timeOfNextEvent = OSCTimeTagForNextMessage();
if (0 > OSCTT_Compare(timeOfNextEvent, OSCTT_BiggestPossibleTimeTag()))
{
// There's an event in the queue. Timeout the select in time for it
OSCTT_TimeTagIntervalToTimeval(currentTime, timeOfNextEvent, tv);
*timeout = tv;
} else {
// no more events in the queue. Wait until something comes in
*timeout = NULL;
}
}
- (void)start {
[NSThread detachNewThreadSelector: @selector(runListeningLoop:) toTarget: self withObject: nil];
}
- (void)stop
{
_keepRunning = NO;
}
- (void)cleanup
{
if (_ownsSocket)
close(_socket);
_socket = NULL;
}
- (OSCcontainer)topLevelContainer { return _topLevelContainer; }
- (OSCcontainer)newContainerNamed:(char*)name {
return [self newContainerNamed: name under: _topLevelContainer];
}
- (OSCcontainer)newContainerNamed:(char*)name under:(OSCcontainer)container {
return OSCNewContainer(name, container, &_cqinfo);
}
- (OSCMethod)newMethodNamed:(char*)name under:(OSCcontainer)container
callback:(methodCallback)callback context:(void*)context
{
return OSCNewMethod(name, container, callback, context, &_mqinfo);
}
- (void)setOwnsSocket:(BOOL)ownsSocket { _ownsSocket = ownsSocket; }
@end