diff --git a/.vscode/launch.json b/.vscode/launch.json index 732ce52..bdf17e8 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,7 @@ "type": "debugpy", "request": "launch", "module": "hbd.server.cli", - "args": ["-c", "~/.hb.yaml", "-f", "-v", "-x"], + "args": ["-c", "~/.hb.yaml", "-f", "-v"], "cwd": "${workspaceFolder}", "env": { "PYTHONPATH": "${workspaceFolder}" diff --git a/hbd/server/main.py b/hbd/server/main.py index 9f9703f..0267bfb 100644 --- a/hbd/server/main.py +++ b/hbd/server/main.py @@ -191,14 +191,14 @@ async def _run_async(config, config_path=None): sock.bind(bind_addr) logger.info("Starting UDP server on %s:%s", *bind_addr) - # Try to enable kernel receive timestamps (Linux SO_TIMESTAMPNS). + # Try to enable kernel receive timestamps (Linux SO_TIMESTAMP). # If supported, read datagrams via recvmsg() so RTT uses the kernel # timestamp rather than the time.time() call after asyncio scheduling. use_kernel_ts = udp.enable_kernel_timestamps(sock) if use_kernel_ts: - logger.info("SO_TIMESTAMPNS enabled: using kernel receive timestamps for RTT") + logger.info("SO_TIMESTAMP enabled: using kernel receive timestamps for RTT") else: - logger.info("SO_TIMESTAMPNS not available: using time.time() for RTT") + logger.info("SO_TIMESTAMP not available: using time.time() for RTT") def udp_handler(msg, addr, transport, recv_ts=None): ctx = dict( diff --git a/hbd/server/udp.py b/hbd/server/udp.py index 2494e1c..49dc045 100644 --- a/hbd/server/udp.py +++ b/hbd/server/udp.py @@ -7,6 +7,8 @@ import time import zlib import logging +from platform import system as platform_system + from ..common.proto import stodict, oldmtodict from ..common.utils import dur from . import notify as notify_mod @@ -16,9 +18,18 @@ eventlog = notify_mod.eventlog # SO_TIMESTAMP: kernel attaches a struct timeval to each received datagram. # Supported on Linux, FreeBSD, and macOS. The constant is not exposed by -# Python's socket module on all platforms, so fall back to the Linux value (29) -# when absent. -_SO_TIMESTAMP = getattr(socket, 'SO_TIMESTAMP', 29) +# Python's socket module on all platforms +platform = platform_system() +if platform == "Darwin": + _SO_TIMESTAMP = 1024 # SO_TIMESTAMP on macOS (not in Python's socket module) +elif platform == "Linux": + _SO_TIMESTAMP = 29 # Linux value (not in older Python versions) +elif platform == "FreeBSD": + _SO_TIMESTAMP = 32 # FreeBSD value (not in older Python versions) +else: + logger.warning("SO_TIMESTAMP may not be supported on this platform (%s)", platform) + _SO_TIMESTAMP = None + # struct timeval uses two native C longs: tv_sec and tv_usec _TIMEVAL = struct.Struct('@ll')