netclient.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. """netClient C++ 单例的 stub: 直接持有 Wire,负责收发 Frame。"""
  2. from __future__ import annotations
  3. from apilot.wire import Frame, Wire
  4. class NetClientStub:
  5. def __init__(self) -> None:
  6. self.wire: Wire | None = None
  7. self._sdata: int = 0
  8. def connect(self, host: str, port: int, timeout: float = 10) -> None:
  9. self.wire = Wire(host, port, timeout)
  10. def disconnect(self) -> None:
  11. if self.wire is not None:
  12. self.wire.close()
  13. self.wire = None
  14. def is_connected(self) -> bool:
  15. return self.wire is not None
  16. def send_raw(self, data: bytes) -> None:
  17. assert self.wire is not None, "netClient not connected"
  18. self.wire.sock.sendall(data)
  19. def send_frame(self, f: Frame) -> None:
  20. assert self.wire is not None, "netClient not connected"
  21. self.wire.send(f)
  22. def tick(self, deadline_s: float) -> list[Frame]:
  23. if self.wire is None:
  24. return []
  25. return self.wire.recv(deadline_s)
  26. def set_sdata(self, v: int) -> None:
  27. self._sdata = int(v)
  28. def get_sdata(self) -> int:
  29. return self._sdata