Skip to content

HandshakeHandler

veltix.handler.handshake_handler.HandshakeHandler

Manage the version compatibility handshake for a single raw TCP connection. Uses a 3-way protocol to ensure both sides are synchronized:

  1. Server → Client : {"v", "meta"}
  2. Client → Server : {"v", "meta"}
  3. Server → Client : {"result": "ok"}

Server mode sends first, then validates client version before acking. Client mode reads server version, validates, sends its version, then waits for the server ack before returning.

__init__

__init__(
    mode: Mode, bus: VeltixBus, id_window: int = 30000
) -> None

Initialise the handshake handler for a given role.

Parameters:

Name Type Description Default
mode Mode

Whether this handler operates as SERVER or CLIENT.

required
bus VeltixBus

Event bus for emitting handshake events and logging.

required
id_window int

The ID window size announced to the peer (server only).

30000

do_server_handshake

do_server_handshake(
    sock: RawSocket, timeout: float = 5.0
) -> bool

Perform the server-side 3-way handshake.

Steps
  1. Send {"v": ..., "meta": {"id_window": ...}} to the client.
  2. Receive the client's {"v": ..., "meta": ...} response.
  3. Validate the client's version against the compatibility table.
  4. Send {"result": "ok"} to acknowledge.

Parameters:

Name Type Description Default
sock RawSocket

A raw TCP socket conforming to :class:RawSocket.

required
timeout float

Maximum seconds to wait for each recv.

5.0

Returns:

Type Description
bool

True if the handshake succeeded, False on any failure.

do_client_handshake

do_client_handshake(
    sock: RawSocket,
) -> tuple[bool, Optional[dict[str, Any]]]

Perform the client-side 3-way handshake.

Steps
  1. Receive the server's {"v": ..., "meta": ...} payload.
  2. Validate the server's version against the compatibility table.
  3. Send {"v": ..., "meta": {}} to the server.
  4. Wait for the server's {"result": "ok"} acknowledgment.

Parameters:

Name Type Description Default
sock RawSocket

A raw TCP socket conforming to :class:RawSocket.

required

Returns:

Type Description
bool

A tuple of (success, meta) where meta is the server's

Optional[dict[str, Any]]

metadata dict on success, or None on failure.