I've made a simple UDP server for the new uasyncio code. It invokes a callback when a packet arrives. It looks at the return value of the callback and sends this back the client. If the callback returns None no response is sent. It is pretty minimalist but it does what I need it do do.
One thing I was unsure of was if it is really OK to poll() the socket busy. So I added a 1ms timeout to poll(). As I understand it this is pointless on MCUs as there are no powersaving states on the MCU but if anyone would care to acknowledge that it would be great.
I use it like this:
Code: Select all
from dgram import UDPServer
import uasyncio
(..)
def cb(msg, adr):
print('Got:', msg)
return 'ack'.encode('ascii')
def main():
s = dgram.UDPServer()
l = uasyncio.get_event_loop()
l.run_until_complete(s.serve(cb, '0.0.0.0', port))
Feedback is welcome.