Client improvements

* Added broadcast support
* Added _DEBUG switch to switch logs state
This commit is contained in:
2025-11-10 08:04:59 -05:00
parent 7807c6b2ad
commit 4cfe79f474

View File

@@ -3,6 +3,7 @@
import asyncio import asyncio
import struct import struct
_DEBUG = True
_TASK = None _TASK = None
_Q = asyncio.Queue() _Q = asyncio.Queue()
@@ -16,7 +17,8 @@ def __string_to_osc(s: str) -> bytes:
async def __serve() -> None: async def __serve() -> None:
''' Task that manages OSC sending ''' ''' Task that manages OSC sending '''
# log # log
print('[I] OSC: task is running') if _DEBUG:
print('[I] OSC Client: task is running')
# task loop # task loop
while True: while True:
# get value from queue # get value from queue
@@ -45,12 +47,13 @@ async def __serve() -> None:
osc_data += __string_to_osc(d) osc_data += __string_to_osc(d)
# unsupported # unsupported
else: else:
print('[!] OSC: unsupported data type was provided! The packet is discarded.') if _DEBUG:
print(' * host: %s' % host) print('[!] OSC Client: unsupported data type was provided! The packet is discarded.')
print(' * port: %s' % port) print(' * host: %s' % host)
print(' * address: %s' % addr) print(' * port: %s' % port)
print(' * data: %s' % data) print(' * address: %s' % addr)
print(' (bad type is %s)' % d_type) print(' * data: %s' % data)
print(' (bad type is %s)' % d_type)
type_tag = None type_tag = None
break break
# no type tag # no type tag
@@ -64,20 +67,23 @@ async def __serve() -> None:
# send the packet # send the packet
trans, prot = await asyncio.get_running_loop().create_datagram_endpoint( trans, prot = await asyncio.get_running_loop().create_datagram_endpoint(
asyncio.DatagramProtocol, asyncio.DatagramProtocol,
remote_addr=(host, port) remote_addr=(host, port),
allow_broadcast=True
) )
try: try:
trans.sendto(packet) trans.sendto(packet)
print('[I] OSC: sent data to %s:%s' % (host, port)) if _DEBUG: print('[I] OSC Client: sent data to %s:%s' % (host, port))
except: except:
print('[!] OSC: failed to send to %s:%s!' % (host, port)) if _DEBUG:
print(' * address: %s' % addr) print('[!] OSC Client: failed to send to %s:%s!' % (host, port))
print(' * data: %s' % data) print(' * address: %s' % addr)
print(' * data: %s' % data)
finally: finally:
trans.close() trans.close()
# log # log
print('[I] OSC: task is stopped') if _DEBUG:
print('[I] OSC Client: task is stopped')
async def start() -> None: async def start() -> None:
''' Starts OSC task ''' ''' Starts OSC task '''