# `Alternera.PluginWasmFramework.Runtime.Port.Protocol`
[🔗](https://github.com/10e-ab/alternera-wasm/blob/v0.9.1/alternera_plugin_wasm_framework/lib/alternera/plugin_wasm_framework/runtime/port/protocol.ex#L1)

The frames between the node and `alternera_wasm_host serve`, over the
port with `{:packet, 4}`. A frame is one tag byte and fixed
little-endian fields, then bytes to its end. The framework's own frames
(`PluginWasmFramework.Frame`) travel inside as bytes.

Node to host: `H` hello; `L` load (digest, pinned, the compiled file's
path); `U` unload; `C` call (digest, flags, timeout ms, memory bytes,
free bytes, export, input); `R` a host call's answer (1 and the reply,
or 0: refused); `B` a body piece (1 and the bytes, or 0: no such body);
`G` a growth answer (1 allowed, 0 refused); `X` cancel.

Host to node: `h` hello (protocol, Wasmtime version, target); `l`
loaded (0 and whether `_initialize` is exported, or 1 and why not);
`c` a host call (op, arg); `b` a body read (which, offset, length);
`g` growth (the bytes wanted in all); `r` the result (outcome, the
memory's size, then the reply or a trap's text).

The host speaks only to answer, and inside a call to ask: whoever owns
the port reads every frame there is.

# `body`

```elixir
@spec body(binary() | :none) :: iodata()
```

# `call`

```elixir
@spec call(binary(), String.t(), iodata() | :none, map(), non_neg_integer()) ::
  iodata()
```

# `cancel`

```elixir
@spec cancel() :: binary()
```

# `decode`

```elixir
@spec decode(binary()) ::
  {:host_call, String.t(), binary()}
  | {:body_read, :request | :response, non_neg_integer(), non_neg_integer()}
  | {:grow, non_neg_integer()}
  | {:result, atom(), non_neg_integer(), binary()}
  | {:loaded, boolean()}
  | {:load_failed, String.t()}
  | {:unknown, binary()}
```

A frame from the host, decoded.

# `decode_hello`

```elixir
@spec decode_hello(binary()) ::
  {:ok,
   %{protocol: non_neg_integer(), wasmtime: String.t(), target: String.t()}}
  | :error
```

Decodes an `h` frame.

# `grow`

```elixir
@spec grow(boolean()) :: binary()
```

# `hello`

```elixir
@spec hello() :: binary()
```

# `host_reply`

```elixir
@spec host_reply(iodata() | :refused) :: iodata()
```

A host call's answer: the guest-facing reply, or a refusal.

# `load`

```elixir
@spec load(binary(), boolean(), Path.t()) :: iodata()
```

# `load_into`

```elixir
@spec load_into(port(), binary(), boolean(), Path.t(), timeout()) ::
  :ok | {:error, String.t()} | {:no_answer, String.t()}
```

Loads a compiled module into the port this process owns and waits for
the answer, at most `timeout` ms. `{:no_answer, reason}` when none came
in time: the host may still answer, so the process must not serve
another call.

# `send_frame`

```elixir
@spec send_frame(port(), iodata()) :: boolean()
```

Sends `frame`; false when the port is closed (the host died, or was
killed by another call on it).

# `version`

```elixir
@spec version() :: pos_integer()
```

The protocol version this side speaks.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
