# `PhoenixWebTransport.SessionEngine`
[🔗](https://github.com/knevan/phoenix-wtransport/blob/main/lib/phoenix_wtransport/session_engine.ex#L1)

Backend-agnostic WebTransport session state machine driving a
`Phoenix.Socket.Transport`.

`PhoenixWebTransport.Quic.Session` delegates routing, origin validation, dev
code reloading, `connect_info` & `auth_token` bridging, inbound frame
decoding, and outbound push/close normalization to this module.

Functions return `{engine, actions}` (or `{:ok | :error, ...}` on connect)
where `actions` are backend-neutral effect tuples:

  * `{:push, lane, {:text | :binary, binary()}}`
  * `{:push_barrier, 0, {:text, binary()}, [pos_integer()]}`
  * `{:push_datagram, {:text | :binary, binary()}}`
  * `:drain_session`
  * `{:close_session, close_code :: non_neg_integer(), reason :: binary()}`

# `action`

```elixir
@type action() ::
  {:push, non_neg_integer(), {:text | :binary, binary()}}
  | {:push_barrier, 0, {:text, binary()}, [pos_integer()]}
  | {:push_datagram, {:text | :binary, binary()}}
  | :drain_session
  | {:close_session, close_code(), binary()}
```

# `close_code`

```elixir
@type close_code() :: non_neg_integer()
```

# `connect_request`

```elixir
@type connect_request() :: %{
  optional(:protocol) =&gt; binary() | nil,
  :path =&gt; binary(),
  optional(:query) =&gt; binary() | nil,
  optional(:headers) =&gt; [{binary(), binary()}],
  optional(:peer_data) =&gt; map() | nil,
  optional(:host) =&gt; binary() | nil,
  optional(:port) =&gt; :inet.port_number() | nil
}
```

# `t`

```elixir
@type t() :: %PhoenixWebTransport.SessionEngine{
  closing: boolean(),
  endpoint: module() | nil,
  in_buf: %{optional(term()) =&gt; binary()},
  in_buf_bytes: non_neg_integer(),
  max_client_streams: pos_integer(),
  max_frame_size: non_neg_integer(),
  max_in_buf_bytes: non_neg_integer(),
  max_lanes: pos_integer(),
  path: String.t(),
  pending_connect: map() | nil,
  routes: [PhoenixWebTransport.Router.route()],
  socket: module() | nil,
  socket_opts: keyword(),
  socket_state: term() | nil,
  terminated?: boolean()
}
```

# `accepted?`

```elixir
@spec accepted?(t()) :: boolean()
```

Returns true when `socket.connect/1` succeeded or a pre-join `:auth` frame is pending.

# `admit_stream?`

```elixir
@spec admit_stream?(t(), term()) :: boolean()
```

Returns true when a new inbound client stream can be admitted without
exceeding `max_client_streams`.

# `awaiting_auth?`

```elixir
@spec awaiting_auth?(t()) :: boolean()
```

Returns true while awaiting a pre-join `:auth` frame before `socket.connect/1`.

# `close_stream`

```elixir
@spec close_stream(t(), term()) :: t()
```

Drops state and byte accounting for a closed inbound stream.

# `connect`

```elixir
@spec connect(t(), connect_request()) :: {:ok, t()} | {:error, pos_integer(), term()}
```

Validates an incoming WebTransport `CONNECT` request against `routes`, checks
origin, runs dev code reloading if configured, and either invokes
`socket.connect/1` or defers until the client sends its pre-join `:auth` frame.

# `drain`

```elixir
@spec drain(t()) :: {t(), [action()]}
```

Initiates a graceful shutdown drain (`1012` + `:drain_session`), invoking
`socket.handle_info(:socket_drain, ...)` when initialized.

# `handle_datagram`

```elixir
@spec handle_datagram(t(), binary()) :: {t(), [action()]}
```

Routes an inbound RFC 9297 WebTransport datagram through `socket.handle_in/2`.

# `handle_in`

```elixir
@spec handle_in(t(), term(), binary()) :: {t(), [action()]}
```

Decodes inbound stream bytes into Phoenix frames (handling any pre-join
`:auth` frame first) and dispatches them through `socket.handle_in/2`.

# `handle_info`

```elixir
@spec handle_info(t(), term()) :: {t(), [action()]}
```

Routes an Erlang message through `socket.handle_info/2` and normalizes
pushes and 3/4/5-tuple `:stop` returns.

# `init_socket`

```elixir
@spec init_socket(t()) :: {:ok, t()}
```

Initializes the Phoenix socket inside the session-owning process.

If pre-join auth is still pending, returns `{:ok, engine}` and initializes
automatically as soon as the `:auth` frame arrives.

# `new`

```elixir
@spec new(keyword()) :: t()
```

Builds a new session engine from listener or router options.

# `open_stream`

```elixir
@spec open_stream(t(), term()) :: t()
```

Registers an inbound client stream.

# `terminate`

```elixir
@spec terminate(t(), term()) :: t()
```

Calls `socket.terminate/2` idempotently when the session stops.

---

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