mstar.communication.codec#

The encode/decode seam shared by both ZMQ transports.

Kept apart from either communicator so they can both import it, and mirroring the Rust Codec trait:

pub trait Codec<M> {
    fn encode(msg: &M) -> Result<Vec<u8>, CommError>;
    fn decode(bytes: &[u8]) -> Option<M>;
}

The transport never looks inside the bytes, so moving an edge to another encoding is a codec change, never a transport change.

Functions

decode_each(codec, frames, who)

Decode a drained batch one frame at a time.

default_codec()

MSTAR_WIRE_CODEC=pickle falls back to pickle.

Classes

Codec()

MsgpackCodec()

Raw msgpack, no schema: for edges whose payloads are already plain dicts and whose other end is not Python — the Rust mstar-server frontend bridge.

PickleCodec()

The old wire.

WireCodec()

Typed msgpack (mstar.communication.wire): language-neutral and lossless for the message dataclasses.

class mstar.communication.codec.Codec[source]#

Bases: object

static decode(data)[source]#
Parameters:

data (bytes)

static encode(msg)[source]#
Return type:

bytes

class mstar.communication.codec.MsgpackCodec[source]#

Bases: Codec

Raw msgpack, no schema: for edges whose payloads are already plain dicts and whose other end is not Python — the Rust mstar-server frontend bridge. default=str is lossy, so this is NOT a general replacement for pickle; use WireCodec for message dataclasses.

static decode(data)[source]#
Parameters:

data (bytes)

static encode(msg)[source]#
Return type:

bytes

class mstar.communication.codec.PickleCodec[source]#

Bases: Codec

The old wire. Python-only, so an edge using it cannot terminate in a Rust process; kept for bisecting a wire problem.

static decode(data, /, *, fix_imports=True, encoding='ASCII', errors='strict', buffers=())#

Read and return an object from the given pickle data.

The protocol version of the pickle is detected automatically, so no protocol argument is needed. Bytes past the pickled object’s representation are ignored.

Optional keyword arguments are fix_imports, encoding and errors, which are used to control compatibility support for pickle stream generated by Python 2. If fix_imports is True, pickle will try to map the old Python 2 names to the new names used in Python 3. The encoding and errors tell pickle how to decode 8-bit string instances pickled by Python 2; these default to ‘ASCII’ and ‘strict’, respectively. The encoding can be ‘bytes’ to read these 8-bit string instances as bytes objects.

static encode(obj, protocol=None, *, fix_imports=True, buffer_callback=None)#

Return the pickled representation of the object as a bytes object.

The optional protocol argument tells the pickler to use the given protocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default protocol is 4. It was introduced in Python 3.4, and is incompatible with previous versions.

Specifying a negative protocol version selects the highest protocol version supported. The higher the protocol used, the more recent the version of Python needed to read the pickle produced.

If fix_imports is True and protocol is less than 3, pickle will try to map the new Python 3 names to the old module names used in Python 2, so that the pickle data stream is readable with Python 2.

If buffer_callback is None (the default), buffer views are serialized into file as part of the pickle stream. It is an error if buffer_callback is not None and protocol is None or smaller than 5.

class mstar.communication.codec.WireCodec[source]#

Bases: Codec

Typed msgpack (mstar.communication.wire): language-neutral and lossless for the message dataclasses. The default.

static decode(data)[source]#
Parameters:

data (bytes)

static encode(msg)[source]#
Return type:

bytes

mstar.communication.codec.decode_each(codec, frames, who)[source]#

Decode a drained batch one frame at a time.

A frame that fails to decode is logged and skipped. Raising instead would lose every frame already drained with it – control messages for other requests included – to one bad payload.

Parameters:
Return type:

list

mstar.communication.codec.default_codec()[source]#

MSTAR_WIRE_CODEC=pickle falls back to pickle.

Both ends of an edge must agree, so this is all-or-nothing for a mesh — a single process set differently will not be able to talk to its peers.

Return type:

type[Codec]