# `HL7v2.MLLP.Client`
[🔗](https://github.com/Balneario-de-Cofrentes/hl7v2/blob/v3.10.1/lib/hl7v2/mllp/client.ex#L1)

MLLP TCP client for sending HL7v2 messages.

Maintains a persistent TCP connection and provides synchronous
send-and-receive for HL7v2 messages over MLLP.

## Examples

    {:ok, client} = HL7v2.MLLP.Client.start_link(host: "localhost", port: 2575)

    {:ok, ack} = HL7v2.MLLP.Client.send_message(client, hl7_message)

    :ok = HL7v2.MLLP.Client.close(client)

## TLS

    {:ok, client} = HL7v2.MLLP.Client.start_link(
      host: "remote.host",
      port: 2576,
      tls: [verify: :verify_peer, cacertfile: "ca.pem"]
    )

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `close`

```elixir
@spec close(GenServer.server()) :: :ok
```

Closes the client connection.

# `send_message`

```elixir
@spec send_message(GenServer.server(), binary(), keyword()) ::
  {:ok, binary()} | {:error, term()}
```

Sends an HL7v2 message and waits for the response.

MLLP is strictly request/response — one message sent, one ACK received.
Terminal errors that close the connection and stop the client:

- `{:error, :protocol_desync}` — stale bytes from a previous exchange
- `{:error, :message_too_large}` — response exceeded `:max_message_size`

After either error the caller must start a new client to continue.

The message is MLLP-framed before sending. The response is returned
with MLLP framing stripped.

## Options

- `:timeout` — override the default timeout for this call.

# `start_link`

```elixir
@spec start_link(keyword()) :: GenServer.on_start()
```

Starts a client connection.

## Options

- `:host` (required) — hostname or IP address to connect to.
- `:port` (required) — TCP port to connect to.
- `:timeout` — send/receive timeout in milliseconds (default: `30_000`).
- `:max_message_size` — maximum response size in bytes (default: `10_485_760` — 10 MB).
  Returns `{:error, :message_too_large}` if the response buffer exceeds this limit.
- `:tls` — keyword list of `:ssl` options. When present, connects via TLS.

---

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