23ec393
Branches > main > 23ec393 > files
Rename codecs as type< and type> for readers and writers
Commit: 23ec393465f389ff19c8233860df79f305032655
Parent: 1ef4425
5 changed files:
src/net/bowmap/server/protocol/clientbound_packets.clj view file
@@ -7,8 +7,8 @@ (def clientbound-packets "Map of connection phases to pairs of [clientbound-packet-id writer]" {::phase/status- [::status-response status-response->- ::pong-response [::p/timestamp long->]]+ [::status-response status-response>+ ::pong-response [::p/timestamp long>]] ::phase/login [::disconnect nil ::encryption-request nilsrc/net/bowmap/server/protocol/codec.clj view file
@@ -18,14 +18,14 @@ (writers/to-writer schema) (throw (ex-info "No such packet with that id!" {:phase phase :numeric-id numeric-id})))] (-> (frame)- (writers/varint-> numeric-id)+ (writers/varint> numeric-id) (payload-writer packet)))) (defn decode-frame "Decodes a packet from a frame. Can throw if the frame does not contain a valid packet." [{:keys! [::phase/phase]} frame]- (let [numeric-id (readers/->varint frame)+ (let [numeric-id (readers/varint< frame) id (req! (serverbound/phase->numeric-id->id phase) numeric-id) schema (req! (serverbound/phase->id->schemas phase) id) payload-reader (readers/to-reader schema)]src/net/bowmap/server/protocol/readers.clj view file
@@ -1,7 +1,7 @@ (ns net.bowmap.server.protocol.readers "Functions for reading values from io.netty.ByteBufs. Buffers are assumed to be properly framed and contain exactly 1 packet.- Functions look like ->type, where 'type' is a Minecraft protocol type.+ Functions look like type<, where 'type' is a Minecraft protocol type. See https://minecraft.wiki/w/Java_Edition_protocol/Packets#Data_types" (:require [medley.core :refer [assoc-some]]@@ -10,16 +10,16 @@ [io.netty.buffer ByteBuf] [java.nio.charset Charset])) -(def ->byte ByteBuf/.readByte)-(def ->ubyte ByteBuf/.readUnsignedByte)-(def ->short ByteBuf/.readShort)-(def ->ushort ByteBuf/.readUnsignedShort)-(def ->int ByteBuf/.readInt)-(def ->long ByteBuf/.readLong)-(def ->float ByteBuf/.readFloat)-(def ->double ByteBuf/.readDouble)+(def byte< ByteBuf/.readByte)+(def ubyte< ByteBuf/.readUnsignedByte)+(def short< ByteBuf/.readShort)+(def ushort< ByteBuf/.readUnsignedShort)+(def int< ByteBuf/.readInt)+(def long< ByteBuf/.readLong)+(def float< ByteBuf/.readFloat)+(def double< ByteBuf/.readDouble) -(defn ->varint [buf]+(defn varint< [buf] (loop [num-read 0 result 0] (if (>= num-read max-varint-size)@@ -30,17 +30,17 @@ result' (recur (inc num-read) result')))))) -(defn ->string [buf]- (let [len (->varint buf)]+(defn string< [buf]+ (let [len (varint< buf)] (if (neg? len) (throw (Exception. "String length cannot be negative")) (ByteBuf/.readCharSequence buf len (Charset/forName "UTF-8"))))) -(defn ->enum [reader m]+(defn enum< [reader m] (fn [buf] (m (reader buf)))) -(defn ->table [schema]+(defn table< [schema] (fn [buf] (reduce (fn [acc [field reader]] (assoc-some acc field (reader buf)))@@ -52,6 +52,6 @@ Nil is coerced to the empty table reader." [reader] (cond- (or (vector? reader) (nil? reader)) (->table reader)+ (or (vector? reader) (nil? reader)) (table< reader) (fn? reader) reader :else (throw (IllegalArgumentException. (str "Cannot coerce into reader: " (pr-str reader))))))src/net/bowmap/server/protocol/serverbound_packets.clj view file
@@ -7,13 +7,13 @@ (def serverbound-packets "Map of connection phases to pairs of [serverbound-packet-id reader]" {::phase/handshaking- [::handshaking [:protocol ->varint- :address ->string- :port ->short- ::phase/phase (->enum ->varint {1 ::phase/status 2 ::phase/login 3 ::phase/transfer})]]+ [::handshaking [:protocol varint<+ :address string<+ :port short<+ ::phase/phase (enum< varint< {1 ::phase/status 2 ::phase/login 3 ::phase/transfer})]] ::phase/status [::status-request []- ::ping-request [::p/timestamp ->long]]+ ::ping-request [::p/timestamp long<]] ::phase/login [::login-start [] ::encryption-response []src/net/bowmap/server/protocol/writers.clj view file
@@ -1,6 +1,6 @@ (ns net.bowmap.server.protocol.writers "Functions for writing values into io.netty.ByteBufs.- Functions look like type->, where 'type' is a Minecraft protocol type.+ Functions look like type>, where 'type' is a Minecraft protocol type. See https://minecraft.wiki/w/Java_Edition_protocol/Packets#Data_types" (:require [clojure.data.json :as json]@@ -9,16 +9,16 @@ [io.netty.buffer ByteBuf] [java.nio.charset Charset])) -(def byte-> ByteBuf/.writeByte)-(def ubyte-> ByteBuf/.writeByte)-(def short-> ByteBuf/.writeShort)-(def ushort-> ByteBuf/.writeShort)-(def int-> ByteBuf/.writeInt)-(def long-> ByteBuf/.writeLong)-(def float-> ByteBuf/.writeFloat)-(def double-> ByteBuf/.writeDouble)+(def byte> ByteBuf/.writeByte)+(def ubyte> ByteBuf/.writeByte)+(def short> ByteBuf/.writeShort)+(def ushort> ByteBuf/.writeShort)+(def int> ByteBuf/.writeInt)+(def long> ByteBuf/.writeLong)+(def float> ByteBuf/.writeFloat)+(def double> ByteBuf/.writeDouble) -(defn varint-> [buf n]+(defn varint> [buf n] (loop [buf buf n n] (let [b (bit-and n 0x7F)@@ -28,25 +28,25 @@ (recur (ByteBuf/.writeByte buf (bit-or b 0x80)) nxt))))) -(defn string-> [buf s]+(defn string> [buf s] {:pre [(string? s)]} (doto buf- (varint-> (count s))+ (varint> (count s)) (ByteBuf/.writeCharSequence s (Charset/forName "UTF-8")))) -(defn json-> [buf s]- (string-> buf (json/write-str s)))+(defn json> [buf s]+ (string> buf (json/write-str s))) -(defn status-response-> [buf {::p/keys! [version-name protocol]+(defn status-response> [buf {::p/keys! [version-name protocol] ::p/keys [motd max-player-count online-player-count]}]- (json-> buf+ (json> buf (cond-> {:version {:name version-name :protocol protocol}} motd (assoc :description motd) (and max-player-count online-player-count) (assoc :players {:max max-player-count :online online-player-count})))) -(defn table-> [schema]+(defn table> [schema] (fn [buf m] (reduce (fn [acc [field writer]] (writer acc (get m field)))@@ -58,6 +58,6 @@ Nil is not a valid writer." [writer] (cond- (vector? writer) (table-> writer)+ (vector? writer) (table> writer) (fn? writer) writer :else (throw (IllegalArgumentException. (str "Cannot coerce into writer: " (pr-str writer))))))