Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sans_io import *
- @protocol
- async def bytes_protocol():
- try:
- while True:
- sz: bytes = await read_exactly(4)
- length = int.from_bytes(sz, 'big')
- hdr = await read_exactly(length*4)
- data = array.array.frombytes('L', hdr)
- if sys.byteorder != "big":
- data.byteswap()
- text, *buffers = [(await read_exactly(l)) for l in data]
- text = text.encode("utf-8")
- text = json.loads(text)
- await emit(Message(text, buffers))
- except ConnectionResetError:
- pass
- x = bytes_protocol()
- for event in x.feed(b"stuff..."):
- _do_stuff_with(event)
- for event in x.feed(b"stuff..."):
- _do_stuff_with(event)
- #...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement