frame.clj

Bowmap · bowmap.git · Tickets

Branches > main > e81f338 > files / src / net / bowmap / server / packet / frame.clj

(ns net.bowmap.server.packet.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."
  (:import
   [io.netty.buffer ByteBuf ByteBufUtil Unpooled]))

(defn frame [array]
  (tagged-literal `frame array))

(defn frame? [x]
  (and (tagged-literal? x)
       (= `frame (:tag x))))

(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]
  (ByteBuf/.asReadOnly (Unpooled/wrappedBuffer (array frame))))

(defn ByteBuf->frame
  [buf]
  (frame (ByteBufUtil/getBytes buf)))