b3b1618

Branches > client-handling-rewrite > b3b1618 > files

Move instance to depend on network server, not the other way around

Grahp, 2026-06-02

Commit: b3b16186aa0a2cf672843bd91d5cdfc189937002
Parent: 67c4f1a

3 changed files:

src/net/bowmap/core.clj modified (view file)

@@ -1,9 +1,9 @@ (ns net.bowmap.core   (:require-   [net.bowmap.network.server :as server]))+   [net.bowmap.instance :as instance]))  (defn start-server []-  (server/start-server! {:port 25565}))+  (instance/start-server! {:port 25565}))  (defn -main [& _args]   (start-server)

src/net/bowmap/instance.clj modified (view file)

@@ -1,3 +1,4 @@+; Could be called minecraft-server? (ns net.bowmap.instance   (:require    [clojure.core.async :refer [<!! >!! alt!! chan io-thread put! timeout]]@@ -6,6 +7,7 @@    [net.bowmap.network.message :as msg]    [net.bowmap.network.message.client :as cl]    [net.bowmap.network.message.server :as srv]+   [net.bowmap.network.server :as server]    [net.bowmap.network.state :as state]))  ; The logical server, holds a set of clients connected@@ -14,17 +16,7 @@ (defonce instance (atom {::clients {}                          ::message-out-channels {}})) -(defn start! [server]-  (swap! instance assoc ::server server)-  server)--(defn client-  "Returns a new client value"-  []-  ; TODO this state should be server-wide, I think.-  {::state/connection-state ::state/handshaking-   ::client/id (random-uuid)})-+(defonce instance2 (atom {::conections {}}))  (defn send-message! [client msg]   ; TODO is put! okay?@@ -63,6 +55,7 @@  (defn connect! [client packets-in packets-out]   (let [messages-out (chan)]+    ; TODO error handling and disconnect     (io-thread       (loop [client client]         (when-some [client (handle-packets-and-messages client packets-in messages-out packets-out)]@@ -72,3 +65,21 @@     (swap! instance update ::clients assoc (::client/id client) client)     (swap! instance update ::message-out-channels assoc (::client/id client) messages-out)     client))++(defn client+  "Returns a new client value"+  []+  ; TODO this state should be server-wide, I think.+  ; ??? What is he on about ^+  {::state/connection-state ::state/handshaking+   ::client/id (random-uuid)})++(defn handle-client [packets-in packets-out]+  (let [client (client)]+    (connect! client packets-in packets-out)))++; TODO rename...+(defn start-server! [{:keys [port]}]+  (server/start-server! {:port port+                         :handler #'handle-client})+  nil)

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

@@ -3,24 +3,22 @@    [aleph.tcp :as tcp]    [clojure.core.async :refer [chan]]    [manifold.stream :as s]-   [net.bowmap.instance :as instance]    [net.bowmap.network.packet.frame :as frame])   (:import    [io.netty.bootstrap ServerBootstrap]    [io.netty.channel ChannelOption])) -(defn- handle-client [stream info]+(defn- handle-client [handler stream info]   (let [packets-in (chan 1 frame/packet-framer-xf)-        packets-out (chan)-        client (instance/client)]+        packets-out (chan)]     (s/connect stream packets-in)     (s/connect packets-out stream)-    (instance/connect! client packets-in packets-out)))+    (handler packets-in packets-out)))  (defn start-server!   "Starts a Minecraft server at the port, passing clients into the instance."-  [& {:keys [port]}]-  (instance/start!-    (tcp/start-server handle-client-                      {:port port-                       :bootstrap-transform #(ServerBootstrap/.childOption % ChannelOption/TCP_NODELAY true)})))+  [& {:keys [port handler]}]+  (tcp/start-server (fn [stream info]+                      (handle-client handler stream info))+                    {:port port+                     :bootstrap-transform #(ServerBootstrap/.childOption % ChannelOption/TCP_NODELAY true)}))