[英]Netty pipeline broken when adding decoder/encoder
我最近開始將netty用於我的一個項目。 為了了解netty的工作原理,我實現了HexDumpProxy示例。 我在通道管道中添加StringDecoder和StringEncoder時遇到問題,這會導致管道中斷。 如果不存在解碼器/編碼器,則程序將正常運行。 有人可以解釋一下為什么會這樣嗎? 任何幫助深表感謝!
接下來,我要添加代碼。
主班:
public final class HexDumpProxy {
public static void main(String[] args) throws Exception {
// Configure the bootstrap.
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new HexDumpProxyInitializer("127.0.0.1", 9000))
.childOption(ChannelOption.AUTO_READ, false)
.bind(8000).sync().channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
初始化類:
public class HexDumpProxyInitializer extends ChannelInitializer<SocketChannel> {
private final String remoteHost;
private final int remotePort;
public HexDumpProxyInitializer (String remoteHost, int remotePort) {
this.remoteHost = remoteHost;
this.remotePort = remotePort;
}
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("frameDecoder", new LineBasedFrameDecoder(80));
ch.pipeline().addLast("decoder", new StringDecoder());
ch.pipeline().addLast("encoder", new StringEncoder());
//ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));
ch.pipeline().addLast(new HexDumpProxyFrontendHandler(remoteHost, remotePort));
}
}
前端處理程序類:
public class HexDumpProxyFrontendHandler extends ChannelInboundHandlerAdapter {
private final String remoteHost;
private final int remotePort;
private Channel serverChannel;
public HexDumpProxyFrontendHandler(String remoteHost, int remotePort) {
this.remoteHost = remoteHost;
this.remotePort = remotePort;
}
@Override
public void channelActive(ChannelHandlerContext ctx) {
final Channel clientChannel = ctx.channel();
Bootstrap b = new Bootstrap();
b.group(clientChannel.eventLoop())
.channel(ctx.channel().getClass())
.handler(new HexDumpProxyBackendHandler(clientChannel))
.option(ChannelOption.AUTO_READ, false);
ChannelFuture f = b.connect(remoteHost, remotePort);
serverChannel = f.channel();
f.addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) {
if (future.isSuccess()) {
clientChannel.read();
}
else
clientChannel.close();
}
});
}
@Override
public void channelRead (final ChannelHandlerContext ctx, Object msg) {
if (serverChannel.isActive()) {
System.out.println("************" + msg);
serverChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) {
if (future.isSuccess()) {
ctx.channel().read();
System.out.println("Read from server channel. - channelRead");
} else {
future.channel().close();
System.out.println("Close server channel.");
}
}
});
}
}
@Override
public void channelInactive (ChannelHandlerContext ctx) {
if (serverChannel != null) {
closeOnFlush(serverChannel);
System.out.println("close on flush - server channel");
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
static void closeOnFlush (Channel ch) {
if (ch.isActive())
ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
ctx.channel().read();
}
}
后端處理程序類:
public class HexDumpProxyBackendHandler extends ChannelInboundHandlerAdapter {
private final Channel clientChannel;
public HexDumpProxyBackendHandler (Channel clientChannel) {
this.clientChannel = clientChannel;
}
@Override
public void channelActive (ChannelHandlerContext ctx) {
ctx.channel().read();
System.out.println("Read from client channel. - channelActive");
}
@Override
public void channelRead (final ChannelHandlerContext ctx, Object msg) {
System.out.println("~~~~~~~~~~~" + msg);
clientChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) {
if (future.isSuccess()) {
clientChannel.read();
System.out.println("Read from client channel. - channelRead");
} else {
clientChannel.close();
System.out.println("Close client channel.");
}
}
});
}
@Override
public void channelInactive (ChannelHandlerContext ctx) {
HexDumpProxyFrontendHandler.closeOnFlush(clientChannel);
System.out.println("close on flush - client channel");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
ctx.channel().read();
}
}
對於將來會遇到類似問題的每個人,我都知道問題出在哪里。
在類HexDumpProxyFrontendHandler
而不是在引導過程中使用新的HexDumpProxyBackendHandler(clientChannel)
作為處理程序,創建一個新類,例如HexDumpProxyBackendInitializer(clientChannel)
並在該類中以與類HexDumpProxyInitializer
相同的方式初始化管道。
希望這可以幫助某人!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.