cbd1421
Branches > main > cbd1421 > files / src / net / bowmap
Update core
Commit: cbd1421a4bc3e09e762ac70842aad4f6fe146371
Parent: de0d217
1 changed file:
src/net/bowmap/core.clj view file
@@ -4,32 +4,30 @@ [manifold.stream :as s] [net.bowmap.client.state :as state] [net.bowmap.codecs.read :as read]- [net.bowmap.packet-handling :refer [handle-packet]]+ [net.bowmap.packet-handling :as handle] [net.bowmap.packets.client :refer [encode-packet!]] [net.bowmap.packets.server :as sp]) (:import [io.netty.buffer ByteBuf Unpooled] [io.netty.channel ChannelOption])) -(defn print-bb [bb]- (subvec (vec (.array bb))- (.readerIndex bb)- (.writerIndex bb)))+;; TODO disconnect client on exception -;; Document why parsing and handling are done synchronously-(defn read-packet! [client bs]+(defn handle-bytes+ "Handles a byte array of one TCP packet, splits it into Minecraft protocol packets,+ sends response packets to the client, and returns a new client.+ Packets are parsed and handled synchronously as packet decoding can update+ client state, so is impure."+ [client bs] (let [{::keys [buf stream]} client buf (ByteBuf/.writeBytes buf bs)] (loop [client client] (if (read/can-read-packet? buf)- (let [{:keys [::state/state response]}- (handle-packet (sp/decode-packet! buf (::state/state client))- client)+ (let [pk (sp/decode-packet! buf (::state/state client))+ {:keys [::state/state response]} (handle/handle-packet pk client) client (cond-> client state (assoc ::state/state state))] (when response- ;; TODO this can also throw... (let [pk (encode-packet! response (::state/state client))]- (prn "response packet:" (print-bb pk)) (s/put! stream pk))) (ByteBuf/.discardReadBytes buf) (recur client))@@ -39,10 +37,7 @@ (let [client {::state/state ::state/handshaking ::buf (Unpooled/buffer) ::stream s}]- (s/reduce (fn [client bs]- (read-packet! client bs))- client- s)))+ (s/reduce #'handle-bytes client s))) (defn start-server! [] (tcp/start-server #'client-handler@@ -55,5 +50,5 @@ (println "Server Started")) (comment- (.close server)- (def server (start-server!)))+ (def server (start-server!))+ (.close server))