67fc197
Branches > facts > 67fc197 > files / deps.edn
Move client stream code to a separate namespace
Commit: 67fc197f842bfb62b33b5bb86833f2cf89d2461c
Parent: 2cf928c
3 changed files:
src/net/bowmap/network/client.clj view file
@@ -1,62 +1,15 @@ (ns net.bowmap.network.client (:require [net.bowmap.network.packets.clientbound :refer [encode-packet!]]- [net.bowmap.network.packets.types :as t]- [net.bowmap.network.state :as state])+ [net.bowmap.network.state :as state]+ [net.bowmap.network.streams :as streams]) (:import- [io.netty.buffer ByteBuf Unpooled]- [java.io- EOFException- IOException- InputStream- OutputStream] [java.net Socket])) -(defn read-byte!!- "Blocks and returns an unsigned byte from 'input-stream', or throws if EOF is reached"- [input-stream]- (let [x (InputStream/.read input-stream)]- (if (= -1 x)- (throw (EOFException. "EOF while reading from InputStream"))- x)))--(defn read-varint!!- "Blocks and returns a varint from 'input-stream', or throws if one cannot be read"- [input-stream]- (loop [num-read 0- result 0]- (if (>= num-read t/max-varint-size)- (throw (IOException. "VarInt too long"))- (let [b (read-byte!! input-stream)- result' (bit-or result (bit-shift-left (bit-and b 0x7F) (* 7 num-read)))]- (if (zero? (bit-and b 0x80))- result'- (recur (inc num-read) result'))))))--(defn read-frame!!- "Reads a frame from 'input-stream', or throws if one cannot be read"- [input-stream]- (let [len (read-varint!! input-stream)- arr (byte-array len)]- (loop [offset 0]- (when (< offset len)- (let [r (InputStream/.read input-stream arr offset (- len offset))]- (if (= r -1)- (throw (EOFException. "EOF while reading frame"))- (recur (+ offset r))))))- (Unpooled/wrappedBuffer arr)))--(defn write-frame!! [frame output-stream]- (let [len (ByteBuf/.readableBytes frame)- arr (byte-array len)]- (ByteBuf/.readBytes frame arr)- (OutputStream/.write output-stream arr)- (OutputStream/.flush output-stream)))- (defn send-clientbound-packet [client packet] (println "sending packet out: " packet)- (write-frame!! (encode-packet! packet (req! client ::state/connection-state))- (Socket/.getOutputStream (req! client ::socket)))+ (streams/write-frame!! (encode-packet! packet (req! client ::state/connection-state))+ (Socket/.getOutputStream (req! client ::socket))) client) (defn client [socket]src/net/bowmap/network/server.clj view file
@@ -2,7 +2,8 @@ (:require [clojure.core.async :refer [io-thread]] [net.bowmap.network.client :as client]- [net.bowmap.network.handle :as handle])+ [net.bowmap.network.handle :as handle]+ [net.bowmap.network.streams :as streams]) (:import [java.io EOFException] [java.net ServerSocket Socket SocketException]))@@ -16,7 +17,7 @@ (io-thread (try (loop []- (let [frame (client/read-frame!! input-stream)]+ (let [frame (streams/read-frame!! input-stream)] (send client-agent handle/process-serverbound-frame frame)) (recur)) (catch EOFException _)src/net/bowmap/network/streams.clj view file
@@ -0,0 +1,53 @@+(ns net.bowmap.network.streams+ "Functions for reading and writing frames from input and output streams"+ (:require+ [net.bowmap.network.packets.types :as t])+ (:import+ [io.netty.buffer ByteBuf Unpooled]+ [java.io+ EOFException+ IOException+ InputStream+ OutputStream]))++(defn read-byte!!+ "Blocks and returns an unsigned byte from 'input-stream', or throws if EOF is reached"+ [input-stream]+ (let [x (InputStream/.read input-stream)]+ (if (= -1 x)+ (throw (EOFException. "EOF while reading from InputStream"))+ x)))++(defn read-varint!!+ "Blocks and returns a varint from 'input-stream', or throws if one cannot be read"+ [input-stream]+ (loop [num-read 0+ result 0]+ (if (>= num-read t/max-varint-size)+ (throw (IOException. "VarInt too long"))+ (let [b (read-byte!! input-stream)+ result' (bit-or result (bit-shift-left (bit-and b 0x7F) (* 7 num-read)))]+ (if (zero? (bit-and b 0x80))+ result'+ (recur (inc num-read) result'))))))++(defn read-frame!!+ "Reads a frame from 'input-stream', or throws if one cannot be read"+ [input-stream]+ (let [len (read-varint!! input-stream)+ arr (byte-array len)]+ (loop [offset 0]+ (when (< offset len)+ (let [r (InputStream/.read input-stream arr offset (- len offset))]+ (if (= r -1)+ (throw (EOFException. "EOF while reading frame"))+ (recur (+ offset r))))))+ (Unpooled/wrappedBuffer arr)))++(defn write-frame!! [frame output-stream]+ (let [len (ByteBuf/.readableBytes frame)+ arr (byte-array len)]+ (ByteBuf/.readBytes frame arr)+ (OutputStream/.write output-stream arr)+ (OutputStream/.flush output-stream)))+