Server¶
veltix.server.server.Server
¶
__init__
¶
Initialize the TCP server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
ServerConfig
|
Server configuration. |
required |
set_callback
¶
Bind a callback function to a server event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
Union[str, Events]
|
Event type (Events enum or string). |
required |
func
|
Callable
|
Callback function: - ON_RECV: func(client: ClientInfo, response: Response) - ON_CONNECT: func(client: ClientInfo) - ON_DISCONNECT: func(client: ClientInfo) |
required |
route
¶
Decorator to register a route callback for a specific message type.
Usage
@server.route(MY_TYPE) def on_my_type(response: Response, client: ClientInfo) -> None: ...
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_
|
MessageType
|
Message type to intercept. |
required |
Returns:
| Type | Description |
|---|---|
Callable
|
Decorator function. |
get_all_clients_sockets
¶
Return a list of all connected client sockets.
send_and_wait
¶
Send a request to a client and block until the matching response is received.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
Request to send. |
required |
client
|
ClientInfo
|
Target client. |
required |
timeout
|
float
|
Maximum time to wait for a response in seconds (default: 5.0). |
5.0
|
Returns:
| Type | Description |
|---|---|
Optional[Response]
|
Matching Response, or None on timeout or send failure. |
ping_client
¶
Ping a client and measure round-trip latency.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
ClientInfo
|
Client to ping. |
required |
timeout
|
float
|
Timeout in seconds (default: 5.0). |
5.0
|
Returns:
| Type | Description |
|---|---|
Optional[float]
|
Latency in milliseconds, or None on timeout. |
ping_client_async
¶
ping_client_async(client: ClientInfo, callback: Callable[[Optional[float]], None], timeout: float = 5.0) -> None
Ping a client asynchronously and call callback with the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
ClientInfo
|
Client to ping. |
required |
callback
|
Callable[[Optional[float]], None]
|
Called with latency in ms, or None on timeout. |
required |
timeout
|
float
|
Timeout in seconds (default: 5.0). |
5.0
|
start
¶
Start the server and begin accepting connections.
Non-blocking — starts a background thread and returns immediately.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
_on_th
|
bool
|
Internal parameter, do not use. |
False
|
close_client
¶
Close a client connection and remove it from the list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
ClientInfo
|
The client to disconnect. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the client was found and removed, False otherwise. |
veltix.server.server.ServerConfig
dataclass
¶
TCP server configuration.
Attributes:
| Name | Type | Description |
|---|---|---|
host |
str
|
Server listening address (default: '0.0.0.0') |
port |
int
|
Server listening port (default: 8080) |
buffer_size |
int
|
Buffer size for receiving data in bytes. Use BufferSize enum for common presets (default: BufferSize.SMALL). Can also be set to any custom integer value. |
max_connection |
int
|
Maximum number of simultaneous connections (default: 2) |
max_message_size |
int
|
Maximum allowed message size in bytes (default: 10MB) |
handshake_timeout |
float
|
Maximum time to wait for handshake completion in seconds (default: 5.0) |
max_workers |
int
|
Number of worker threads for callback execution (default: 4). Increase if your on_recv callback is slow or blocking. |
performance_mode |
PerformanceMode
|
Controls internal timing parameters (default: BALANCED). |
veltix.server.server.ClientInfo
dataclass
¶
Information about a connected client.
Attributes:
| Name | Type | Description |
|---|---|---|
conn |
socket
|
Socket connection to the client. |
addr |
_RetAddress
|
Client address (host, port). |
thread_id |
int
|
Internal thread identifier. |
handshake_done |
bool
|
Whether the handshake has been completed. |