-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOSCHandler.sc
66 lines (53 loc) · 1.39 KB
/
OSCHandler.sc
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
/* OSCHandler.sc
* Author: Marc J. Szalkiewicz
* Date: 2014 Jan 25
*/
OSCHandler {
var <>server, <>client, <oscResponders;
*new { arg argServer;
^super.new.init(argServer);
}
init { arg argServer;
server = argServer;
client = this.makeClient;
oscResponders = Set.new;
}
empty {
oscResponders.do({ |r, i| r.disable; r.clear; r.free; });
oscResponders.makeEmpty;
}
close {
this.empty;
server.disconnect;
client.disconnect;
}
makeClient {
var udpOK = false;
var udptries = 0;
var clientPort = -1;
while({ (udpOK == false) && (udptries < 10) }, {
clientPort = rrand(1025, 65535);
udpOK = thisProcess.openUDPPort(clientPort);
udptries = udptries + 1;
});
if(udpOK == false, { clientPort = NetAddr.langPort } );
^NetAddr.new(NetAddr.localAddr.hostname.asString, clientPort);
}
addResponders { |argArray|
/* addAll can return a new collection, so assign the result as well. */
oscResponders = oscResponders.addAll(argArray);
}
recv {
this.subclassResponsibility(thisMethod);
}
send { |msg|
/* Hack for passing arrays to this wrapper function is: sendRaw(msg.asRawOSC). */
/* Here we use 'collection as arguments to f' notation. */
server.sendMsg(*msg);
}
printOn { arg stream;
stream << "OSCHandler(server: " << server.asString
<< ", client: " << client.asString
<< ", r: " << oscResponders.asString << ")";
}
}