MeshxRpc.Protocol.Default (MeshxRpc v0.1.0) View Source

RPC protocol default functions.

Link to this section Summary

Functions

Calculates checksum for given data with :erlang.crc32/1.

Returns connection reference as 4 bytes random binary.

De-serializes given bin to Erlang term with :erlang.binary_to_term/2.

Returns node reference as Node.self() converted to string with length limited to 255 characters.

Serializes given Erlang term to binary with :erlang.term_to_binary/2.

Link to this section Functions

Specs

checksum(data :: binary(), _opts :: term()) :: binary()

Calculates checksum for given data with :erlang.crc32/1.

Function returns checksum as 4 bytes binary big endian unsigned integer.

iex(1)> MeshxRpc.Protocol.Default.checksum("test", [])
<<216, 127, 126, 12>>

Specs

conn_ref() :: binary()

Returns connection reference as 4 bytes random binary.

iex(1)> MeshxRpc.Protocol.Default.conn_ref()
<<171, 248, 41, 163>>
iex(2)> MeshxRpc.Protocol.Default.conn_ref() |> Base.encode64(padding: false)
"IKWzCw"
Link to this function

deserialize(bin, opts, ser_flag \\ 1)

View Source

Specs

deserialize(bin :: binary(), opts :: Keyword.t(), serialization_flag :: 0..255) ::
  {:ok, result :: term()} | {:error, reason :: term()}

De-serializes given bin to Erlang term with :erlang.binary_to_term/2.

Function performs reverse operation to serialize/2. If serialization_flag is 0 de-serialization step is skipped and function returns {:ok, bin}. Otherwise bin is de-serialized using :erlang.binary_to_term/2.

opts is passed as second argument to :erlang.binary_to_term/2. serialize/2 provides usage example.

Specs

node_ref() :: binary()

Returns node reference as Node.self() converted to string with length limited to 255 characters.

iex(1)> MeshxRpc.Protocol.Default.node_ref()
"nonode@nohost"

Specs

serialize(term :: term(), opts :: Keyword.t()) ::
  {:ok, result :: binary(), serialization_flag :: 0..255}
  | {:error, reason :: term()}

Serializes given Erlang term to binary with :erlang.term_to_binary/2.

If successful function returns serialized binary as result and serialization_flag.

If user provided term is of binary type, serialization step is skipped and serialization_flag is set to 0. Otherwise :erlang.term_to_binary(term, opts) is called and serialization_flag is set to 1.

Function argument opts is passed as options to :erlang.term_to_binary/2. Serialization options can be used to force binary data compression, which by default is disabled.

iex(1)> {:ok, bin, ser_flag} = MeshxRpc.Protocol.Default.serialize(%{test_k: "test_v"}, [])
{:ok,
<<131, 116, 0, 0, 0, 1, 100, 0, 6, 116, 101, 115, 116, 95, 107, 109, 0, 0, 0,
 6, 116, 101, 115, 116, 95, 118>>, 1}
iex(2)> MeshxRpc.Protocol.Default.deserialize(bin, [], ser_flag)
{:ok, %{test_k: "test_v"}}
iex(3)> {:ok, bin, ser_flag} = MeshxRpc.Protocol.Default.serialize("test", [])
{:ok, "test", 0}
iex(4)> MeshxRpc.Protocol.Default.deserialize(bin, [], ser_flag)
{:ok, "test"}