Netty:永远不会调用 ChannelRead0
Netty: ChannelRead0 is never called
我正在尝试将文件从 java 服务器应用程序发送到 java 客户端应用程序。
该文件似乎是从服务器发送到客户端的,因为我得到这样的输出:
...
Transfer progress: 8459452
Transfer progress: 8459452 / 8459452
Transfer complete.
问题似乎出在客户端。 channelReadComplete
为服务器发送的每个块调用。但是 channelRead0
永远不会被调用!甚至 class 中的 channelRead
(在我的处理程序中没有被覆盖)也不会被调用。知道我的代码有什么问题吗?提前致谢!
客户端管道:
ChannelPipeline p = ch.pipeline();
p.addLast("Encoder", new ObjectEncoder());
p.addLast( "Decoder", new ObjectDecoder( Integer.MAX_VALUE, ClassResolvers.cacheDisabled( null ) ));
p.addLast("chunkedWriteHandler", new ChunkedWriteHandler());
p.addLast( "FileChunkHandler", new FileChunkHandler());
服务器管道:
p.addLast( "Encoder", new ObjectEncoder());
p.addLast( "Decoder", new ObjectDecoder( Integer.MAX_VALUE, ClassResolvers.cacheDisabled( null ) ));
p.addLast( "chunkedWriteHandler", new ChunkedWriteHandler());
p.addLast( "FileSenderHandler", new FileSenderHandler());
客户端的FileChunkHandler:
public class FileChunkHandler extends SimpleChannelInboundHandler<ChunkedFile>{
@Override protected void channelRead0(ChannelHandlerContext ctx, ChunkedFile msg) throws Exception{
System.out.println( "channelRead0 ++++");
}
@Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception{
System.out.println( "channelReadComplete ++++");
ctx.fireChannelReadComplete();
}
}
服务器上的 FileSenderHandler:
public class FileSenderHandler 扩展 ChannelInboundHandlerAdapter{
@Override public void channelRegistered(ChannelHandlerContext ctx) throws Exception{
File file = new File( "/Users/kathrinjennifer/Documents/tmp/test/test.mp3" );
RandomAccessFile raf = null;
long fileLength = 0;
try {
raf = new RandomAccessFile(file, "r");
} catch(IOException e){
e.printStackTrace();
}
ChannelFuture sendFileFuture = null;
ChunkedFile chunkedFile = new ChunkedFile( raf);
sendFileFuture = ctx.write( chunkedFile, ctx.newProgressivePromise());
ctx.flush();
sendFileFuture.addListener(new ChannelProgressiveFutureListener() {
@Override
public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) {
if (total < 0) { // total unknown
System.err.println(future.channel() + " Transfer progress: " + progress);
} else {
System.err.println(future.channel() + " Transfer progress: " + progress + " / " + total);
}
}
@Override
public void operationComplete(ChannelProgressiveFuture future) {
System.err.println(future.channel() + " Transfer complete.");
}
});
ctx.fireChannelRegistered();
}
您应该使用 SimpleChannelInboundHandler<ByteBuf>
,因为您将收到字节。
我正在尝试将文件从 java 服务器应用程序发送到 java 客户端应用程序。 该文件似乎是从服务器发送到客户端的,因为我得到这样的输出:
...
Transfer progress: 8459452
Transfer progress: 8459452 / 8459452
Transfer complete.
问题似乎出在客户端。 channelReadComplete
为服务器发送的每个块调用。但是 channelRead0
永远不会被调用!甚至 class 中的 channelRead
(在我的处理程序中没有被覆盖)也不会被调用。知道我的代码有什么问题吗?提前致谢!
客户端管道:
ChannelPipeline p = ch.pipeline();
p.addLast("Encoder", new ObjectEncoder());
p.addLast( "Decoder", new ObjectDecoder( Integer.MAX_VALUE, ClassResolvers.cacheDisabled( null ) ));
p.addLast("chunkedWriteHandler", new ChunkedWriteHandler());
p.addLast( "FileChunkHandler", new FileChunkHandler());
服务器管道:
p.addLast( "Encoder", new ObjectEncoder());
p.addLast( "Decoder", new ObjectDecoder( Integer.MAX_VALUE, ClassResolvers.cacheDisabled( null ) ));
p.addLast( "chunkedWriteHandler", new ChunkedWriteHandler());
p.addLast( "FileSenderHandler", new FileSenderHandler());
客户端的FileChunkHandler:
public class FileChunkHandler extends SimpleChannelInboundHandler<ChunkedFile>{
@Override protected void channelRead0(ChannelHandlerContext ctx, ChunkedFile msg) throws Exception{
System.out.println( "channelRead0 ++++");
}
@Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception{
System.out.println( "channelReadComplete ++++");
ctx.fireChannelReadComplete();
}
}
服务器上的 FileSenderHandler:
public class FileSenderHandler 扩展 ChannelInboundHandlerAdapter{
@Override public void channelRegistered(ChannelHandlerContext ctx) throws Exception{
File file = new File( "/Users/kathrinjennifer/Documents/tmp/test/test.mp3" );
RandomAccessFile raf = null;
long fileLength = 0;
try {
raf = new RandomAccessFile(file, "r");
} catch(IOException e){
e.printStackTrace();
}
ChannelFuture sendFileFuture = null;
ChunkedFile chunkedFile = new ChunkedFile( raf);
sendFileFuture = ctx.write( chunkedFile, ctx.newProgressivePromise());
ctx.flush();
sendFileFuture.addListener(new ChannelProgressiveFutureListener() {
@Override
public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) {
if (total < 0) { // total unknown
System.err.println(future.channel() + " Transfer progress: " + progress);
} else {
System.err.println(future.channel() + " Transfer progress: " + progress + " / " + total);
}
}
@Override
public void operationComplete(ChannelProgressiveFuture future) {
System.err.println(future.channel() + " Transfer complete.");
}
});
ctx.fireChannelRegistered();
}
您应该使用 SimpleChannelInboundHandler<ByteBuf>
,因为您将收到字节。