[英]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.