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:
- Server → Client : {"v", "pv", "meta"}
- Client → Server : {"v", "pv", "meta"}
- Server → Client : {"result": "ok"}
Server mode sends first, then validates client protocol version before acking. Client mode reads server protocol version, validates, sends its version, then waits for the server ack before returning.
__init__
¶
Initialise the handshake handler for a given role.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
Mode
|
Whether this handler operates as |
required |
bus
|
VeltixBus
|
Event bus for emitting handshake events and logging. |
required |
send_rejection
¶
Send a rejection payload and close the connection.
Used by the server to explicitly reject a client before the handshake (e.g. when the server is full).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sock
|
RawSocket
|
A raw TCP socket conforming to :class: |
required |
reason
|
str
|
Rejection reason string (e.g. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the rejection was sent, False on error. |
do_server_handshake
¶
Perform the server-side 3-way handshake.
Steps
- Send
{"v": ..., "pv": ..., "meta": {}}to the client. - Receive the client's
{"v": ..., "pv": ..., "meta": ...}response. - Validate the client's protocol version.
- Send
{"result": "ok"}to acknowledge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sock
|
RawSocket
|
A raw TCP socket conforming to :class: |
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
¶
Perform the client-side 3-way handshake.
Steps
- Receive the server's
{"v": ..., "pv": ..., "meta": ...}payload. - Validate the server's protocol version.
- Send
{"v": ..., "meta": {}}to the server. - Wait for the server's
{"result": "ok"}acknowledgment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sock
|
RawSocket
|
A raw TCP socket conforming to :class: |
required |
timeout
|
float
|
Maximum seconds to wait for each recv. |
5.0
|
Returns:
| Type | Description |
|---|---|
bool
|
A tuple of |
dict[str, Any] | None
|
metadata dict on success, or |