prpc.protocol.messages

Logical message types definitions, encapsulating both field access and wire serialization format.

Note

Message classes are rather similar, so they could have been generated using some factory. However, generating ‘first-class’ classes without self-introspection would require really ugly hacks with exec (e.g. see collections.namedtuple). As a bonus, manual classes are IDE-friendly.

Note

namedtuple itself isn’t right as there is no sane way to ‘bind’ the first tuple argument to be constant (MessageType).

prpc.protocol.messages.message_class(cls)

Decorator to register message class for deserialization.

Explicit mapping looks cleaner than __subclasses__ hacks and adds some bonus validation.

Parameters:cls – Class with (not yet registered) class-level MESSAGE_TYPE variable.
Returns:Cls itself without any changes.
prpc.protocol.messages.pack(data)

Convert ‘simple’ (~POD) python object to a compact byte representation.

Parameters:data – Object to pack.
Returns:Byte representation of the object.
prpc.protocol.messages.unpack(dgram)

Unpack byte representation of an object.

Parameters:dgram – Serialized object bytes.
Returns:Unpacked object.
class prpc.protocol.messages.ProtocolMessage

Base class for protocol messages.

classmethod from_bytes(dgram)

Decode message from wire format.

Parameters:dgram – Message bytes.
Returns:Decoded message.
to_bytes()

Encode message to wire format.

Returns:Serialized message bytes.
to_tuple()

Convert message to a tuple (with fixed field order).

class prpc.protocol.messages.HandshakeData(protocol_version, id, user_data)

Handshake data. Very basic for now.

id

Alias for field number 1

protocol_version

Alias for field number 0

user_data

Alias for field number 2

class prpc.protocol.messages.ErrorDescription(message, cause_type, cause_message, trace)

Serialized exception object.

cause_message

Alias for field number 2

cause_type

Alias for field number 1

message

Alias for field number 0

trace

Alias for field number 3

class prpc.protocol.messages.RequestHandshake(id, args)

Handshake message sent by ‘connecting’ side.

Contains 1 payload field:

  • args: handshake ‘arguments’ (must be HandshakeData tuple)
class prpc.protocol.messages.RequestCallStart(id, call_type, method, args, kwargs)

Message requesting a new call.

Contains 4 payload fields:

  • call_type: enum value desribing call type
  • method: method name
  • args: positional argument list
  • kwargs: keyword argument dict
class prpc.protocol.messages.RequestCallCancel(id)

Request to close remote stream on the callee side.

Has no payload.

class prpc.protocol.messages.RequestStreamMessage(id, data)

Data message containing stream message from caller.

Only 1 payload field:

  • data: unserialized data (will be packed with the message itself)
class prpc.protocol.messages.RequestStreamClose(id)

Request to close remote stream on the callee side.

Has no payload.

class prpc.protocol.messages.ResponseHandshake(id, result, status, error)

Handshake call response.

Contains 3 payload fields:

  • result: call return value (must be a HandshakeData tuple)
  • status: call status
  • error: error as ErrorDescription tuple
class prpc.protocol.messages.ResponseResult(id, result, status, error)

Regular call final response.

Has similar structure to the hanshake response, but the result isn’t limited to HandshakeData.

Contains 3 payload fields:

  • result: call return value
  • status: call status
  • error: error as ErrorDescription tuple
class prpc.protocol.messages.ResponseStreamMessage(id, data)

Data message containing stream message from callee.

Only 1 payload field:

  • data: unserialized data (will be packed with the message itself)
class prpc.protocol.messages.ResponseStreamClose(id)

Request to close stream on the calling side.

Has no payload.