Connect to the CLI server via TCP socket. Creates a TCP connection to the server at the configured host and port. Raises: RuntimeError: If the server port is not available or connection fails.
(self)
| 3697 | self._client.start(loop) |
| 3698 | |
| 3699 | async def _connect_via_tcp(self) -> None: |
| 3700 | """ |
| 3701 | Connect to the CLI server via TCP socket. |
| 3702 | |
| 3703 | Creates a TCP connection to the server at the configured host and port. |
| 3704 | |
| 3705 | Raises: |
| 3706 | RuntimeError: If the server port is not available or connection fails. |
| 3707 | """ |
| 3708 | if not self._runtime_port: |
| 3709 | raise RuntimeError("Server port not available") |
| 3710 | |
| 3711 | # Create a TCP socket connection with timeout |
| 3712 | import socket |
| 3713 | |
| 3714 | # Connection timeout constant |
| 3715 | TCP_CONNECTION_TIMEOUT = 10 # seconds |
| 3716 | |
| 3717 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 3718 | sock.settimeout(TCP_CONNECTION_TIMEOUT) |
| 3719 | |
| 3720 | try: |
| 3721 | tcp_connect_start = time.perf_counter() |
| 3722 | logger.info( |
| 3723 | "CopilotClient._connect_via_tcp connecting to CLI server", |
| 3724 | extra={"host": self._actual_host, "port": self._runtime_port}, |
| 3725 | ) |
| 3726 | sock.connect((self._actual_host, self._runtime_port)) |
| 3727 | sock.settimeout(None) # Remove timeout after connection |
| 3728 | log_timing( |
| 3729 | logger, |
| 3730 | logging.DEBUG, |
| 3731 | "CopilotClient._connect_via_tcp TCP connect complete", |
| 3732 | tcp_connect_start, |
| 3733 | host=self._actual_host, |
| 3734 | port=self._runtime_port, |
| 3735 | ) |
| 3736 | except OSError as e: |
| 3737 | raise RuntimeError( |
| 3738 | f"Failed to connect to CLI server at {self._actual_host}:{self._runtime_port}: {e}" |
| 3739 | ) |
| 3740 | |
| 3741 | # Create a file-like wrapper for the socket |
| 3742 | sock_file = sock.makefile("rwb", buffering=0) |
| 3743 | |
| 3744 | # Create a mock process object that JsonRpcClient expects |
| 3745 | class SocketWrapper: |
| 3746 | def __init__(self, sock_file, sock_obj): |
| 3747 | self.stdin = sock_file |
| 3748 | self.stdout = sock_file |
| 3749 | self.stderr = None |
| 3750 | self._socket = sock_obj |
| 3751 | |
| 3752 | def terminate(self): |
| 3753 | import socket as _socket_mod |
| 3754 | |
| 3755 | # shutdown() sends TCP FIN to the server (triggering |
| 3756 | # server-side disconnect detection) and interrupts any |
no test coverage detected