Reputation: 5753
I'm new to Netty and have succeeded in creating a server that works over TLS. I'm now designing a second server but I'm having some issues with the pipeline. What I need is a dynamic pipeline like the port unification example from Netty: http://netty.io/4.0/xref/io/netty/example/portunification/PortUnificationServerHandler.html
Each connection starts with the client sending a UTF-8 encoded string. I can read this fine, but my reply isn't coming through. My reply is just a byte array of 3 bytes.
Here's my server:
// Configure the server
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new LineBasedFrameDecoder(192));
pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast(new ByteArrayEncoder());
pipeline.addLast(new TokenHandler());
}
});
// Start the server
ChannelFuture f = b.bind(port).sync();
// Wait until the server socket is closed
f.channel().closeFuture().sync();
}
finally {
// Shut down all event loops to terminate all threads
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
And my TokenHandler, which is a SimpleChannelInboundHandler:
public class TokenHandler extends SimpleChannelInboundHandler<String> {
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, String token) throws Exception {
System.out.println("Token received: " + token);
channelHandlerContext.writeAndFlush(new byte[] {2, 1, 0});
}
}
I've also tried replacing that last line with
channelHandlerContext.writeAndFlush(Unpooled.copiedBuffer(new byte[] {2, 1, 0}));
and removing the ByteArrayEncoder from my pipeline, but this doesn't make any difference: no bytes are being sent. I can send strings just fine if I add a StringEncoder, and the Echo example from Netty also works (available from the user guide: http://netty.io/wiki/user-guide-for-4.x.html). I'm guessing it's a basic mistake being new to Netty, but I can't find other people with cases with a similar issue.
EDIT: I'm using Netty 4.0.34.Final
Upvotes: 1
Views: 1687
Reputation: 5753
I should've read the docs more carefully. The solution was to add super(true) to the constructor of my TokenHandler because by default a SimpleChannelInboundHandler releases its messages after processing which stops the response from propagating further through the pipeline.
Upvotes: 1