szhnet / kcp-netty

Java implementation of KCP based on Netty. 基于netty实现的java版kcp。
MIT License
329 stars 110 forks source link

为什么设置了UKCP_SND_WND和UKCP_RCV_WND,用Netty的Http方式传输文件,会出现最后一些字节发不出去的情况? #2

Closed benjaminpan8 closed 6 years ago

benjaminpan8 commented 6 years ago

用Netty的Http传输文件,如果调整了UKCP_SND_WND和UKCP_RCV_WND的值,就会导致有一定的几率,这个文件最后一点字节没发出去。用ctx.flush()或者channel.flush都没用。

用wireshark抓包发现的,最后一次发送貌似存在KCP server缓存里,没有发送出去。

Server端的代码: UkcpServerBootstrap b = new UkcpServerBootstrap(); b.group(group) .channel(UkcpServerChannel.class) .childOption(UkcpChannelOption.UKCP_SND_WND, 1024) .childOption(UkcpChannelOption.UKCP_RCV_WND, 1024) .childHandler(new ChannelInitializer() { @Override public void initChannel(UkcpChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new HttpObjectAggregator(100* 65536)); pipeline.addLast(new ChunkedWriteHandler()); pipeline.addLast(new HttpStaticFileServerHandler()); } }); ChannelOptionHelper.nodelay(b, true, 20, 2, true)

Server Handler出问题的代码,直接拷贝Netty的 Example,没动过: https://github.com/netty/netty/blob/4.1/example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java

ChannelFuture sendFileFuture; sendFileFuture = ctx.writeAndFlush(new HttpChunkedInput(new ChunkedFile(raf, 0, fileLength, 8192)), ctx.newProgressivePromise());

benjaminpan8 commented 6 years ago

貌似是线程阻塞的问题。如果server的ctx.writeAndFlush(buff)之后立刻Thread.sleep(5000),就会发不出去。

szhnet commented 6 years ago

我没有你的代码,不知道具体什么情况,为什么还要Thread.sleep(5000)。kcp-netty用了slf4j记录日志,你可以输出日志看看,也许能有帮助。