latest
Branches > main > latest > files / src
Rewrite server promise logic
Commit: e95c5137225df03b183e3c14bc2e9f69510f3afd
Parent: 599e72a
2 changed files:
src/net/bowmap/network/minecraft_server.clj view file
@@ -9,8 +9,7 @@ [net.bowmap.network.protocol.message.clientbound :as clientbound] [net.bowmap.network.protocol.message.serverbound :as serverbound] [net.bowmap.network.protocol.state :as state]- [net.bowmap.network.server :as server]- [net.bowmap.server.step :as step]))+ [net.bowmap.network.server :as server])) (defn handle-incoming-frame [client frame outgoing-messages] (let [message (serverbound/frame->message frame (req! client ::state/connection-state))@@ -51,33 +50,22 @@ client (assoc client ::state/connection-state ::state/handshaking)] (io-thread (loop [client client]- (recur (handle-client!! client outgoing-messages))))- nil))--(defn closing-promise- "Returns a promise that closes 'ch' when it is resolved."- [ch]- (let [p (promise)]- (io-thread- @p- (close! ch))- p))+ (recur (handle-client!! client outgoing-messages)))))) (defn start-minecraft-server!- "Starts the Minecraft server.- Returns a promise that will deliver when the server closes.- Delivering the promise will also close the server."+ "Starts the Minecraft server on a deamon thread.+ Returns a promise that will deliver when the server closes,+ and will close the server when delivered." []- (let [clients (server/start-tcp-server! {:port 25565})- shutdown (closing-promise clients)]- (io-thread+ (let [server (server/start-tcp-server! :port 25565)+ shutdown (promise)]+ (future (loop []- (if-some [client (<!! clients)]+ (if-some [client (<!! server)] (do (connect! client) (recur)) (deliver shutdown :shutdown))))- #_(io-thread- (loop [model model]- (let [[model' effects] (step/step model fact)])- (recur)))+ (io-thread+ @shutdown+ (close! server)) shutdown))src/net/bowmap/network/server.clj view file
@@ -27,6 +27,7 @@ {::incoming-frames incoming-frames ::outgoing-frames outgoing-frames})) +; TODO should return the actual server... (defn start-tcp-server! "Starts a TCP server at the given port. Returns a channel of clients as they connect. When the server closes, client channel will be closed, and vice versa."