From 7d9764e799ad17a9d1921554bbe3087a0bf8445e Mon Sep 17 00:00:00 2001 From: Simon-L Date: Sat, 30 Sep 2023 13:54:04 +0200 Subject: [PATCH 1/2] WIP: Add non-blocking mode --- examples/server-nonblocking.lua | 40 +++++++++++++++++++++++++++++++++ src/losc/init.lua | 7 ++++++ src/losc/plugins/udp-socket.lua | 19 ++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 examples/server-nonblocking.lua diff --git a/examples/server-nonblocking.lua b/examples/server-nonblocking.lua new file mode 100644 index 0000000..a861f66 --- /dev/null +++ b/examples/server-nonblocking.lua @@ -0,0 +1,40 @@ +-- REMOVE NEXT LINE BEFORE MERGING +package.path = ';./src/?/init.lua;./src/?.lua;/usr/local/share/lua/5.1/?.lua' + +local losc = require'losc' +local plugin = require'losc.plugins.udp-socket' + +local udp = plugin.new { + recvAddr = 'localhost', + recvPort = 9000, + non_blocking = true, -- do not block on :open(), :poll() handles processing + ignore_late = true, -- ignore late bundles +} +local osc = losc.new {plugin = udp} + +local function print_data(data) + local msg = data.message + print('address: ' .. msg.address, 'timestamp: ' .. data.timestamp) + for index, argument in ipairs(msg) do + print('index: ' .. index, 'arg: ' .. argument) + end +end + +osc:add_handler('/test', function(data) + print_data(data) +end) + +osc:add_handler('/param/{x,y,z}', function(data) + print_data(data) +end) + +osc:open() -- non blocking call :) + +local i = 0 + +while true do + print("Loop iteration " .. i) + require'socket'.select(nil, nil, 2) -- equivalent for sleep() to simulate other tasks + osc:poll() + i = i + 1 +end diff --git a/src/losc/init.lua b/src/losc/init.lua index bd77ec5..2726a5b 100644 --- a/src/losc/init.lua +++ b/src/losc/init.lua @@ -158,6 +158,13 @@ function losc:open(...) return pcall(self.plugin.open, self.plugin, ...) end +function losc:poll(...) + if not self.plugin then + error('"poll" must be implemented using a plugin.') + end + return pcall(self.plugin.poll, self.plugin, ...) +end + --- Closes an OSC server. -- @param[opt] ... Plugin specific arguments. -- @return status, nil or error diff --git a/src/losc/plugins/udp-socket.lua b/src/losc/plugins/udp-socket.lua index bd9219a..66338ec 100644 --- a/src/losc/plugins/udp-socket.lua +++ b/src/losc/plugins/udp-socket.lua @@ -95,6 +95,9 @@ function M:open(host, port) host = socket.dns.toip(host) port = port or self.options.recvPort self.handle:setsockname(host, port) + if self.options.non_blocking == true then + return + end while true do local data = self.handle:receive() if data then @@ -106,6 +109,22 @@ function M:open(host, port) end end +function M:poll(timeout) + if self.options.non_blocking ~= true then + return + end + self.handle:settimeout((timeout == nil and 0.1) or timeout) + local data = self.handle:receive() + if data == nil then return false end + if data then + local ok, err = pcall(Pattern.dispatch, data, self) + if not ok then + print(err) + end + end + return true +end + --- Close UDP server. function M:close() if self.handle then From c358c29054bdebbdb9f965152db78850ceba9500 Mon Sep 17 00:00:00 2001 From: FergusL Date: Fri, 19 Apr 2024 01:39:45 +0200 Subject: [PATCH 2/2] Update rockspec url --- rockspecs/losc-scm-0.rockspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rockspecs/losc-scm-0.rockspec b/rockspecs/losc-scm-0.rockspec index b884b7c..b6b1d72 100644 --- a/rockspecs/losc-scm-0.rockspec +++ b/rockspecs/losc-scm-0.rockspec @@ -1,7 +1,7 @@ package = "losc" version = "scm-0" source = { - url = "git+https://github.com/davidgranstrom/losc.git" + url = "git+https://github.com/Simon-L/losc.git" } description = { summary = "OSC 1.0 library.",