comparison Source/TouchKeys/Osc.h @ 6:36fe60d0aadb

Improve OSC input support and enable in the main GUI. External OSC input isn't used yet (aside from internal OSC handling) but this allows a server to run on a specified port.
author Andrew McPherson <andrewm@eecs.qmul.ac.uk>
date Wed, 13 Nov 2013 23:40:53 +0000
parents 3580ffe87dc8
children f943785252fc
comparison
equal deleted inserted replaced
5:0bcbe28a25a2 6:36fe60d0aadb
94 // This class specifically implements OSC messages coming from external sources 94 // This class specifically implements OSC messages coming from external sources
95 95
96 class OscReceiver : public OscMessageSource 96 class OscReceiver : public OscMessageSource
97 { 97 {
98 public: 98 public:
99 OscReceiver(lo_server_thread thread, const char *prefix) { 99 OscReceiver(const int port, const char *prefix) {
100 oscServerThread_ = thread; 100 globalPrefix_.assign(prefix);
101 globalPrefix_.assign(prefix);
102 useThru_ = false; 101 useThru_ = false;
103 lo_server_thread_add_method(thread, NULL, NULL, OscReceiver::staticHandler, (void *)this); 102
104 } 103 // Only start the server if the port is positive
104 if(port > 0) {
105 char portStr[16];
106 snprintf(portStr, 16, "%d", port);
107
108 oscServerThread_ = lo_server_thread_new(portStr, staticErrorHandler);
109 if(oscServerThread_ != 0) {
110 lo_server_thread_add_method(oscServerThread_, NULL, NULL, OscReceiver::staticHandler, (void *)this);
111 lo_server_thread_start(oscServerThread_);
112 }
113 }
114 else
115 oscServerThread_ = 0;
116 }
105 117
106 void setThruAddress(lo_address thruAddr, const char *prefix) { 118 void setThruAddress(lo_address thruAddr, const char *prefix) {
107 thruAddress_ = thruAddr; 119 thruAddress_ = thruAddr;
108 thruPrefix_.assign(prefix); 120 thruPrefix_.assign(prefix);
109 useThru_ = true; 121 useThru_ = true;
110 } 122 }
123
124 // Check whether the server is operating
125 bool running() { return (oscServerThread_ != 0); }
126
127 // Get or set the current port. Setting the port requires restarting the server.
128 // setPort() returns true on success; false if an error occurred (which will leave the server not running).
129 const int port() {
130 if(oscServerThread_ == 0)
131 return 0;
132 return lo_server_get_port(oscServerThread_);
133 }
134 bool setPort(const int port);
111 135
112 // staticHandler() is called by liblo with new OSC messages. Its only function is to pass control 136 // staticHandler() is called by liblo with new OSC messages. Its only function is to pass control
113 // to the object-specific handler method, which has access to all internal variables. 137 // to the object-specific handler method, which has access to all internal variables.
114 138
115 int handler(const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *data); 139 int handler(const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *data);
116 static int staticHandler(const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *userData) { 140 static int staticHandler(const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *userData) {
117 return ((OscReceiver *)userData)->handler(path, types, argv, argc, msg, userData); 141 return ((OscReceiver *)userData)->handler(path, types, argv, argc, msg, userData);
118 } 142 }
143
144 // staticErrorHandler() is called by liblo when an error occurs. For now, ignore errors.
145
146 static void staticErrorHandler(int num, const char *msg, const char *path) {}
119 147
120 ~OscReceiver() { 148 ~OscReceiver() {
121 lo_server_thread_del_method(oscServerThread_, NULL, NULL); 149 if(oscServerThread_ != 0) {
122 } 150 lo_server_thread_del_method(oscServerThread_, NULL, NULL);
151 lo_server_thread_stop(oscServerThread_);
152 lo_server_thread_free(oscServerThread_);
153 }
154 }
123 155
124 private: 156 private:
125 lo_server_thread oscServerThread_; // Thread that handles received OSC messages 157 lo_server_thread oscServerThread_; // Thread that handles received OSC messages
126 158
127 // OSC thru 159 // OSC thru