068f95e
Branches > main > 068f95e > files
Use Server protocol rather than channel
Commit: 068f95ed08b7a768454f8cdd4d83dd4955c34693
Parent: 30cd6b9
2 changed files:
src/net/bowmap/server/network/server.clj view file
@@ -1,22 +1,25 @@ (ns net.bowmap.server.network.server (:require- [clojure.core.async :refer [>!! chan close! io-thread]]+ [clojure.spec.alpha :as s] [net.bowmap.server.network.connection :as connection]) (:import [java.net ServerSocket])) +(defprotocol Server+ (accept-connection!! [server]+ "Accepts and returns a Connection from the server.+ Blocks until the connection is ready.+ Throws if a connection cannot be received from the server."))++(s/def ::server #(satisfies? Server %))+ (defn start-server!- "Starts a Minecraft server at the supplied port and bind address.- Returns a channel of connections that is coupled to the server's lifecycle." [& {:keys [port bind-addr backlog] :or {:backlog 50 :port 0}}]- (let [server-socket (ServerSocket. port backlog bind-addr)- connection-chan (chan backlog)]- (io-thread- (try- (loop []- (when (>!! connection-chan (connection/connection (ServerSocket/.accept server-socket)))- (recur)))- (finally- (close! connection-chan)- (ServerSocket/.close server-socket))))- connection-chan))+ (let [server-socket (ServerSocket. port backlog bind-addr)]+ (reify Server+ (accept-connection!! [_]+ (try+ (connection/connection (ServerSocket/.accept server-socket))+ (catch Throwable t+ (ServerSocket/.close server-socket)+ (throw t)))))))src/net/bowmap/server/runtime/connections.clj view file
@@ -2,6 +2,7 @@ (:require [clojure.core.async :refer [<!! alts!! chan io-thread put!]] [net.bowmap.server.network.connection :as connection]+ [net.bowmap.server.network.server :as server] [net.bowmap.server.protocol.client :as client] [net.bowmap.server.protocol.packet-serialization :as serialize] [net.bowmap.server.protocol.phase :as phase]@@ -37,10 +38,7 @@ (recur client')))))))) (defn handle-connections!- "Handles connections from a channel of connections.- Blocks until the channel closes."- [connection-chan]+ [server] (loop []- (when-some [connection (<!! connection-chan)]- (handle-connection! connection)- (recur))))+ (handle-connection! (server/accept-connection!! server))+ (recur)))