Factorization

* Added packet.py which can serialize OSC packets
This commit is contained in:
2025-11-11 12:30:16 -05:00
parent 4cfe79f474
commit b7c525a84a
3 changed files with 74 additions and 33 deletions

View File

@@ -3,6 +3,9 @@
import asyncio
import struct
from pytyuosc import packet
#import packet
_DEBUG = True
_TASK = None
_Q = asyncio.Queue()
@@ -31,38 +34,13 @@ async def __serve() -> None:
port = val['port']
addr = val['addr']
data = val['data']
# OSC type tag string
type_tag = ','
# OSC data to append to the final packet
osc_data = bytes()
# check if data is correct
for d in data:
d_type = type(d)
# process the value
if d_type is int:
type_tag += 'i'
osc_data += struct.pack('>i', d)
elif d_type is str:
type_tag += 's'
osc_data += __string_to_osc(d)
# unsupported
else:
if _DEBUG:
print('[!] OSC Client: unsupported data type was provided! The packet is discarded.')
print(' * host: %s' % host)
print(' * port: %s' % port)
print(' * address: %s' % addr)
print(' * data: %s' % data)
print(' (bad type is %s)' % d_type)
type_tag = None
break
# no type tag
if type_tag is None:
try:
data_to_send = packet.serialize(addr, data, _DEBUG)
if data_to_send is None:
raise Exception()
except:
continue
# convert type tag to OSC-string
type_tag = __string_to_osc(type_tag)
# create OSC packet
packet = __string_to_osc(addr) + type_tag + osc_data
# send the packet
trans, prot = await asyncio.get_running_loop().create_datagram_endpoint(
@@ -71,7 +49,7 @@ async def __serve() -> None:
allow_broadcast=True
)
try:
trans.sendto(packet)
trans.sendto(data_to_send)
if _DEBUG: print('[I] OSC Client: sent data to %s:%s' % (host, port))
except:
if _DEBUG:
@@ -118,3 +96,16 @@ async def send(host: str, port: str, addr: str, values: list) -> None:
'data': values
}
await _Q.put(d)
#
# TEST
#
async def __test__() -> None:
print('Going to send data to 255.255.255.255:8000')
await start()
await send('255.255.255.255', 8000, '/pytyuosc/osc/testing/is/in/progress', ['a', 'b', 1, 2, 3.4, 5.6])
await stop()
print('Data must have been sent')
if __name__ == '__main__':
asyncio.run(__test__())