Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions examples/server-nonblocking.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
-- REMOVE NEXT LINE BEFORE MERGING
package.path = ';./src/?/init.lua;./src/?.lua;/usr/local/share/lua/5.1/?.lua'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you intended on removing this before creating the PR?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will remove it in an upcoming commit, like the comment and the PR says this is just a quick draft I put together for @davidgranstrom and you to review, thanks
But I guess eveyone installs with luarocks makeand this was not even needed in the first place!


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
2 changes: 1 addition & 1 deletion rockspecs/losc-scm-0.rockspec
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down
7 changes: 7 additions & 0 deletions src/losc/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ function losc:open(...)
return pcall(self.plugin.open, self.plugin, ...)
end

function losc:poll(...)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you beat me to the PR!
I would add some documentation here:

--- Polls an OSC server.
-- Used by non-blocking plugins.
-- @param[opt] ... Plugin specific arguments.
-- @return bool representing if the plugin dispatched a message
-- @usage losc:poll()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks I'll add it!
I should maybe link to the PIL page that has an example of this as well: https://www.lua.org/pil/9.4.html
It's about coroutines but the purpose of the :timeout() method is clearly shown

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
Expand Down
19 changes: 19 additions & 0 deletions src/losc/plugins/udp-socket.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down