gpt4 book ai didi

java - Netty:未使用 DefaultFileRegion 调用 ChannelProgressiveFutureListener

转载 作者:太空宇宙 更新时间:2023-11-04 12:58:19 24 4
gpt4 key购买 nike

我正在使用 DefaulfFileRegion 从基于 netty 的服务器进行下游传输。当客户端接收数据时,我添加到 channel future 的 ChannelProgressiveFutureListener 永远不会被调用。我对使用 netty 编程不太有经验,所以我想我误解了 netty 渐进式 ChannelFuture 的工作原理。因此,我查看了在这里找到的示例代码:programcreek.com 。但它的代码基本相同?我尝试在这里寻找类似的问题,但找不到原因。预先感谢您的任何提示,为什么听众永远不会被调用。

   long todo = file.length () - mRequest.pCursor;
ctx.writeAndFlush (mFileCursorPacket.encodeInformation (new FileCursor (mRequest.pToken, todo)));


mFileregion = new DefaultFileRegion (file, mRequest.pCursor, todo);
ChannelFuture future = ctx.writeAndFlush (mFileregion,ctx.newProgressivePromise ());

//the listener that is never called!
future.addListener (new ChannelProgressiveFutureListener () {
@Override
public void operationProgressed (ChannelProgressiveFuture future, long progress, long total) throws Exception {
O.debug (mSession.getId ()+": downloaded "+progress+" bytes = "+String.valueOf ((float)progress/(float)total));
}

@Override
public void operationComplete (ChannelProgressiveFuture future) throws Exception {
boolean success = future.isSuccess ();
O.info (mSession.getId () + ": downloaded successfull: " + success);
if (mFileregion != null) mFileregion.release ();
mFileregion = null;
}
});

我的管道如下所示:

        ServerBootstrap b = new ServerBootstrap (); // 
b.group (mBossGroup, mWorkerGroup).channel (NioServerSocketChannel.class) //
//.channel(ServerSocketChannel.class)
.childHandler (new ChannelInitializer<SocketChannel> () { //
@Override
public void initChannel (SocketChannel ch) throws Exception {

ProtocolMessageInboundHandler inboundHandler = new ProtocolMessageInboundHandler ();
inboundHandler.setMessageSizeLimit (MAX_PACKET_LENGTH);
ch.pipeline ().addLast (ReadTimeoutHandler.class.getName (), new ReadTimeoutHandler (SO_TIMEOUT_STD));
ch.pipeline ().addLast (NAME_PROTOCOL_MESSAGE_INBOUND, inboundHandler);
ch.pipeline ().addLast (NAME_PROTOCOL_MESSAGE_OUTBOUND, new ProtoclMessageOutboundHandler ());

//context passed to the LoginHandler is a ServerContext and has nothing to do with netty
ch.pipeline ().addLast (LoginHandler.class.getName (), new LoginHandler (context));
ch.pipeline ().addLast (TimeoutHandler.class.getName (), new TimeoutHandler ());


}
}).option (ChannelOption.SO_BACKLOG, 128).childOption (ChannelOption.SO_KEEPALIVE, true); //

// Bind and start to accept incoming connections.
b.bind (PORT).sync ();

而文件发送处理程序插入在 ProtocolMessageInboundHandler 之后(仅修改 byte[] 对象并传递 bytebuf 对象)。所以文件发送处理程序和?netty?之间没有其他处理程序?

最佳答案

如果您想发送FileRegion,您的管道中应该有一个可以发送此类对象的处理程序。 Netty 中最常用的用于此目的的类是 ChunkedWriteHandler 。这也用在 example 中对于DefaultFileRegion

将以下代码添加到初始化程序中以使其支持 FileRegion:

ch.pipeline ().addLast(new ChunkedWriteHandler());

关于java - Netty:未使用 DefaultFileRegion 调用 ChannelProgressiveFutureListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35176725/

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