serverRoutine.lua 862 B

1234567891011121314151617181920212223242526272829303132
  1. local osc = require("osc")
  2. local socket = require("socket")
  3. local routing = require("routing")
  4. return function(host, port, sendPorts)
  5. -- server setup
  6. print("Binding to host '" .. host .. "' and port " .. port .. "...")
  7. local udp = assert(socket.udp())
  8. assert(udp:setsockname(host, port))
  9. assert(udp:settimeout(.01))
  10. local ip, boundPort = udp:getsockname()
  11. assert(ip, boundPort)
  12. print("Waiting packets on " .. ip .. ":" .. port .. "...")
  13. coroutine.yield()
  14. -- server loop
  15. while true do
  16. local dgram = udp:receivefrom()
  17. if dgram then
  18. local incoming = osc.unpack(dgram)
  19. local outgoings = routing.createRoutedMessages(incoming)
  20. for _, out in pairs(outgoings) do
  21. for _, sp in pairs(sendPorts) do
  22. udp:sendto(osc.pack(out),"127.0.0.1",sp)
  23. end
  24. end
  25. end
  26. coroutine.yield()
  27. end
  28. end