Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame^] | 1 | # |
| 2 | # SPDX-License-Identifier: GPL-2.0-only |
| 3 | # |
| 4 | |
| 5 | import itertools |
| 6 | import json |
| 7 | |
| 8 | # The Python async server defaults to a 64K receive buffer, so we hardcode our |
| 9 | # maximum chunk size. It would be better if the client and server reported to |
| 10 | # each other what the maximum chunk sizes were, but that will slow down the |
| 11 | # connection setup with a round trip delay so I'd rather not do that unless it |
| 12 | # is necessary |
| 13 | DEFAULT_MAX_CHUNK = 32 * 1024 |
| 14 | |
| 15 | |
| 16 | def chunkify(msg, max_chunk): |
| 17 | if len(msg) < max_chunk - 1: |
| 18 | yield ''.join((msg, "\n")) |
| 19 | else: |
| 20 | yield ''.join((json.dumps({ |
| 21 | 'chunk-stream': None |
| 22 | }), "\n")) |
| 23 | |
| 24 | args = [iter(msg)] * (max_chunk - 1) |
| 25 | for m in map(''.join, itertools.zip_longest(*args, fillvalue='')): |
| 26 | yield ''.join(itertools.chain(m, "\n")) |
| 27 | yield "\n" |
| 28 | |
| 29 | |
| 30 | from .client import AsyncClient, Client |
| 31 | from .serv import AsyncServer, AsyncServerConnection |