> ## Documentation Index
> Fetch the complete documentation index at: https://iso8583sim.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Framing

> Message framing for TCP: length headers and TPDUs.

`iso8583sim.net.framing`

## `Framing`

```python theme={null}
Framing(
    header: str = '2b',
    tpdu: bytes | None = None,
    header_includes_self: bool = False,
)
```

How messages are delimited on a TCP stream.

**Attributes**

<ResponseField name="header" type="str">
  Length header: "2b" / "4b" (2 or 4 byte big-endian binary) or "2a" / "4a"
  (2 or 4 ASCII digits). The length covers everything after the header.
</ResponseField>

<ResponseField name="tpdu" type="bytes | None">
  Optional TPDU sent after the length header, e.g. bytes.fromhex("6000010000").
  On responses the destination and source addresses are swapped.
</ResponseField>

<ResponseField name="header_includes_self" type="bool">
  Some hosts count the header bytes in the length. Off by default.
</ResponseField>

### `frame()`

```python theme={null}
def frame(message: bytes, tpdu: bytes | None = None) -> bytes
```

Add the TPDU (if configured) and the length header to an encoded message.

### `read()`

```python theme={null}
async def read(reader: asyncio.StreamReader) -> tuple[bytes | None, bytes]
```

Read one framed message. Returns (tpdu, message).

Raises asyncio.IncompleteReadError if the connection closes mid-frame, and EOFError
when it closes cleanly between frames.

### `split()`

```python theme={null}
def split(buffer: bytes) -> tuple[list[tuple[bytes | None, bytes]], bytes]
```

Split a byte buffer into complete frames. Returns (frames, leftover bytes).

## Functions

### `swap_tpdu()`

```python theme={null}
def swap_tpdu(tpdu: bytes | None) -> bytes | None
```

Swap a TPDU's destination and source addresses, as a response does.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.