c3b3aa9
Branches > namespace-reorganization > c3b3aa9 > files / src / net / bowmap / network / registry.clj
Add docstrings and specs to client message ns
Commit: c3b3aa9948320d3db28f645f0dec0f7a28a7e70e
Parent: 53ac1a6
1 changed file:
src/net/bowmap/network/message/client.clj view file
@@ -5,8 +5,10 @@ [clojure.alpha.spec :as s] [dev.grahp.build :refer [build]] [dev.grahp.error :refer [adorn get!]]+ [dev.grahp.spec :as S] [net.bowmap.network.message :as msg] [net.bowmap.network.packet :as pk]+ [net.bowmap.network.packet.types :as t] [net.bowmap.network.packet.write :refer [boolean-> byte-> context-optional-> enum-> float-> identifier-> int-> json->@@ -18,7 +20,12 @@ (:import [io.netty.buffer ByteBuf Unpooled])) -; TODO docstrings!+(S/def ::registry (s/map-of ::state/connection-state (s/coll-of ::msg/id :type vector?)))+(S/def ::message->packet-ids (s/map-of (s/schema [::state/connection-state ::msg/id]) ::pk/id))+(S/def ::packets+ (s/map-of ::msg/id (s/fspec :args (s/cat :buf #(instance? ByteBuf %) :pk ::msg/message)+ :ret #(instance? ByteBuf %))))+ (defn build-packet-ids [registry] (build [[state packet-ids] registry [i id] (map-indexed vector packet-ids)]@@ -26,15 +33,21 @@ ::msg/id id} i])) -; TODO docstrings!-; And spec input?+(s/fdef build-packet-ids+ :args (s/cat :registry ::registry)+ :ret ::message->packet-ids)+ (defn build-packets [registry] (reduce (fn [acc [packet-id table]] (assoc acc packet-id (table-> table))) {} registry)) +(s/fdef build-packets+ :args (s/cat (s/map-of ::msg/id ::t/table))+ :ret ::packets)+ ; TODO docstring?-(def ^{:spec (s/map-of (s/schema [::state/connection-state ::msg/id]) ::pk/id)}+(def ^{:spec ::message->packet-ids} message->packet-ids ; Packet ids could map directly to numeric-ids and be stateless, ; but some packets have duplicates across connection states that you'd want to handle in the same way.@@ -213,8 +226,7 @@ ::clear-dialog ::show-dialog-play]})) -(def ^{:spec (s/map-of ::msg/id (s/fspec :args (s/cat :buf #(instance? ByteBuf %) :pk ::msg/message)- :ret #(instance? ByteBuf %)))}+(def ^{:spec ::packets} packets (build-packets {::status-response [:response "Response to serverbound ::status-request" json->]@@ -261,13 +273,10 @@ :value (or-> float-> 0)]})) (defn message->packet- "Encodes packet 'pk' into a ByteBuf of the packet bytes.- Like decoding, encoding a packet also requires the connection state, as- packet ids are not unique across connection state, so there isn't a- direct mapping of packet ids to numeric ids.- There are several packets (disconnect, keep alive) that are logically the- same across connection states, and so can be sent without needing to switch- on the connection state."+ "Encodes a message into a packet.+ Like decoding, encoding a packet requires the connection state, as packet+ ids are not unique across connection states. Several packets are logically+ the same across states, and can be sent without needing to switch on state." [msg state] (-> (let [encode-body (get! packets (::msg/id msg))] (length-prefixed (-> (Unpooled/buffer)