xref: /OK3568_Linux_fs/yocto/bitbake/lib/bb/asyncrpc/__init__.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun#
2*4882a593Smuzhiyun# Copyright BitBake Contributors
3*4882a593Smuzhiyun#
4*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0-only
5*4882a593Smuzhiyun#
6*4882a593Smuzhiyun
7*4882a593Smuzhiyunimport itertools
8*4882a593Smuzhiyunimport json
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun# The Python async server defaults to a 64K receive buffer, so we hardcode our
11*4882a593Smuzhiyun# maximum chunk size. It would be better if the client and server reported to
12*4882a593Smuzhiyun# each other what the maximum chunk sizes were, but that will slow down the
13*4882a593Smuzhiyun# connection setup with a round trip delay so I'd rather not do that unless it
14*4882a593Smuzhiyun# is necessary
15*4882a593SmuzhiyunDEFAULT_MAX_CHUNK = 32 * 1024
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun
18*4882a593Smuzhiyundef chunkify(msg, max_chunk):
19*4882a593Smuzhiyun    if len(msg) < max_chunk - 1:
20*4882a593Smuzhiyun        yield ''.join((msg, "\n"))
21*4882a593Smuzhiyun    else:
22*4882a593Smuzhiyun        yield ''.join((json.dumps({
23*4882a593Smuzhiyun                'chunk-stream': None
24*4882a593Smuzhiyun            }), "\n"))
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun        args = [iter(msg)] * (max_chunk - 1)
27*4882a593Smuzhiyun        for m in map(''.join, itertools.zip_longest(*args, fillvalue='')):
28*4882a593Smuzhiyun            yield ''.join(itertools.chain(m, "\n"))
29*4882a593Smuzhiyun        yield "\n"
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun
32*4882a593Smuzhiyunfrom .client import AsyncClient, Client
33*4882a593Smuzhiyunfrom .serv import AsyncServer, AsyncServerConnection, ClientError, ServerError
34