Socket Module

class mtf.network_port.socket.Socket
__init__(self: mtf.libs.mtf_pybinder.mtf_socket.Socket, socket_type: mtf.libs.mtf_pybinder.mtf_socket.SocketType, socket_protocol: mtf.libs.mtf_pybinder.mtf_socket.SocketProtocol) None

Initializes a new Socket object.

Parameters: socket_type (SocketType): Specifies the stack of the socket, either LWIP or BOOST. socket_protocol (SocketProtocol): Specifies the type of the socket, either TCP or UDP.

Example: socket = Socket(SocketType.LWIP, SocketProtocol.TCP)

clean_up()
__new__(**kwargs)
accept(self: mtf.libs.mtf_pybinder.mtf_socket.Socket) None

Accepts an incoming connection.

This method retrieves a pending connection request from the queue.

bind(self: mtf.libs.mtf_pybinder.mtf_socket.Socket, address: Tuple[str, int]) None

Binds the socket to a local address and port.

Parameters: address (tuple): A tuple containing:

  • address (str): The local address to bind to.

  • port (int): The local port to bind to.

close(self: mtf.libs.mtf_pybinder.mtf_socket.Socket) None

Closes the socket.

This method will clean up the resources associated with the socket.

connect(self: mtf.libs.mtf_pybinder.mtf_socket.Socket, peer: Tuple[str, int]) mtf.libs.mtf_pybinder.Result

Connects the socket to a remote address and port.

Parameters: peer (tuple): A tuple containing:

  • peer_address (str): The remote address to connect to.

  • peer_port (int): The remote port to connect to.

Returns: core::Result: Returns kOk if the connection was successful, or kNotOk if it failed.

classmethod controllers_cleanup()

Clean up all controller instances.

Calls the clean_up() method for each instance.

is_connected(self: mtf.libs.mtf_pybinder.mtf_socket.Socket) bool

Checks if the TCP client socket is connected.

Returns: bool: True if the TCP client socket is connected, False otherwise.

Raises: SocketException: If the socket is not a TCP client.

is_running(self: mtf.libs.mtf_pybinder.mtf_socket.Socket) bool

Checks if the TCP server socket is running.

Returns: bool: True if the TCP server socket is running, False otherwise.

listen(self: mtf.libs.mtf_pybinder.mtf_socket.Socket, max_connections: int = 5) bool

Starts listening for incoming connections.

This method must be called after binding a TCP server socket to an address and port. It enables the server to accept incoming client connections.

Parameters: max_connections (int): The maximum number of pending connections that the queue will hold.

Returns: bool: True if the server started successfully, False otherwise.

send(self: mtf.libs.mtf_pybinder.mtf_socket.Socket, payload: List[int]) mtf.libs.mtf_pybinder.Result

Sends data through the socket.

Parameters: payload (List[int]): The data to send.

Returns: core::Result: Returns kOk if the data was sent successfully, or kNotOk if it failed.

set_connection_handler(self: mtf.libs.mtf_pybinder.mtf_socket.Socket, connection_handler: function) None

Sets a callback function to handle connection state changes.

Parameters: connection_handler (function): A Python function that takes a SocketState enumeration value as input.

set_error_handler(self: mtf.libs.mtf_pybinder.mtf_socket.Socket, error_handler: function) None

Sets a callback function to handle errors.

Parameters: error_handler (function): A Python function that takes an integer error code and a string error message as input.

set_network_adapter(self: mtf.libs.mtf_pybinder.mtf_socket.Socket, adapter_ip_or_name: str) None

Configure the network adapter for the socket (specifically for use with LWIP).

This method allows you to set the physical network adapter by specifying its IP address or name. It is applicable only when working with LWIP sockets.

Parameters: adapter_ip_or_name (string): The IP address or name of the adapter you want to bind the socket to.

Example: socket.set_network_adapter(“192.168.1.1”) socket.set_network_adapter(“eth0”)

set_receive_handler(self: mtf.libs.mtf_pybinder.mtf_socket.Socket, receive_handler: function) None

Sets a callback function to handle received data.

Parameters: receive_handler (function): A Python function that takes a list of integers as input, representing the received data.

set_timeout(self: mtf.libs.mtf_pybinder.mtf_socket.Socket, timeout: int) None

Sets the connection timeout for the socket (default to 1s).

This method allows you to specify a timeout duration for connection attempts. A timeout value of 0 indicates no timeout (asynchronous connection).

Parameters: timeout (int): The timeout duration in milliseconds.

Example: socket.set_timeout(5000) # Set a timeout of 5000 milliseconds (5 seconds).