359397c

Branches > main > 359397c > files

Keep track of clientbound packet channels on clients based on their uuid

Grahp, 2026-08-24

Commit: 359397c46726ddcba7319a9b1a2cc8ff11dc28f1
Parent: 4b378ce

3 changed files:

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

@@ -140,8 +140,7 @@                          :velocity-z 0                          :yaw 0                          :pitch 0-                         :flags #{}}))-     :keep-alive? true}))+                         :flags #{}}))}))  (defn handle-player-update-movement-packet [{::player/keys! [uuid]}                                             {:keys [& ::position/x ::position/y ::position/z ::position/yaw ::position/pitch]

src/net/bowmap/protocol/keepalive.clj deleted (view file)

@@ -1,29 +0,0 @@-(ns net.bowmap.protocol.keepalive-  (:require-   [clojure.core.async :refer [<!! io-thread put! timeout]]-   [net.bowmap.protocol.client :as client]-   [net.bowmap.protocol.packet :as pk]-   [net.bowmap.protocol.packet.clientbound :as clientbound]))--; TODO this takes a snapshot of the clients value...-(defonce keepalive-clients (ref #{}))-(defonce running? (atom false))--(defn keep-alive! [client]-  (dosync-    (commute keepalive-clients conj client)))--(defn start-keep-alives! []-  (when-not @running?-    (reset! running? true)-    (io-thread-      (loop []-        (<!! (timeout 20000))-        (doseq [{::client/keys [clientbound-packets] :as client} @keepalive-clients]-          (put! clientbound-packets {::pk/id ::clientbound/keep-alive-                                     :id (rand-int Integer/MAX_VALUE)})-          (fn [result]-            (when-not result-              (dosync-                (commute keepalive-clients disj client)))))-        (recur)))))

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

@@ -1,9 +1,9 @@ (ns net.bowmap.protocol.server   (:require-   [clojure.core.async :refer [<!! >!! alt!! io-thread put!]]+   [clojure.core.async :refer [<!! >!! alt!! io-thread put! timeout]]    [net.bowmap.protocol.client :as client]    [net.bowmap.protocol.handle :as handle]-   [net.bowmap.protocol.keepalive :as keepalive]+   [net.bowmap.protocol.packet :as pk]    [net.bowmap.protocol.packet.clientbound :as clientbound :refer [encode-packet!]]    [net.bowmap.protocol.packet.serverbound :refer [decode-packet!]]    [net.bowmap.protocol.state :as state]@@ -14,20 +14,29 @@    [net.bowmap.transport.connection :as connection]    [net.bowmap.transport.server :as mc])) +; Map of uuid -> clientbound packet channel+(defonce client-packets (atom {}))+(defonce keepalive-clients (atom #{}))++(defn send-packet! [uuid packet]+  (put! (req! @client-packets uuid) packet))+ (defn adorn-effects-with-client [client events]   (mapv #(assoc % ::client/client client) events))  (defn handle-serverbound-frame [client frame]   (let [{:keys! [::state/connection-state ::client/clientbound-packets]} client+        was-playing? (::client/playing? client)         packet (decode-packet! frame connection-state)-        {:keys [client packets events keep-alive?] :or {:client client}} (handle/interpret-packet client packet)]-    ; TODO probably want to clean up this side effecting stuff+        {:keys [client packets events] :or {:client client}} (handle/interpret-packet client packet)]     (doseq [pk packets]       (put! clientbound-packets pk))     (doseq [event events]       (execute-effects! (adorn-effects-with-client client (logic/handle-events event))))-    (when keep-alive?-      (keepalive/keep-alive! client))+    (when (and (not was-playing?) (::client/playing? client))+      (let [{::player/keys! [uuid]} client]+        (swap! client-packets assoc uuid clientbound-packets)+        (swap! keepalive-clients conj uuid)))     client))  (defn client-lifecycle@@ -61,6 +70,7 @@           {::connection/keys! [close-fn]} connection           client (client-lifecycle client)]       (when-let [uuid (::player/uuid client)]+        (swap! keepalive-clients disj uuid)         (execute-effects! (adorn-effects-with-client client                                                      (logic/handle-events {::event/id ::event/player-left                                                                            ::player/uuid uuid}))))@@ -68,7 +78,13 @@  (defn start-server! [port]   (let [connections (mc/start-server! port)]-    (keepalive/start-keep-alives!)+    (io-thread+      (loop []+        (<!! (timeout 20000))+        (doseq [uuid @keepalive-clients]+          (send-packet! uuid {::pk/id ::clientbound/keep-alive+                              :id (rand-int Integer/MAX_VALUE)}))+        (recur)))     (loop []       (when-let [connection (<!! connections)]         (handle-connection connection))