242aed7

Branches > main > 242aed7 > files / deps.edn

Unbundle client agent from packet handling code

Grahp, 2026-08-19

Commit: 242aed74e25353ca132a26cb5e11ec08143ed821
Parent: b557414

3 changed files:

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

@@ -56,8 +56,7 @@ (defn send-clientbound-packet [client packet]   (println "sending packet out: " packet)   (write-frame!! (encode-packet! packet (req! client ::state/connection-state))-                 (Socket/.getOutputStream (req! client ::socket)))-  client)+                 (Socket/.getOutputStream (req! client ::socket))))  (defn client [socket]   (agent {::socket socket

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

@@ -12,20 +12,20 @@ ; Handles incoming packets from clients  (defmulti handler-  (fn [client client-agent packet]+  (fn [client packet]     (::pk/id packet))) -(defmethod handler :default [client a pk]+(defmethod handler :default [client pk]   (prn "Unhandled packet:" pk)   client) -(defmethod handler ::serverbound/handshaking [client _ pk]+(defmethod handler ::serverbound/handshaking [client pk]   (assoc client          ::state/connection-state (req! pk :intent)          :protocol-id (req! pk :protocol))) -(defmethod handler ::serverbound/status-request [client a _]-  (send a client/send-clientbound-packet+(defmethod handler ::serverbound/status-request [client _]+  (client/send-clientbound-packet client         {::pk/id ::clientbound/status-response          ;; Should be un-bundled yeah.          :response {:version {:name "26.2"@@ -37,40 +37,44 @@                     :enforcesSecureChat false}})   client) -(defmethod handler ::serverbound/ping-request [client a pk]-  (send a client/send-clientbound-packet-        {::pk/id ::clientbound/pong-response-         :timestamp (:timestamp pk)})+(defmethod handler ::serverbound/ping-request [client pk]+  (client/send-clientbound-packet+    client+    {::pk/id ::clientbound/pong-response+     :timestamp (:timestamp pk)})   client) -(defmethod handler ::serverbound/login-start [client a pk]+(defmethod handler ::serverbound/login-start [client pk]   (let [{:keys! [uuid username]} pk]-    (send a client/send-clientbound-packet-          {::pk/id ::clientbound/login-success-           :profile {:uuid uuid-                     :username username-                     :properties []}-           :session-id (random-uuid)}))+    (client/send-clientbound-packet+      client+      {::pk/id ::clientbound/login-success+       :profile {:uuid uuid+                 :username username+                 :properties []}+       :session-id (random-uuid)}))   client) -(defmethod handler ::serverbound/login-acknowledged [client a pk]-  (send a client/send-clientbound-packet-        {::pk/id ::clientbound/known-packs-         ;; TODO also probably good to unbundle this-         :known-packs [{:namespace "minecraft"-                        :id "core"-                        :version "26.2"}]})-  (assoc client ::state/connection-state ::state/config))+(defmethod handler ::serverbound/login-acknowledged [client _]+  (let [client (assoc client ::state/connection-state ::state/config)]+    (client/send-clientbound-packet+      client+      {::pk/id ::clientbound/known-packs+       ;; TODO also probably good to unbundle this+       :known-packs [{:namespace "minecraft"+                      :id "core"+                      :version "26.2"}]})+    client)) -(defmethod handler ::serverbound/known-packs [client a pk]+(defmethod handler ::serverbound/known-packs [client _]   (doseq [pk (registry/registry-packets)]-    (send a client/send-clientbound-packet pk))+    (client/send-clientbound-packet client pk)) -  (send a client/send-clientbound-packet {::pk/id ::clientbound/update-tags+  (client/send-clientbound-packet client {::pk/id ::clientbound/update-tags                                           :tagged-registries [{:registry "minecraft:block"                                                                :tags [{:tag-name "minecraft:infiniburn_overworld"                                                                        :entries []}]}]})-  (send a client/send-clientbound-packet {::pk/id ::clientbound/update-tags+  (client/send-clientbound-packet client {::pk/id ::clientbound/update-tags                                           :tagged-registries [{:registry "minecraft:damage_type"                                                                :tags [{:tag-name "minecraft:is_fire"                                                                        :entries []}@@ -78,7 +82,7 @@                                                                        :entries []}                                                                       {:tag-name "minecraft:bypasses_shield"                                                                        :entries []}]}]})-  (send a client/send-clientbound-packet {::pk/id ::clientbound/update-tags+  (client/send-clientbound-packet client {::pk/id ::clientbound/update-tags                                           :tagged-registries [{:registry "minecraft:banner_pattern"                                                                :tags [{:tag-name "minecraft:pattern_item/bordure_indented" :entries []}                                                                       {:tag-name "minecraft:pattern_item/creeper" :entries []}@@ -92,7 +96,7 @@                                                                       {:tag-name "minecraft:pattern_item/skull" :entries []}]}]})  -  (send a client/send-clientbound-packet {::pk/id ::clientbound/finish-config})+  (client/send-clientbound-packet client {::pk/id ::clientbound/finish-config})   client)  (defn- build-chunk-packets []@@ -118,64 +122,55 @@              :chunk-x x              :chunk-z z)))) -(defmethod handler ::serverbound/finish-config [client a pk]-  (send a client/send-clientbound-packet-        {::pk/id ::clientbound/login-         ;; TODO magic one :p-         ;; I think you can keep a registry of entity ids and stick this on the client-         :entity-id 1-         :hardcore? false-         :dimension-names ["world"]-         :max-players 1000 ; TODO extract :p-         :view-distance 32-         :simulation-distance 32-         :reduced-debug-info? false-         :enable-respawn-screen? false-         :do-limited-crafting? false-         :dimension-type-id 0-         :dimension-name "world"-         :hashed-seed 203894-         :gamemode ::gamemode/adventure-         :debug-world? false-         :superflat-world? true-         :has-death-location? false-         :portal-cooldown 10-         :sea-level 60-         :online-mode? false-         :enforce-secure-chat? true})+(defmethod handler ::serverbound/finish-config [client _]+  (let [client (assoc client ::state/connection-state ::state/play)]+    (client/send-clientbound-packet client+                                    {::pk/id ::clientbound/login+                                     ;; TODO magic one :p+                                     ;; I think you can keep a registry of entity ids and stick this on the client+                                     :entity-id 1+                                     :hardcore? false+                                     :dimension-names ["world"]+                                     :max-players 1000 ; TODO extract :p+                                     :view-distance 32+                                     :simulation-distance 32+                                     :reduced-debug-info? false+                                     :enable-respawn-screen? false+                                     :do-limited-crafting? false+                                     :dimension-type-id 0+                                     :dimension-name "world"+                                     :hashed-seed 203894+                                     :gamemode ::gamemode/adventure+                                     :debug-world? false+                                     :superflat-world? true+                                     :has-death-location? false+                                     :portal-cooldown 10+                                     :sea-level 60+                                     :online-mode? false+                                     :enforce-secure-chat? true}) -  (send a client/send-clientbound-packet-        {::pk/id ::clientbound/game-event-         :event :wait-for-chunks})-  (doseq [pk (build-chunk-packets)]-    (send a client/send-clientbound-packet pk))-  (send a client/send-clientbound-packet-        {::pk/id ::clientbound/synchronize-player-position-         :teleport-id (rand-int Integer/MAX_VALUE)-         :x 0-         :y 16-         :z 0-         :velocity-x 0-         :velocity-y 0-         :velocity-z 0-         :yaw 0-         :pitch 0-         :flags #{}})-  (assoc client ::state/connection-state ::state/play))+    (client/send-clientbound-packet client+                                    {::pk/id ::clientbound/game-event+                                     :event :wait-for-chunks})+    (doseq [pk (build-chunk-packets)]+      (client/send-clientbound-packet client pk))+    (client/send-clientbound-packet client+                                    {::pk/id ::clientbound/synchronize-player-position+                                     :teleport-id (rand-int Integer/MAX_VALUE)+                                     :x 0+                                     :y 16+                                     :z 0+                                     :velocity-x 0+                                     :velocity-y 0+                                     :velocity-z 0+                                     :yaw 0+                                     :pitch 0+                                     :flags #{}})+    client))  ; TODO keepalive -; Should probably return the packets to be sent out?-; I don't liek that the agent is in here.-(defn handle-packet-  "Handles a severbound packet from a client. Returns the new client state"-  [client client-agent packet]-  (println packet)-  (handler client client-agent packet))--(defn process-serverbound-packet [client client-agent frame]+(defn process-serverbound-frame [client frame]   (let [packet (decode-packet! frame (req! client ::state/connection-state))]-    (handle-packet client client-agent packet)))--(defn process-incoming-frame! [client-agent frame]-  (send client-agent process-serverbound-packet client-agent frame))+    ; TODO make handler return packets and send them out here+    (handler client packet)))

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

@@ -17,7 +17,7 @@           (try             (loop []               (let [frame (client/read-frame!! input-stream)]-                (handle/process-incoming-frame! client-agent frame))+                (send client-agent handle/process-serverbound-frame frame))               (recur))             (catch EOFException _)             (catch SocketException _)