21b5507

Branches > main > 21b5507 > files / src / net / bowmap / server

Move connection stuff to a separate namespace

Grahp, 2026-08-21

Commit: 21b55070705f051dea40a8bd81d4563d4175f849
Parent: e359674

3 changed files:

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

@@ -1,8 +1,9 @@ (ns net.bowmap.network.connection   (:require+   [clojure.core.async :refer [chan]]    [net.bowmap.network.streams :as streams])   (:import-   [java.net ServerSocket Socket]))+   [java.net Socket]))  ; Connection can be sent and received frames? ; It is the I/O part of the client@@ -13,13 +14,11 @@ (defn connection [socket]   {::socket socket    ::input-stream (Socket/.getInputStream socket)-   ::output-stream (Socket/.getOutputStream socket)})+   ::output-stream (Socket/.getOutputStream socket)+   ::serverbound-frames (chan 64)})  (defn read-frame!! [connection]   (streams/read-frame!! (req! connection ::input-stream)))  (defn send-frame!! [frame connection]   (streams/write-frame!! frame (req! connection ::output-stream)))--(defn accept-connection!! [server-socket]-  (connection (ServerSocket/.accept server-socket)))

src/net/bowmap/network/minecraft_server.clj added (view file)

@@ -0,0 +1,35 @@+(ns net.bowmap.network.minecraft-server+  (:require+   [clojure.core.async :refer [>!! chan close! io-thread]]+   [net.bowmap.network.connection :as connection])+  (:import+   [java.net ServerSocket]))++(defonce server (atom nil))++(defn accept-connection!! [server-socket]+  (connection/connection (ServerSocket/.accept server-socket)))++(defn start-server! [port]+  (when-let [server @server]+    (ServerSocket/.close server))+  (let [connections (chan 64)+        server-socket (ServerSocket. port)]+    (reset! server server-socket)+    (io-thread+      (loop []+        ; TODO close the channel when the server closes+        (let [connection (accept-connection!! server-socket)+              {:keys! [::connection/serverbound-frames]} connection]+          (io-thread+            (try+              (loop []+                (>!! serverbound-frames (connection/read-frame!! connection))+                (recur))+              (finally+                (close! serverbound-frames))))+          (>!! connections connection))+        (recur)))+    connections))++

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

@@ -1,30 +1,20 @@ (ns net.bowmap.network.server   (:require-   [clojure.core.async :refer [>!! alt!! chan close! io-thread]]+   [clojure.core.async :refer [<!! alt!! io-thread]]    [net.bowmap.network.client :as client :refer [send-packet!!]]    [net.bowmap.network.connection :as connection]    [net.bowmap.network.handle :as handle]+   [net.bowmap.network.minecraft-server :as mc]    [net.bowmap.network.packets.serverbound :refer [decode-packet!]]-   [net.bowmap.network.state :as state])-  (:import-   [java.net ServerSocket]))--(defonce server (atom nil))+   [net.bowmap.network.state :as state]))  ; TODO handle errors and disconnect player ; Send player disconnected fact, and update the player list from that (defn handle-connection [connection]   (io-thread     (let [client (client/client connection)-          serverbound-frames (chan 64)-          clientbound-packets (req! client ::client/clientbound-packets)]-      (io-thread-        (try-          (loop []-            (>!! serverbound-frames (connection/read-frame!! (::connection/connection client)))-            (recur))-          (finally-            (close! serverbound-frames))))+          clientbound-packets (req! client ::client/clientbound-packets)+          serverbound-frames (req! connection ::connection/serverbound-frames)]       (loop [client client]         (recur           (alt!!@@ -41,12 +31,8 @@                                    (throw (Exception. "Disconnect, eventually"))))))))))  (defn start-server! [port]-  (when-let [server @server]-    (ServerSocket/.close server))-  (let [server-socket (ServerSocket. port)]-    (reset! server server-socket)+  (let [connections (mc/start-server! port)]     (loop []-      ; TODO can error, not sure what should happen-      (let [connection (connection/accept-connection!! server-socket)]+      (when-let [connection (<!! connections)]         (handle-connection connection))       (recur))))