ExMqtt.Protocol (ex_mqtt_uv v0.1.0)

Copy Markdown View Source

Unified Sans-I/O protocol entry point dispatching between MQTT 3.1.1 (V311) and MQTT 5.0 (V5) codecs.

Version Selection

Supported version identifiers (all normalized to :"3.1.1" or :"5.0"):

  • MQTT 3.1.1: :"3.1.1", :v311, 4, "3.1.1"
  • MQTT 5.0: :"5.0", :v5, 5, "5.0"

Summary

Functions

Returns the codec module for the given normalized protocol version.

Decodes a single MQTT packet from the front of buffer.

Decodes all complete MQTT packets from buffer, returning {:ok, packets, remaining_buffer} or {:error, reason} if a malformed packet is encountered.

Encodes an ExMqtt.Protocol.Packet struct into iodata for version.

Encodes a packet directly into a flat binary.

Normalizes user-provided protocol version into :"3.1.1" or :"5.0".

Same as normalize_version/1 but raises on unsupported version.

Types

version()

@type version() :: :"3.1.1" | :"5.0"

version_input()

@type version_input() :: version() | :v311 | :v5 | 4 | 5 | String.t()

Functions

codec_for(atom)

@spec codec_for(version()) :: module()

Returns the codec module for the given normalized protocol version.

decode(buffer, version \\ :"5.0")

@spec decode(binary(), version_input()) ::
  {:ok, ExMqtt.Protocol.Packet.t(), binary()} | :incomplete | {:error, term()}

Decodes a single MQTT packet from the front of buffer.

Returns:

  • {:ok, packet, rest} when a complete packet was decoded
  • :incomplete when more bytes are needed from the transport
  • {:error, reason} when the packet violates the selected protocol version

decode_all(buffer, version \\ :"5.0")

@spec decode_all(binary(), version_input()) ::
  {:ok, [ExMqtt.Protocol.Packet.t()], binary()} | {:error, term()}

Decodes all complete MQTT packets from buffer, returning {:ok, packets, remaining_buffer} or {:error, reason} if a malformed packet is encountered.

encode(packet, version \\ :"5.0")

@spec encode(ExMqtt.Protocol.Packet.t(), version_input()) ::
  {:ok, iodata()} | {:error, term()}

Encodes an ExMqtt.Protocol.Packet struct into iodata for version.

encode_binary(packet, version \\ :"5.0")

@spec encode_binary(ExMqtt.Protocol.Packet.t(), version_input()) ::
  {:ok, binary()} | {:error, term()}

Encodes a packet directly into a flat binary.

normalize_version(v)

@spec normalize_version(version_input()) ::
  {:ok, version()} | {:error, {:unsupported_version, term()}}

Normalizes user-provided protocol version into :"3.1.1" or :"5.0".

normalize_version!(v)

@spec normalize_version!(version_input()) :: version()

Same as normalize_version/1 but raises on unsupported version.