(socket: net.Socket)
| 433 | } |
| 434 | |
| 435 | private handleConnection(socket: net.Socket): void { |
| 436 | if (this.stopping) { socket.destroy(); return; } |
| 437 | this.acceptedSockets.add(socket); |
| 438 | socket.once('close', () => this.acceptedSockets.delete(socket)); |
| 439 | // Hello first so the proxy can verify versions before piping any |
| 440 | // application bytes. The proxy reads exactly one line, then forwards. |
| 441 | const hello: DaemonHello = { |
| 442 | codegraph: CodeGraphPackageVersion, |
| 443 | pid: process.pid, |
| 444 | socketPath: this.socketPath, |
| 445 | protocol: 1, |
| 446 | }; |
| 447 | socket.write(JSON.stringify(hello) + '\n'); |
| 448 | |
| 449 | // Read the optional client-hello (proxy → daemon) to learn the client's |
| 450 | // peer pids, then hand the socket to the session. Fail-safe: any problem — |
| 451 | // timeout, a non-hello first line, an early close — yields null pids and we |
| 452 | // fall back to the socket-close lifecycle exactly as before (#692). |
| 453 | void readClientHello(socket).then((peers) => { |
| 454 | if (this.stopping || socket.destroyed) { socket.destroy(); return; } |
| 455 | const transport = new SocketTransport(socket); |
| 456 | const session = new MCPSession(transport, this.engine, { |
| 457 | explicitProjectPath: this.projectRoot, |
| 458 | }); |
| 459 | transport.onClose(() => this.dropClient(session)); |
| 460 | this.clients.add(session); |
| 461 | this.clientPeers.set(session, peers); |
| 462 | this.disarmIdleTimer(); |
| 463 | session.start(); |
| 464 | // Observe inbound bytes purely to feed the inactivity backstop — a second |
| 465 | // 'data' listener that reads nothing, added AFTER the transport's so the |
| 466 | // unshifted client-hello tail reaches the transport intact. |
| 467 | socket.on('data', () => { this.lastActivityAt = Date.now(); }); |
| 468 | }); |
| 469 | } |
| 470 | |
| 471 | private dropClient(session: MCPSession): void { |
| 472 | if (!this.clients.delete(session)) return; |
no test coverage detected