handle.clj

Branches > main > 2c083b6 > files / src / net / bowmap / network / handle.clj

(ns net.bowmap.network.handle
  (:require
   [net.bowmap.network.client :as client]
   [net.bowmap.network.packet :as pk]
   [net.bowmap.network.packets.clientbound :as clientbound]
   [net.bowmap.network.packets.serverbound :as serverbound :refer [decode-packet!]]
   [net.bowmap.network.state :as state]))

; Handles incoming packets from clients

(defn handle-packet
  "Handles a severbound packet from a client"
  [client client-agent packet]
  (println packet)
  (case (::pk/id packet)
    ::serverbound/handshaking (assoc client ::state/connection-state (req! packet :intent))
    ::serverbound/status-request (do (send client-agent client/send-clientbound-packet
                                           {::pk/id ::clientbound/status-response
                                            ;; Should be un-bundled yeah.
                                            :response {:version {:name "26.2"
                                                                 :protocol 776}
                                                       :players {:online 0
                                                                 :max 1000}
                                                       ; Is this a text component? Could make that interface
                                                       :description {:text "Bowmap!"}
                                                       :enforcesSecureChat false}})
                                     client)
    ::serverbound/ping-request (do (send client-agent client/send-clientbound-packet
                                         {::pk/id ::clientbound/pong-response
                                          :timestamp (:timestamp packet)})
                                   client)
    client))

(defn process-serverbound-packet [client client-agent frame]
  (let [packet (decode-packet! frame (req! client ::state/connection-state))]
    ; Get some packets back
    ; Send them out :D
    (handle-packet client client-agent packet)))

(defn process-incoming-frame! [client-agent frame]
  (send client-agent process-serverbound-packet client-agent frame))