From b5a587fadfd0821088d37d9d23388c343d73eb45 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Fri, 15 Nov 2013 12:00:33 +0100 Subject: [PATCH] Use pooled allocator for AutobahnServer and voidPromises --- .../http/websocketx/autobahn/AutobahnServer.java | 12 +++++++----- .../websocketx/autobahn/AutobahnServerHandler.java | 12 +++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/example/src/main/java/io/netty/example/http/websocketx/autobahn/AutobahnServer.java b/example/src/main/java/io/netty/example/http/websocketx/autobahn/AutobahnServer.java index 457434f359..52b3f4a778 100644 --- a/example/src/main/java/io/netty/example/http/websocketx/autobahn/AutobahnServer.java +++ b/example/src/main/java/io/netty/example/http/websocketx/autobahn/AutobahnServer.java @@ -16,7 +16,9 @@ package io.netty.example.http.websocketx.autobahn; import io.netty.bootstrap.ServerBootstrap; +import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; @@ -34,20 +36,20 @@ public class AutobahnServer { } public void run() throws Exception { - EventLoopGroup bossGroup = new NioEventLoopGroup(); - EventLoopGroup workerGroup = new NioEventLoopGroup(); + EventLoopGroup group = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); - b.group(bossGroup, workerGroup) + b.group(group) .channel(NioServerSocketChannel.class) + .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) + .option(ChannelOption.SO_BACKLOG, 1024) .childHandler(new AutobahnServerInitializer()); ChannelFuture f = b.bind(port).sync(); System.out.println("Web Socket Server started at port " + port); f.channel().closeFuture().sync(); } finally { - bossGroup.shutdownGracefully(); - workerGroup.shutdownGracefully(); + group.shutdownGracefully(); } } diff --git a/example/src/main/java/io/netty/example/http/websocketx/autobahn/AutobahnServerHandler.java b/example/src/main/java/io/netty/example/http/websocketx/autobahn/AutobahnServerHandler.java index 44f8c03f2b..006eec27b1 100644 --- a/example/src/main/java/io/netty/example/http/websocketx/autobahn/AutobahnServerHandler.java +++ b/example/src/main/java/io/netty/example/http/websocketx/autobahn/AutobahnServerHandler.java @@ -35,6 +35,7 @@ import io.netty.handler.codec.http.websocketx.WebSocketFrame; import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker; import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; import io.netty.util.CharsetUtil; +import io.netty.util.ReferenceCountUtil; import io.netty.util.internal.StringUtil; import java.util.logging.Level; @@ -59,6 +60,8 @@ public class AutobahnServerHandler extends ChannelInboundHandlerAdapter { handleHttpRequest(ctx, (FullHttpRequest) msg); } else if (msg instanceof WebSocketFrame) { handleWebSocketFrame(ctx, (WebSocketFrame) msg); + } else { + ReferenceCountUtil.release(msg); } } @@ -104,13 +107,13 @@ public class AutobahnServerHandler extends ChannelInboundHandlerAdapter { if (frame instanceof CloseWebSocketFrame) { handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame); } else if (frame instanceof PingWebSocketFrame) { - ctx.write(new PongWebSocketFrame(frame.isFinalFragment(), frame.rsv(), frame.content())); + ctx.write(new PongWebSocketFrame(frame.isFinalFragment(), frame.rsv(), frame.content()), ctx.voidPromise()); } else if (frame instanceof TextWebSocketFrame) { - ctx.write(frame); + ctx.write(frame, ctx.voidPromise()); } else if (frame instanceof BinaryWebSocketFrame) { - ctx.write(frame); + ctx.write(frame, ctx.voidPromise()); } else if (frame instanceof ContinuationWebSocketFrame) { - ctx.write(frame); + ctx.write(frame, ctx.voidPromise()); } else if (frame instanceof PongWebSocketFrame) { frame.release(); // Ignore @@ -139,7 +142,6 @@ public class AutobahnServerHandler extends ChannelInboundHandlerAdapter { @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - cause.printStackTrace(); ctx.close(); }