125ac6e
Branches > main > 125ac6e > files / src / net / bowmap
Don't length prefix frames when encoding packets
Commit: 125ac6e31e01d28ea09e7421cd8b75c4813897bb
Parent: acb3926
3 changed files:
src/net/bowmap/server/network/streams.clj view file
@@ -47,6 +47,17 @@ (recur (+ offset r)))))) (frame arr))) +(defn write-varint!!+ "Blocks and writes a varint to an OutputStream."+ [output-stream n]+ (loop [n n]+ (let [b (bit-and n 0x7F)+ nxt (unsigned-bit-shift-right n 7)]+ (if (zero? nxt)+ (OutputStream/.write output-stream b)+ (do (OutputStream/.write output-stream (bit-or b 0x80))+ (recur nxt))))))+ (defn write-frame!! "Blocks and writes a frame to an OutputStream. Throws if the frame cannot be written."@@ -54,5 +65,6 @@ (let [len (ByteBuf/.readableBytes frame) arr (byte-array len)] (ByteBuf/.readBytes frame arr)+ (write-varint!! output-stream len) (OutputStream/.write output-stream arr) (OutputStream/.flush output-stream)))src/net/bowmap/server/protocol/codec.clj view file
@@ -1,7 +1,7 @@ (ns net.bowmap.server.protocol.codec (:require [net.bowmap.server.protocol.clientbound-packets :refer [clientbound-packets]]- [net.bowmap.server.protocol.frame :refer [frame length-prefixed]]+ [net.bowmap.server.protocol.frame :refer [frame]] [net.bowmap.server.protocol.packet :as packet] [net.bowmap.server.protocol.phase :as phase] [net.bowmap.server.protocol.readers :as readers]@@ -27,10 +27,9 @@ payload-writer (if (some? schema) (writers/to-writer schema) (throw (ex-info "No such packet with that id!" {:phase phase :numeric-id numeric-id})))]- (length-prefixed- (-> (frame)- (writers/varint-> numeric-id)- (payload-writer packet)))))+ (-> (frame)+ (writers/varint-> numeric-id)+ (payload-writer packet)))) (defn decode-frame "Decodes a packet from a frame.src/net/bowmap/server/protocol/frame.clj view file
@@ -2,11 +2,9 @@ "A framed byte buffer containing one serialized Minecraft protocol packet. See https://minecraft.wiki/w/Java_Edition_protocol/Packets for the format." (:require- [clojure.spec.alpha :as s]- [net.bowmap.server.protocol.types :refer [max-varint-size]]- [net.bowmap.server.protocol.writers :refer [varint->]])+ [clojure.spec.alpha :as s]) (:import- [io.netty.buffer ByteBuf CompositeByteBuf Unpooled]))+ [io.netty.buffer ByteBuf Unpooled])) (s/def ::frame #(instance? ByteBuf %)) @@ -14,16 +12,3 @@ "Returns an empty frame, or a frame containing the bytes of an array." ([] (Unpooled/buffer)) ([arr] (Unpooled/wrappedBuffer arr)))--(defn- composite-byte-buf- "Returns a CompositeByteBuf containing 'bufs'."- [& bufs]- (-> (Unpooled/compositeBuffer (count bufs))- (CompositeByteBuf/.addComponents true bufs)))--(defn length-prefixed- "Returns a frame prefixed by the the varint length of the given frame."- [frame]- (composite-byte-buf- (varint-> (Unpooled/buffer max-varint-size) (ByteBuf/.writerIndex frame))- frame))