Encode a binary frame into a provided buffer (length prefix + body).
The buffer is cleared first; capacity is preserved across calls.
Used by per-session buffering to avoid per-call allocation.
Extract session_id from raw frame bytes without full deserialization.
raw starts at the first byte after the 4-byte length prefix (i.e. the msg_type byte).
Returns None for Authenticate (which has no session_id).
Serialize a binary frame to a complete byte vector (length prefix + body).
Used by per-session buffering to build the frame without holding any shared lock.