blob: b2bec31ab2fc85b5f84c54af129ba6a4df83997a [file] [log] [blame]
Andrew Geisslerc926e172021-05-07 16:11:35 -05001#
2# SPDX-License-Identifier: GPL-2.0-only
3#
4
5import itertools
6import 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
13DEFAULT_MAX_CHUNK = 32 * 1024
14
15
16def 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
30from .client import AsyncClient, Client
31from .serv import AsyncServer, AsyncServerConnection