client.clj
Branches > facts > 8237942 > files / src / net / bowmap / network / client.clj
(ns net.bowmap.network.client
(:require
[net.bowmap.network.packets.clientbound :refer [encode-packet!]]
[net.bowmap.network.packets.serverbound :refer [decode-packet!]]
[net.bowmap.network.state :as state]
[net.bowmap.network.streams :as streams])
(:import
[java.net Socket]))
(defn client [socket]
(ref {::state/connection-state ::state/handshaking
::socket socket}))
(defn read-packet! [client-ref]
(let [{:keys! [::socket ::state/connection-state]} @client-ref
frame (streams/read-frame!! (Socket/.getInputStream socket))]
(decode-packet! frame connection-state)))
(defn send-packet! [client-ref packet]
(let [{:keys! [::socket ::state/connection-state]} @client-ref]
#_(println "sending packet out: " packet)
(streams/write-frame!! (encode-packet! packet connection-state) (Socket/.getOutputStream socket))
client))