8ee8c07

Branches > main > 8ee8c07 > files

Decode all valid packets in decode-packet!, even if they have no schema

Grahp, 2026-08-25

Commit: 8ee8c07d7b9b044f8229f8b9a3dbf026e3e1b852
Parent: 9f21a66

2 changed files:

src/net/bowmap/transport/connection.clj modified (view file)

@@ -16,10 +16,7 @@ (defn handle-serverbound-frame!   [state conn frame]   (let [{::phase/keys! [phase]} @state-        packet (try (decode-packet! frame phase)-                    (catch Exception e-                      (Throwable/.printStackTrace (Exception. "Failed to decode packet!" e))-                      nil))]+        packet (decode-packet! frame phase)]     (when-let [{::pk/keys! [id]} packet]       (case id         ::serverbound/handshaking        (swap! state assoc ::phase/phase (req! packet ::phase/phase))

src/net/bowmap/transport/packets.clj modified (view file)

@@ -31,7 +31,8 @@ (defn encode-packet!   "Encodes a packet into a frame.    Requires the connection phase, as packet ids are not unique across phases.-   Several packets are logically the same across phases."+   Several packets are logically the same across phases.+   Throws when the packet cannot be written."   [packet phase]   (let [payload-writer (req! packet-encoders (::pk/id packet))]     (writers/length-prefixed (-> (Unpooled/buffer)@@ -51,20 +52,21 @@  (defn build-packet-readers [registry]   (reduce (fn [acc [packet-id table]]-            (assoc acc packet-id-                   (let [reader (readers/coerce table)]-                     (fn [buf]-                       (assoc (reader buf)-                              ::pk/id packet-id)))))+            ; TODO coerce isn't a very good name+            (assoc acc packet-id (readers/coerce table)))           {} registry))  (def serverbound-packet-ids (build-serverbound-packet-id-index serverbound/packet-order)) (def packet-decoders (build-packet-readers serverbound/packets)) +; TODO may want to just make this return only the id when there is no schema (defn decode-packet!-  "Decodes a packet from a frame."+  "Decodes a packet from a frame.+   Will throw if no such packet exists with that id.+   May return an empty packet if the packet schema is not known/nil."   [frame phase]   (let [numeric-id (readers/->varint frame)-        packet-id (req! serverbound-packet-ids {::phase/phase phase ::pk/numeric-id numeric-id})-        decode-fn (req! packet-decoders packet-id)]-    (decode-fn frame)))+        packet-id (req! serverbound-packet-ids {::phase/phase phase ::pk/numeric-id numeric-id})]+    (assoc (when-let [decode-fn (packet-decoders packet-id)]+             (decode-fn frame))+           ::pk/id packet-id)))