/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { MockTcpConnection } from "./MockTcpConnection.js";
class MockTcpListener {
  port;
  #host;
  #connectionListeners = /* @__PURE__ */ new Set();
  constructor(host, options) {
    this.port = options.listeningPort ?? 1024 + Math.floor(Math.random() * 64511);
    this.#host = host;
    host.registerTcpListener(this);
  }
  /**
   * Called by {@link MockNetwork.connectTcp} when a client connects.
   * Creates a socket pair, notifies connection listeners with the server-side
   * socket, and returns the client-side socket.
   */
  accept(clientAddress, clientPort) {
    const [clientSocket, serverSocket] = MockTcpConnection.createPair(
      clientAddress,
      clientPort,
      this.#host.defaultRoute,
      this.port
    );
    for (const listener of this.#connectionListeners) {
      listener(serverSocket);
    }
    return clientSocket;
  }
  onConnection(listener) {
    this.#connectionListeners.add(listener);
    return {
      close: async () => {
        this.#connectionListeners.delete(listener);
      }
    };
  }
  async close() {
    this.#host.unregisterTcpListener(this.port);
  }
}
export {
  MockTcpListener
};
//# sourceMappingURL=MockTcpListener.js.map
