0afd85a

Bowmap · bowmap.git · Tickets

Branches > main > 0afd85a > files

Use tagged literals to represent frames

Grahp, 2026-10-09

Commit: 0afd85a5e64b840afc784552a8b7e291b941a0c0
Parent: 5c11afa

2 changed files:

src/net/bowmap/server/network/streams.clj modified (view file)

@@ -58,8 +58,8 @@   "Blocks and writes a frame to an OutputStream.    Throws if the frame cannot be written."   [frame output-stream]-  (frame/write! frame-    (fn [array length]-        (write-varint!! output-stream length)-        (OutputStream/.write output-stream array 0 length)-        (OutputStream/.flush output-stream))))+  (let [array (frame/array frame)]+    (doto output-stream+      (write-varint!! (alength array))+      (OutputStream/.write array)+      (OutputStream/.flush))))

src/net/bowmap/server/packet/frame.clj modified (view file)

@@ -1,36 +1,26 @@ (ns net.bowmap.server.packet.frame-  (:require-   [clojure.spec.alpha :as s])-  (:import-   [io.netty.buffer ByteBuf ByteBufUtil Unpooled]))--(defprotocol Frame   "A immutable sequence of bytes representing one framed, serialized packet.    Frames contain the packet's numeric id and payload, but are not length-prefixed.    See https://minecraft.wiki/w/Java_Edition_protocol/Packets for the packet format."-  (write! [frame f] "Calls 'f' with the array and length of the frame."))+  (:import+   [io.netty.buffer ByteBuf ByteBufUtil Unpooled])) -(s/def ::frame #(satisfies? Frame %))+(defn frame [array]+  (tagged-literal `frame array)) -(defn frame-  ([array] (frame array (alength array)))-  ([array length]-   ^{:type ::frame}-   (reify Frame-     (write! [_ f] (f array length)))))+(defn frame? [x]+  (and (tagged-literal? x)+       (= `frame (:tag x)))) -(defmethod print-method ::frame [frame writer]-  (write! frame-          (fn [array length]-            (.write writer "#Frame ")-            (.write writer (pr-str (subvec (vec array) 0 length))))))+(defn array [frame]+  (if (frame? frame)+    (:form frame)+    (throw (ex-info "Tried to get the array with a non-frame." {:value frame}))))  (defn ->ByteBuf   "Returns a read-only ByteBuf containing the bytes of a frame."   [frame]-  (write! frame-          (fn [array length]-            (ByteBuf/.asReadOnly (Unpooled/wrappedBuffer array 0 length)))))+  (ByteBuf/.asReadOnly (Unpooled/wrappedBuffer (array frame))))  (defn ByteBuf->frame   [buf]