Skip to content

Socket

veltix.socket_core.core.SocketCore

Bases: Enum

Available socket implementations.

veltix.socket_core.base_socket.BaseSocket

Bases: ABC

Abstract base class defining the socket backend interface.

Concrete implementations (ThreadingSocket, AsyncSocket) must implement every abstract method declared here. The server and client layers depend only on this interface, allowing socket backends to be swapped at runtime.

Attributes:

Name Type Description
client_manager ClientsManager

Manages connected client entries.

handshake_timeout float

Timeout in seconds for the handshake phase.

bus VeltixBus

Event bus for structured observability.

send

send(data: bytes) -> bool

Send raw bytes over the connection.

Parameters:

Name Type Description Default
data bytes

The bytes to send.

required

Returns:

Type Description
bool

True if the data was sent successfully, False otherwise.

recv

recv(buf_size: int) -> bytes

Receive data from the connection.

Parameters:

Name Type Description Default
buf_size int

Maximum number of bytes to receive.

required

Returns:

Type Description
bytes

The received bytes, or an empty byte-string when the peer has

bytes

closed the connection (EOF).

Raises:

Type Description
OSError

On socket errors, including ConnectionResetError and BrokenPipeError. A TimeoutError is raised when a timeout is set and no data arrives in time.

settimeout

settimeout(timeout: float) -> bool

Set the socket timeout for blocking operations.

Parameters:

Name Type Description Default
timeout float

Timeout in seconds.

required

Returns:

Type Description
bool

True if the timeout was set successfully, False otherwise.

fileno

fileno() -> int

Return the underlying socket file descriptor.

Returns:

Type Description
int

The file descriptor of the underlying socket.

close abstractmethod

close() -> bool

Close the socket and release associated resources.

Returns:

Type Description
bool

True if the socket was closed successfully, False otherwise.

bind abstractmethod

bind(host: str, port: int, max_client: int, buffer_size: int, timeout: float) -> bool

Bind the socket to an address and start listening for connections.

Parameters:

Name Type Description Default
host str

The host address to bind to.

required
port int

The port number to bind to.

required
max_client int

Maximum number of concurrent clients (-1 for unlimited).

required
buffer_size int

Receive buffer size in bytes.

required
timeout float

Socket poll timeout in seconds for the accept and receive loops.

required

Returns:

Type Description
bool

True if binding succeeded. False when the server is already

bool

running (threading backend).

Raises:

Type Description
OSError

When the address cannot be bound (e.g. the port is already in use); the error propagates to the caller.

connect abstractmethod

connect(host: str, port: int, buffer_size: int, timeout: float) -> bool

Connect to a remote server.

Parameters:

Name Type Description Default
host str

The server host address.

required
port int

The server port number.

required
buffer_size int

Receive buffer size in bytes.

required
timeout float

Timeout in seconds for the connection attempt.

required

Returns:

Type Description
bool

True if the connection was established, False otherwise.

close_client abstractmethod

close_client(client: ClientEntry | int) -> bool

Close a specific client connection on the server side.

Parameters:

Name Type Description Default
client ClientEntry | int

The client entry or client ID to disconnect.

required

Returns:

Type Description
bool

True if the client was disconnected, False otherwise.

disconnect abstractmethod

disconnect(timeout: float) -> bool

Disconnect from the remote server (client-side).

Parameters:

Name Type Description Default
timeout float

Timeout in seconds for the disconnection.

required

Returns:

Type Description
bool

True if disconnection succeeded, False otherwise. Returns True

bool

when the socket was never connected (no-op).

veltix.socket_core.threading_socket.ThreadingSocket

Bases: BaseSocket

Threading-based socket implementation for Veltix (one thread per client).

veltix.socket_core.async_socket.AsyncSocket

Bases: BaseSocket

Selector-based socket implementation for Veltix.

send

send(data: bytes) -> bool

Send raw bytes, queueing any remainder past the kernel buffer.

Non-blocking by design: the selector thread never blocks on a slow peer. When the kernel send buffer is full, the unsent tail is queued and flushed by the selector loop as soon as the socket becomes writable again, so messages larger than the buffer are delivered instead of being dropped.

Parameters:

Name Type Description Default
data bytes

The bytes to send.

required

Returns:

Type Description
bool

True when the data was sent or queued for delivery, False on a

bool

hard connection error.