bf7e6f7
Branches > facts > bf7e6f7 > files / src / net / bowmap
Make connection an interface of clientbound and serverbound frames
Commit: bf7e6f7b8919429e8a95779d096a7bb1d6e7ddad
Parent: aa17513
5 changed files:
src/net/bowmap/network/client.clj view file
@@ -1,6 +1,6 @@ (ns net.bowmap.network.client (:require- [clojure.core.async :refer [chan]]+ [clojure.core.async :refer [>!! chan]] [clojure.spec.alpha :as s] [net.bowmap.network.connection :as connection] [net.bowmap.network.packets.clientbound :refer [encode-packet!]]@@ -16,5 +16,6 @@ (defn send-packet!! [client packet] (let [{:keys! [::connection/connection ::state/connection-state]} client+ {:keys! [::connection/clientbound-frames]} connection frame (encode-packet! packet connection-state)]- (connection/send-frame!! frame connection)))+ (>!! clientbound-frames frame)))src/net/bowmap/network/connection.clj view file
@@ -1,25 +1,39 @@ (ns net.bowmap.network.connection (:require- [clojure.core.async :refer [chan]]+ [clojure.core.async :refer [<!! >!! chan io-thread close!]] [net.bowmap.network.streams :as streams]) (:import- [java.net Socket]))+ [java.io EOFException]+ [java.net Socket SocketException])) ; Connection can be sent and received frames? ; It is the I/O part of the client ; Clients are then I/O free (defn connection [socket]- {::socket socket- ::input-stream (Socket/.getInputStream 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 close! [connection]- (Socket/.close (::socket connection)))+ (let [serverbound-frames (chan 64)+ clientbound-frames (chan 64)+ input-stream (Socket/.getInputStream socket)+ output-stream (Socket/.getOutputStream socket)+ conn {::close-fn (fn [] (Socket/.close socket))+ ::serverbound-frames serverbound-frames+ ::clientbound-frames clientbound-frames}]+ (io-thread+ (try+ (loop []+ (>!! serverbound-frames (streams/read-frame!! input-stream))+ (recur))+ (catch EOFException _)+ (catch SocketException _)+ (finally+ (close! serverbound-frames)+ (Socket/.close socket))))+ (io-thread+ (try+ (loop []+ (when-some [frame (<!! clientbound-frames)]+ (streams/write-frame!! frame output-stream)+ (recur)))+ (finally+ (close! clientbound-frames))))+ conn))src/net/bowmap/network/minecraft_server.clj view file
@@ -1,10 +1,9 @@ (ns net.bowmap.network.minecraft-server (:require- [clojure.core.async :refer [>!! chan close! io-thread]]+ [clojure.core.async :refer [>!! chan io-thread]] [net.bowmap.network.connection :as connection]) (:import- [java.io EOFException]- [java.net ServerSocket SocketException]))+ [java.net ServerSocket])) (defonce server (atom nil)) @@ -20,19 +19,7 @@ (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))- (catch EOFException _)- (catch SocketException _)- (finally- (close! serverbound-frames))))- (>!! connections connection))+ (>!! connections (accept-connection!! server-socket)) (recur))) connections)) src/net/bowmap/network/server.clj view file
@@ -31,6 +31,7 @@ (recur client)))) clientbound-packets ([pk] (if (some? pk)+ ; Would be nice to enqueue frames so there is symmetry (do (send-packet!! client pk) (recur client)) (throw (Exception. "Clientbound packets was closed idk why"))))))@@ -38,7 +39,7 @@ ; TODO Send player disconnected fact, and update the player list from that #_(handle-responses! (logic/handle-fact {::fact/id ::fact/player-left ::entity/uuid (::entity/uuid client)}))- (connection/close! connection))))))+ ((::close-fn connection))))))) (defn start-server! [port] (let [connections (mc/start-server! port)]src/net/bowmap/network/streams.clj view file
@@ -10,6 +10,8 @@ InputStream OutputStream])) +; TODO move this all out to somewhere else, I don't like it here.+ (defn read-byte!! "Blocks and returns an unsigned byte from 'input-stream', or throws if EOF is reached" [input-stream]