# `XGW.Peer.Sctp`
[🔗](https://github.com/Lazarus404/xgw/blob/main/lib/xgw/peer/sctp.ex#L1)

Starts and drives the SCTP association used for WebRTC data channels.

## What problem this solves

Browsers open **data channels** for arbitrary binary/text beside audio/video.
Under the hood that is **SCTP** inside the DTLS tunnel. This module starts a
passive association when SDP negotiated SCTP and SSL is up, then applies
association events (transmit, channel open/close, messages) into Peer or
`XGW.Peer.Datachannel` state and handler callbacks.

Production ownership of the live association and DTLS app-data socket is the
per-Peer `XGW.Peer.Datachannel` process. Handler messages always carry the
Peer pid as `peer_ref` (via state `:peer` when set).

## RFCs

* RFC 8261 -- SCTP over DTLS
* RFC 8831 -- WebRTC data channels
* RFC 8841 -- SDP negotiation of SCTP

# `flush_events`

```elixir
@spec flush_events(map(), list(), (map(), binary() -&gt; :ok | {:error, term()})) ::
  map()
```

Applies SCTP association events; `send_dtls` posts app data on the DTLS socket.

Events include outbound SCTP chunks to transmit, channel lifecycle, and
application messages that become `{:xgw, :datachannel_*, ...}` callbacks.

# `maybe_start`

```elixir
@spec maybe_start(map()) :: map()
```

Starts a passive SCTP association when SDP negotiated SCTP and SSL is up.

No-ops when SCTP was not negotiated, SSL is not ready, or `ex_sctp` is unloaded.

---

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