200396e
Branches > main > 200396e > files
Handle packets directly
Commit: 200396eff6c3daf0cfa899c60f8cdd55c41fea43
Parent: 18e16a1
2 changed files:
src/net/bowmap/server/runtime/packets.clj view file
@@ -1,33 +1,17 @@ (ns net.bowmap.server.runtime.packets (:require- [medley.core :refer [assoc-some]] [net.bowmap.server.protocol.packet :as packet]- [net.bowmap.server.protocol.codec :as codec] [net.bowmap.server.protocol.phase :as phase] [net.bowmap.server.protocol.serverbound-packets :as serverbound])) -(defn phase-transition- "Returns the phase a client should take on after parsing 'packet', or nil."- [packet]- (case (req! packet ::packet/id)- ::serverbound/handshaking (req! packet ::phase/phase)- ::serverbound/login-acknowledged ::phase/config- ::serverbound/finish-config ::phase/play- nil))--(defn with-state-transition- "Updates the client with any state transitions caused by 'packet'.- Serverbound packets may:- 1. Update the connection phase- 2. Update encryption state- 3. Update compression state"+(defn apply-packet+ "'Applies' a serverbound packet to a client.+ Returns [client' [packet*]?]" [client packet]- (assoc-some client ::phase/phase (phase-transition packet)))--(defn apply-serverbound-frame- "Decodes 'frame' using the given client state, and returns [client' packet].- Decoding a frame can update a client's connection phase, encryption state,- and compression state"- [client frame]- (let [packet (codec/decode-frame client frame)]- [(with-state-transition client packet) packet]))+ (let [{::packet/keys! [id]} packet]+ (case id+ ::serverbound/handshaking [(assoc client ::phase/phase (req! packet ::phase/phase))]+ ::serverbound/status-request [client]+ ::serverbound/login-acknowledged [(assoc client ::phase/phase ::phase/config)]+ ::serverbound/finish-config [(assoc client ::phase/phase ::phase/play)]+ [client])))src/net/bowmap/server/runtime/server.clj view file
@@ -1,24 +1,27 @@ (ns net.bowmap.server.runtime.server (:require [clojure.core.async :refer [<!! chan io-thread put!]]- [net.bowmap.server.runtime.packets :refer [apply-serverbound-frame]] [net.bowmap.server.network.connection :as connection]+ [net.bowmap.server.network.minecraft-server :as mc]+ [net.bowmap.server.protocol.codec :as codec] [net.bowmap.server.protocol.phase :as phase]- [net.bowmap.server.network.minecraft-server :as mc]))+ [net.bowmap.server.runtime.packets :as packets])) (defn client-step ([_] {::phase/phase ::phase/handshaking}) ([client in frame] (case in- ::connection/serverbound-frames (apply-serverbound-frame client frame))))+ ::connection/serverbound-frames+ (packets/apply-packet client (codec/decode-frame client frame))))) -(defn handle-connection! [{::connection/keys! [serverbound-frames]}]- (let [serverbound-packets (chan 1024)]+(defn handle-connection! [{::connection/keys! [serverbound-frames clientbound-frames]}]+ (let [clientbound-packets (chan 1024)] (io-thread (loop [client-state (client-step nil)] (when-some [frame (<!! serverbound-frames)]- (let [[client' packet] (client-step client-state ::connection/serverbound-frames frame)]- (put! serverbound-packets packet)+ (let [[client' packets] (client-step client-state ::connection/serverbound-frames frame)]+ (doseq [packet packets]+ (put! clientbound-packets packet)) (recur client'))))))) (defn run-server!