160d258
Branches > main > 160d258 > files / src / net / bowmap
Catch and disconnect clients on error
Commit: 160d2581d747bc07513d15f2763d379bfdb97b8a
Parent: 21b5507
3 changed files:
src/net/bowmap/network/connection.clj view file
@@ -22,3 +22,6 @@ (defn send-frame!! [frame connection] (streams/write-frame!! frame (req! connection ::output-stream)))++(defn close! [connection]+ (Socket/.close (::socket connection)))src/net/bowmap/network/minecraft_server.clj view file
@@ -3,7 +3,8 @@ [clojure.core.async :refer [>!! chan close! io-thread]] [net.bowmap.network.connection :as connection]) (:import- [java.net ServerSocket]))+ [java.io EOFException]+ [java.net ServerSocket SocketException])) (defonce server (atom nil)) @@ -26,6 +27,7 @@ (loop [] (>!! serverbound-frames (connection/read-frame!! connection)) (recur))+ (catch EOFException _) (finally (close! serverbound-frames)))) (>!! connections connection))src/net/bowmap/network/server.clj view file
@@ -8,27 +8,28 @@ [net.bowmap.network.packets.serverbound :refer [decode-packet!]] [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)- clientbound-packets (req! client ::client/clientbound-packets)- serverbound-frames (req! connection ::connection/serverbound-frames)]- (loop [client client]- (recur- (alt!!- serverbound-frames ([frame]- (if (some? frame)- (handle/handle!! client (decode-packet! frame (::state/connection-state client)))- ; TODO should you disconnect the client when this happens?- (throw (Exception. "AAAAH"))))- clientbound-packets ([pk]- (if (some? pk)- ; TODO Can't sending packets change the client state?- (do (send-packet!! client pk)- client)- (throw (Exception. "Disconnect, eventually"))))))))))+ (try+ (let [client (client/client connection)+ clientbound-packets (req! client ::client/clientbound-packets)+ serverbound-frames (req! connection ::connection/serverbound-frames)]+ (loop [client client]+ (recur+ (alt!!+ serverbound-frames ([frame]+ (if (some? frame)+ (handle/handle!! client (decode-packet! frame (::state/connection-state client)))+ (throw (Exception. "Reached end of client socket"))))+ clientbound-packets ([pk]+ (if (some? pk)+ (do (send-packet!! client pk)+ client)+ (throw (Exception. "Clientbound packets was closed idk why"))))))))+ (catch Exception _)+ (finally+ ; TODO Send player disconnected fact, and update the player list from that+ (connection/close! connection))))) (defn start-server! [port] (let [connections (mc/start-server! port)]