# Struct client::Session

> A sans-IO client session. See the module documentation.

`zaun_community_protocol::client::Session` · Struct

```rust
pub struct Session { … }
```

A sans-IO client session. See the module documentation.

## Methods

### `hello`

```rust
pub fn hello(&mut self, now_ms: u64) -> Result<RequestId, ClientError>
```

Sends `session.hello` with a fresh nonce. Call it first, and again after every
reconnect; commands then wait for the new welcome and are resent after it.

SPEC: `community_id` is "unbound only"; it is sent when the session has no binding
and is `null` otherwise.

# Errors
`ClientError::Failed` after a failed welcome,
`ClientError::Rng` or a framing error.

### `send`

```rust
pub fn send<B>(&mut self, body: &B, community: Option<CommunityId>, channel: Option<ChannelId>, now_ms: u64) -> Result<RequestId, ClientError>
```

Sends a command and tracks it for retries with the same `request_id` and
byte-identical body. The body is validated first.

# Errors
`ClientError::NotReady` before a verified welcome, `ClientError::Failed` after a
failed one, `ClientError::WrongCommunity`, `ClientError::WrongKind` for a signal,
or a body, size, framing or RNG error.

### `send_behind_hello`

```rust
pub fn send_behind_hello<B>(&mut self, body: &B, now_ms: u64) -> Result<RequestId, ClientError>
```

Sends a read-only sync command (`sync.snapshot`, `sync.resume`, `sync.events`) right
behind the hello of a bound session, without waiting for the welcome: the host runs the connection's commands in order, so it answers after the
welcome. The answer is reported only once the welcome verified. After the welcome this
is `Session::send`.

# Errors
As `Session::send`; `ClientError::NotReady` for another command, or before a hello
or without a binding.

### `send_signal`

```rust
pub fn send_signal<B>(&mut self, body: &B, community: Option<CommunityId>, channel: Option<ChannelId>) -> Result<(), ClientError>
```

Sends an ephemeral signal (`signal.*`): `request_id: null`, no response, no retry.

# Errors
As `Session::send`; `ClientError::WrongKind` for a non-signal body.

### `resume_request`

```rust
pub fn resume_request(&mut self, now_ms: u64) -> Result<Vec<RequestId>, ClientError>
```

Sends `sync.resume` for every tracked stream, in chunks of 100 channels. Call
it after the welcome of a reconnect.

# Errors
As `Session::send`.

### `send_command`

```rust
pub fn send_command(&mut self, command: &Command, community: Option<CommunityId>, channel: Option<ChannelId>, now_ms: u64) -> Result<Option<RequestId>, ClientError>
```

Sends a command known only at run time (a bot's, an application's raw command):
`Session::send`, or `Session::send_signal` for a `signal.*` (no request id).

# Errors
As `Session::send`.

### `receive`

```rust
pub fn receive(&mut self, text: &str, now_ms: u64) -> Vec<ClientEvent>
```

Processes one received transport text at `now_ms`: reassembles fragments, parses the
envelope, correlates responses, orders events by their stream cursors, and queues any
`sync.resume` (after a gap) or `sync.ack` that became due.

Events are only applied on a verified session; before the welcome (or after a failed
one) they are dropped and the resume after the welcome recovers them.

SPEC: the protocol acks what the client has "durably applied". An ack queued here covers the
events returned by this call, so the caller must apply (persist) them before sending
the texts it drains next.

### `poll_timeouts`

```rust
pub fn poll_timeouts(&mut self, now_ms: u64) -> Vec<ClientEvent>
```

Drives timers at `now_ms`: discards stale fragment reassemblies, resends due requests
with the same text (commands only once the welcome is verified), abandons requests
after the give-up time, and queues a `sync.ack` when 30 s passed with unacked frames or
when a refused ack's retry time has come.

### `new`

```rust
pub fn new(config: SessionConfig) -> Self
```

A session that has sent nothing yet; call `Session::hello` first.

### `state`

```rust
pub const fn state(&self) -> &SessionState
```

The lifecycle state.

### `welcome`

```rust
pub const fn welcome(&self) -> Option<&VerifiedWelcome>
```

The verified welcome of the current hello.

### `community`

```rust
pub fn community(&self) -> Option<&ValidPair>
```

The verified descriptor and delegation; persist its `state()`.

### `hold_pair`

```rust
pub fn hold_pair(&mut self, pair: (SignedJson, SignedJson))
```

Holds `pair` (a verified signed descriptor and delegation) for this connection's
community: the next hello names it, so its welcome may leave it out. For a
creator, whose welcome carried no pair.

### `binding`

```rust
pub const fn binding(&self) -> Option<Binding>
```

The connection's binding, from the welcome or a successful create/redeem/request.

### `negotiated_version`

```rust
pub fn negotiated_version(&self) -> Option<u16>
```

The negotiated protocol version.

### `host_challenge`

```rust
pub fn host_challenge(&self) -> Option<Challenge>
```

The challenge to sign into a `MemberKeyRegistration`.

### `cursors`

```rust
pub const fn cursors(&self) -> &CursorTable
```

The stream cursors.

### `cursors_mut`

```rust
pub const fn cursors_mut(&mut self) -> &mut CursorTable
```

The stream cursors, for seeding after a snapshot or history paging.

### `pending_count`

```rust
pub fn pending_count(&self) -> usize
```

Unanswered requests (hello and commands).

### `poll_transmit`

```rust
pub fn poll_transmit(&mut self) -> Option<String>
```

The next transport text to send, in order.

### `drain_transmit`

```rust
pub fn drain_transmit(&mut self) -> Vec<String>
```

Every queued transport text, in order.

## Trait implementations

`Debug`

[Full rustdoc page](/docs/api/rust/zaun-community-protocol/zaun_community_protocol/client/session/struct.Session.html)
