gpt4 book ai didi

TCP 使用 Aleph 传输长字节

转载 作者:可可西里 更新时间:2023-11-01 02:42:27 30 4
gpt4 key购买 nike

我正在尝试基于 aleph 构建 RCP 服务器。它通过了所有测试,但是当用于发送或接收的字节数组变大时,字节似乎已损坏。

例如。我试图发送一个长度为 2936 的字节数组,但我在服务器上只收到了 1024 个字节

我按照示例对 nippy 进行了自己的修改。编码和解码将由处理程序自己完成。

(defn wrap-duplex-stream
[s]
(let [out (s/stream)]
(s/connect out s)
(s/splice out s)))

(defn client
[host port]
(d/chain (tcp/client {:host host, :port port})
#(wrap-duplex-stream %)))

(defn start-server
[handler port]
(tcp/start-server
(fn [s info]
(handler (wrap-duplex-stream s) info))
{:port port}))

最佳答案

我终于为字节数组制作了自己的编解码器。它很简单,但是弄清楚如何将它与 gloss 和 aleph 一起使用非常耗时

(defn buffer->byte-array [buf-seq]
(byte-streams/to-byte-array buf-seq))

(defn bytes-codec []
(reify
Reader
(read-bytes [this buf-seq]
(let [buf-seq (dup-bytes buf-seq)
byte-arr (buffer->byte-array buf-seq)]
[true byte-arr nil]))
Writer
(sizeof [x]
nil)
(write-bytes [x y byte-arr]
[(ByteBuffer/wrap byte-arr)])))

(def protocol
(gloss/compile-frame
(gloss/finite-frame
:uint32
(bytes-codec))
#(nippy/freeze %)
#(nippy/thaw %)))


(defn wrap-duplex-stream
[s]
(let [out (s/stream)]
(s/connect
(s/map #(io/encode protocol %) out)
s)
(s/splice
out
(io/decode-stream s protocol))))

(defn client
[host port]
(d/chain (tcp/client {:host host, :port port})
#(wrap-duplex-stream %)))

(defn start-server
[handler port]
(tcp/start-server
(fn [s info]
(handler (wrap-duplex-stream s) info))
{:port port}))

关于TCP 使用 Aleph 传输长字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34496356/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com