Skip to content

Commit

Permalink
do check if multicast addr before setting multicast options, have exa…
Browse files Browse the repository at this point in the history
…mples default to unicast UDP case. Fix #3 and is a workaround for #1
  • Loading branch information
todbot committed Dec 14, 2023
1 parent fa21c16 commit e00f1a4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
7 changes: 6 additions & 1 deletion examples/microosc_simplesend.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

import microosc

UDP_HOST = "224.0.0.1" # multicast UDP
UDP_HOST = "" # set to empty string to auto-set unicast UDP
# UDP_HOST = "224.0.0.1" # multicast UDP
UDP_PORT = 5000

ssid = os.getenv("CIRCUITPY_WIFI_SSID")
Expand Down Expand Up @@ -43,6 +44,10 @@ def test_low_level():

test_low_level()

if not UDP_HOST:
# fall back to non-multicast UDP on my IP addr
UDP_HOST = str(wifi.radio.ipv4_address)

osc_client = microosc.OSCClient(socket_pool, "224.0.0.1", 5000)

msg = microosc.OscMsg( "/1/xy1", [0.99, 0.3, ], ("f", "f", ) ) # fmt: skip
Expand Down
7 changes: 6 additions & 1 deletion examples/microosc_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

import microosc

UDP_HOST = "224.0.0.1" # multicast UDP
UDP_HOST = "" # set to empty string to auto-set unicast UDP
# UDP_HOST = "224.0.0.1" # multicast UDP
UDP_PORT = 5000

ssid = os.getenv("CIRCUITPY_WIFI_SSID")
Expand All @@ -39,6 +40,10 @@ def fader_handler(msg):
"/filter1": fader_handler,
}

if not UDP_HOST:
# fall back to non-multicast UDP on my IP addr
UDP_HOST = str(wifi.radio.ipv4_address)

osc_server = microosc.OSCServer(socket_pool, UDP_HOST, UDP_PORT, dispatch_map)

print("MicroOSC server started on ", UDP_HOST, UDP_PORT)
Expand Down
4 changes: 2 additions & 2 deletions microosc.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ def _server_start(self, buf_size=128, timeout=0.001, ttl=2):
self._sock = self._socket_source.socket(
self._socket_source.AF_INET, self._socket_source.SOCK_DGRAM
) # UDP
# TODO: check for IP address type? (multicast/unicast)
self._sock.setsockopt(IPPROTO_IP, IP_MULTICAST_TTL, ttl)
if self.host.startswith("224"): # multicast
self._sock.setsockopt(IPPROTO_IP, IP_MULTICAST_TTL, ttl)
self._sock.bind((self.host, self.port))
self._sock.settimeout(timeout)

Expand Down

0 comments on commit e00f1a4

Please sign in to comment.