bd9f7f9
Branches > main > bd9f7f9 > files
Enqueue clientbound packets so they are sent in the correct order
Commit: bd9f7f9866e8f13ab2459451915437192cbd7cda
Parent: 5b0c894
1 changed file:
src/net/bowmap/server/network/connection.clj view file
@@ -7,40 +7,40 @@ [net.bowmap.server.network.streams :as streams]) (:import [java.io IOException]- [java.net Socket]))+ [java.net Socket]+ [java.util.concurrent LinkedBlockingQueue])) (defprotocol Connection (send-frame! [conn frame] "Sends 'frame' to the connection asynchronously. Returns nil immediately.") (receive-frame!! [conn] "Blocks until a frame can be read from 'conn'.- Returns nil when a frame cannot be read."))+ Returns nil after frames can no longer be read from the connection.")) (s/def ::connection #(satisfies? Connection %)) -(defn- log-unexpected-exception [ex]- (when-not (instance? IOException ex)- (log/warn- (with-out-str- (print-stack-trace ex)))))- (defn connection "Creates a connection from a given java.net.Socket." [socket] (let [input-stream (Socket/.getInputStream socket)- output-stream (Socket/.getOutputStream socket)]+ output-stream (Socket/.getOutputStream socket)+ send-queue (LinkedBlockingQueue.)] (Socket/.setTcpNoDelay socket true)+ (Thread/startVirtualThread+ #(try+ (loop []+ (let [frame (.take send-queue)]+ (streams/write-frame!! frame output-stream)+ (recur)))+ (catch IOException _)+ (finally+ (Socket/.close socket)))) (reify Connection (send-frame! [_ frame]- (Thread/startVirtualThread- #(try (streams/write-frame!! frame output-stream)- (catch Exception e- (Socket/.close socket)- (log-unexpected-exception e))))- nil)+ (.put send-queue frame)) (receive-frame!! [_] (try (streams/read-frame!! input-stream)+ (catch IOException _+ (Socket/.close socket)) (catch Exception e- (Socket/.close socket)- (log-unexpected-exception e)- nil))))))+ (log/warn (with-out-str (print-stack-trace e)))))))))