Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TCP/IP Non-blocking and No Timeout #13

Open
1 task
TylerFlar opened this issue Jan 4, 2025 · 0 comments
Open
1 task

TCP/IP Non-blocking and No Timeout #13

TylerFlar opened this issue Jan 4, 2025 · 0 comments

Comments

@TylerFlar
Copy link
Contributor

Objective

Currently, the TCP/IP or "Simulated" connection is blocked while waiting for a connection when a program utilizes the package. It would be beneficial to run this process in a separate thread or asynchronously, as the main application that uses the package is already waiting for an ACK or another packet. Therefore, there is no need to make the program wait. Additionally, because TCP/IP is stable and allows for more user control, we should eliminate timeouts. The main programs using this package can recreate the object from the drone_comms module to simulate a timeout instead.

Requirements

  • The waiting process for the server/client connection must be handled using threading or asynchronous promises.
  • Server/client connections must not have timeouts.

Tasks

  1. Review and update the connect() method in the SimulatedRadioInterface
    def connect(self) -> None:
    """Establish TCP/IP connection in either client or server mode."""
    import socket
    self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    if self.server_mode:
    self.sock.bind((self.host, self.port))
    self.sock.listen(1)
    logger.info("Waiting for connection on %s:%d...", self.host, self.port)
    # Set socket to non-blocking mode
    self.sock.setblocking(False) # noqa: FBT003
    while True:
    try:
    self.conn, addr = self.sock.accept()
    logger.info("Client connected from %s:%d", addr[0], addr[1])
    # Set normal timeout for subsequent operations
    self.conn.settimeout(self.read_timeout)
    break
    except BlockingIOError:
    time.sleep(0.1) # Sleep briefly to avoid busy-waiting
    except Exception:
    logger.exception("Error accepting connection")
    self.close()
    raise
    else:
    try:
    self.sock.settimeout(10.0) # 10 seconds for client connection
    self.sock.connect((self.host, self.port))
    self.conn = self.sock
    self.conn.settimeout(self.read_timeout)
    logger.info("Connected to server %s:%d", self.host, self.port)
    except (socket.timeout, ConnectionRefusedError):
    logger.exception("Failed to connect to server")
    self.close()
    raise
    .

Deliverables

  • Pull request to the dev branch.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant