connectViaTCP connects to the CLI server via TCP socket.
(ctx context.Context)
| 2002 | |
| 2003 | // connectViaTCP connects to the CLI server via TCP socket. |
| 2004 | func (c *Client) connectViaTCP(ctx context.Context) error { |
| 2005 | if c.actualPort == 0 { |
| 2006 | return fmt.Errorf("server port not available") |
| 2007 | } |
| 2008 | |
| 2009 | // Merge a 10-second timeout with the caller's context so whichever |
| 2010 | // deadline comes first wins. |
| 2011 | address := net.JoinHostPort(c.actualHost, fmt.Sprintf("%d", c.actualPort)) |
| 2012 | dialCtx, cancel := context.WithTimeout(ctx, 10*time.Second) |
| 2013 | defer cancel() |
| 2014 | var dialer net.Dialer |
| 2015 | conn, err := dialer.DialContext(dialCtx, "tcp", address) |
| 2016 | if err != nil { |
| 2017 | return fmt.Errorf("failed to connect to CLI server at %s: %w", address, err) |
| 2018 | } |
| 2019 | |
| 2020 | c.conn = conn |
| 2021 | |
| 2022 | // Create JSON-RPC client with the connection |
| 2023 | c.client = jsonrpc2.NewClient(conn, conn) |
| 2024 | if c.processDone != nil { |
| 2025 | c.client.SetProcessDone(c.processDone, c.processErrorPtr) |
| 2026 | } |
| 2027 | c.client.SetOnClose(func() { |
| 2028 | go func() { |
| 2029 | c.startStopMux.Lock() |
| 2030 | defer c.startStopMux.Unlock() |
| 2031 | c.state = stateDisconnected |
| 2032 | }() |
| 2033 | }) |
| 2034 | c.RPC = rpc.NewServerRPC(c.client) |
| 2035 | c.internalRPC = rpc.NewInternalServerRPC(c.client) |
| 2036 | c.setupNotificationHandler() |
| 2037 | c.client.Start() |
| 2038 | |
| 2039 | return nil |
| 2040 | } |
| 2041 | |
| 2042 | // setupNotificationHandler configures handlers for session events and RPC requests. |
| 2043 | func (c *Client) setupNotificationHandler() { |
no test coverage detected