67c4f1a
Branches > namespace-reorganization > 67c4f1a > files / .gitignore
move framing to its own namespace, move instance management from core into server
Commit: 67c4f1a9030f026a5677445702c5b6000d9f6aaa
Parent: 54711cb
3 changed files:
src/net/bowmap/core.clj view file
@@ -1,10 +1,9 @@ (ns net.bowmap.core (:require- [net.bowmap.instance :as instance] [net.bowmap.network.server :as server])) (defn start-server []- (instance/start! (server/start-server! {:port 25565})))+ (server/start-server! {:port 25565})) (defn -main [& _args] (start-server)src/net/bowmap/network/packet/frame.clj view file
@@ -0,0 +1,24 @@+(ns net.bowmap.network.packet.frame+ (:require+ [net.bowmap.network.packet.read :as read])+ (:import+ [io.netty.buffer ByteBuf Unpooled]))++(defn packet-framer-xf+ "Transducer that frames bytes from incoming byte arrays and outputs ByteBufs of+ framed serialized serverbound packets."+ [rf]+ (let [buf (Unpooled/buffer)]+ (fn+ ([] (rf))+ ([res] (rf res))+ ([res input]+ (ByteBuf/.writeBytes buf input)+ (loop [res res]+ (if (read/can-read-packet? buf)+ (let [len (read/->varint buf)+ res' (rf res (ByteBuf/.copy (ByteBuf/.readRetainedSlice buf len)))]+ (ByteBuf/.discardReadBytes buf)+ (recur res'))+ res))))))+src/net/bowmap/network/server.clj view file
@@ -1,46 +1,26 @@ (ns net.bowmap.network.server (:require [aleph.tcp :as tcp]- [clojure.core.async :refer [chan io-thread]]+ [clojure.core.async :refer [chan]] [manifold.stream :as s] [net.bowmap.instance :as instance]- [net.bowmap.network.packet.read :as read :refer [->varint]])+ [net.bowmap.network.packet.frame :as frame]) (:import [io.netty.bootstrap ServerBootstrap]- [io.netty.buffer ByteBuf Unpooled] [io.netty.channel ChannelOption])) -(defn packet-framer-xf- "Transducer that frames bytes from incoming byte arrays and outputs ByteBufs of- framed serialized serverbound packets."- [rf]- (let [buf (Unpooled/buffer)]- (fn- ([] (rf))- ([res] (rf res))- ([res input]- (ByteBuf/.writeBytes buf input)- (loop [res res]- (if (read/can-read-packet? buf)- (let [len (->varint buf)- res' (rf res (ByteBuf/.copy (ByteBuf/.readRetainedSlice buf len)))]- (ByteBuf/.discardReadBytes buf)- (recur res'))- res))))))- (defn- handle-client [stream info]- (let [packets-in (chan 1 packet-framer-xf)+ (let [packets-in (chan 1 frame/packet-framer-xf) packets-out (chan) client (instance/client)] (s/connect stream packets-in) (s/connect packets-out stream) (instance/connect! client packets-in packets-out))) -; Could return a channel of clients?-; Make sure this is just kept an interface, it really only needs to know about packets-in, packets-out, and then maybe how to close/disconnect? (defn start-server!- ;"Starts a server at the port, calling 'handler' with an 'in' and 'out' channel of packets."+ "Starts a Minecraft server at the port, passing clients into the instance." [& {:keys [port]}]- (tcp/start-server handle-client- {:port port- :bootstrap-transform #(ServerBootstrap/.childOption % ChannelOption/TCP_NODELAY true)}))+ (instance/start!+ (tcp/start-server handle-client+ {:port port+ :bootstrap-transform #(ServerBootstrap/.childOption % ChannelOption/TCP_NODELAY true)})))