Improve DASP trace: use S/R labels, add timestamp and full hex datagram
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -85,16 +85,20 @@ _MSG_TYPE_NAMES = {
|
||||
}
|
||||
|
||||
|
||||
def _trace(direction, msg, addr=None):
|
||||
def _trace(direction, msg, datagram=None):
|
||||
import app_state
|
||||
if not app_state.TRACE_DASP:
|
||||
return
|
||||
import datetime
|
||||
ts = datetime.datetime.now().strftime("%H:%M:%S.%f")[:-3]
|
||||
name = _MSG_TYPE_NAMES.get(msg.msg_type, f"?{msg.msg_type}")
|
||||
seq = f" seq={msg.seq_num}" if msg.seq_num >= 0 else ""
|
||||
ack = f" ack={msg.ack}" if msg.ack >= 0 else ""
|
||||
plen = len(msg.payload) if msg.payload else 0
|
||||
dest = f" {addr[0]}:{addr[1]}" if addr else ""
|
||||
print(f"[DASP] {direction}{dest} {name}{seq}{ack} payload={plen}")
|
||||
hexdump = ""
|
||||
if datagram:
|
||||
hexdump = " [" + datagram.hex(" ") + "]"
|
||||
print(f"[DASP] {ts} {direction} {name}{seq}{ack} payload={plen}{hexdump}")
|
||||
|
||||
|
||||
class DaspException(Exception):
|
||||
@@ -916,8 +920,8 @@ class DaspSocket:
|
||||
# ── Network I/O ─────────────────────────────────────────────
|
||||
|
||||
def _send_msg(self, session, msg):
|
||||
_trace(">>>", msg, (session.host, session.port))
|
||||
data = msg.encode()
|
||||
_trace("S", msg, data)
|
||||
with self._send_lock:
|
||||
try:
|
||||
self._sock.sendto(data, (session.host, session.port))
|
||||
@@ -931,8 +935,9 @@ class DaspSocket:
|
||||
nbytes, addr = self._sock.recvfrom_into(buf)
|
||||
if nbytes < 5:
|
||||
continue
|
||||
raw = bytes(buf[:nbytes])
|
||||
msg = DaspMsg.decode(buf, nbytes)
|
||||
self._dispatch(msg, addr)
|
||||
self._dispatch(msg, addr, raw)
|
||||
except socket.timeout:
|
||||
continue
|
||||
except OSError:
|
||||
@@ -940,8 +945,8 @@ class DaspSocket:
|
||||
continue
|
||||
break
|
||||
|
||||
def _dispatch(self, msg, addr):
|
||||
_trace("<<<", msg, addr)
|
||||
def _dispatch(self, msg, addr, raw=None):
|
||||
_trace("R", msg, raw)
|
||||
session_id = msg.session_id
|
||||
with self._sessions_lock:
|
||||
session = self._sessions.get(session_id)
|
||||
|
||||
Reference in New Issue
Block a user