server.clj

Branches > namespace-reorganization > 28161d2 > files / src / net / bowmap / packet / server.clj

(ns net.bowmap.packet.server
  (:require
   [clojure.alpha.spec :as s]
   [dev.grahp.build :refer [build]]
   [dev.grahp.error :refer [raise]]
   [net.bowmap.client.state :as state]
   [net.bowmap.packet :as pk]
   [net.bowmap.packet.read :as read])
  (:import
   [io.netty.buffer ByteBuf]))

(defn build-packets [registry]
  (reduce (fn [acc [packet-id table]]
            (assoc acc packet-id
                   (let [reader (read/reader [:table table])]
                     (fn [buf]
                       (assoc (reader buf)
                              ::pk/id packet-id)))))
          {} registry))

(defn build-packet-ids [registry]
  (build [[state packet-ids] registry
          [i id] (map-indexed vector packet-ids)]
    [{::state/state   state
      ::pk/numeric-id i}
     id]))

(def ^{:spec (s/map-of (s/schema [::state/state ::pk/numeric-id]) ::pk/id)}
  packet-ids
  (build-packet-ids
    {::state/handshaking
     [::handshaking]
     ::state/status
     [::status-request
      ::ping-request]
     ::state/login
     [::login-start
      ::encryption-response
      ::login-plugin-response
      ::login-acknowledged
      ::login-cookie-response]
     ::state/config
     [::client-information
      ::cookie-response
      ::server-plugin-message
      ::ack-finish-config
      ::server-keep-alive
      ::pong
      ::resource-pack-response
      ::server-known-packs
      ::custom-click-action
      ::accept-code-of-conduct]}))

(def ^{:spec (s/map-of ::pk/id (s/fspec :args (s/cat :buf #(instance? ByteBuf %))
                                        :ret ::pk/packet))}
  packets
  (build-packets
    {::handshaking [:protocol :varint
                    :address :string
                    :port :short
                    :intent [:enum :varint {1 ::state/status 2 ::state/login 3 ::state/transfer}]]
     ::status-request []
     ::ping-request [:timestamp :long]
     ::login-start [:username :string
                    :uuid :uuid]
     ::login-acknowledged []
     ;; This packet has more fields I just don't care about them :p
     ::server-plugin-message []
     ;; This packet also has a bunch of fields that I probably do care about!
     ::client-information []}))

(defn decode-packet!
  "Decodes and returns one packet from ByteBuf 'buf', which should contain at least one readable packet.
   The initial length field of the packet should not be stripped from 'buf'.
   Can throw if:
    1. No such packet with the packet id and state exists
    2. 'buf' is not properly framed, and does not contain a full packet
    3. The contents of 'buf' are not a valid packet"
  [buf state]
  {:pre [(read/can-read-packet? buf)]}
  (let [length (read/read-varint! buf)
        packet (ByteBuf/.readSlice buf length)
        numeric-id (read/read-varint! packet)
        packet-id (packet-ids {::state/state state ::pk/numeric-id numeric-id})
        dedec (packets packet-id)]
    (if dedec
      (dedec packet)
      (raise "Failed to decode packet, no such packet with that id." {:id numeric-id :state state :packet-id packet-id}))))