client.clj

Branches > facts > af56f7e > files / src / net / bowmap / network / client.clj

(ns net.bowmap.network.client
  (:require
   [net.bowmap.network.packets.clientbound :refer [encode-packet!]]
   [net.bowmap.network.state :as state]
   [net.bowmap.network.streams :as streams]
   [net.bowmap.server.entity :as entity]
   [net.bowmap.server.server :as server])
  (:import
   [java.net Socket]))

(defn send-clientbound-packet [client packet]
  #_(println "sending packet out: " packet)
  (streams/write-frame!! (encode-packet! packet (req! client ::state/connection-state))
                         (Socket/.getOutputStream (req! client ::socket)))
  client)

(defn leave! [client-agent]
  (try
    (dosync
      (when-let [uuid (::entity/uuid @client-agent)]
        (commute server/players dissoc uuid)))
    (finally
      (Socket/.close (req! @client-agent ::socket)))))

(defn client [socket]
  (agent {::socket socket
          ::state/connection-state ::state/handshaking}
         :error-handler (fn [a ex]
                          (try
                            (Throwable/.printStackTrace ex *err*)
                            (finally
                              (leave! a))))))