# dista.conn

> A module of Dista's API.

A non-blocking framed connection, for the head's event loop.

The head talks to every driver and worker at once, so it must never block on
one of them. A `Conn` reads and writes with `MSG_DONTWAIT`, so no call
blocks. It reads whatever has arrived into an input buffer and
hands out complete frames, and queues outgoing frames in an output buffer
that `flush` drains as the socket accepts it. The event loop polls for
writability only while something is queued.

## Structs

### `Conn`

```mojo
struct Conn
```

Implements: `Movable`

Fields:

- `stream: Stream`
- `closed: Bool`: The peer hung up, or the connection failed.

#### `__init__`

```mojo
def __init__(out self, var stream: Stream)
```

#### `fd`

```mojo
def fd(self) -> c_int
```

#### `read_available`

```mojo
def read_available(mut self, limit: Int = Int(0))
```

Reads what's waiting on the socket into the input buffer: all of it, or with `limit`, about that many bytes, leaving the rest in the socket, where `poll` still reports it.

#### `next_frame`

```mojo
def next_frame(mut self) -> Optional[Frame]
```

The next complete frame in the input buffer, if there is one.

#### `send`

```mojo
def send(mut self, kind: UInt8, object_id: UInt64, task_id: UInt64, payload: Span[UInt8])
```

Queues a frame and sends as much as the socket takes now.

#### `flush`

```mojo
def flush(mut self)
```

Sends queued output until the socket would block.

#### `wants_write`

```mojo
def wants_write(self) -> Bool
```

#### `unsent`

```mojo
def unsent(self) -> Int
```

Bytes queued to send that the peer hasn't taken yet.
