Use pooled allocator for AutobahnServer and voidPromises
This commit is contained in:
parent
5de4b23c7a
commit
b5a587fadf
@ -16,7 +16,9 @@
|
|||||||
package io.netty.example.http.websocketx.autobahn;
|
package io.netty.example.http.websocketx.autobahn;
|
||||||
|
|
||||||
import io.netty.bootstrap.ServerBootstrap;
|
import io.netty.bootstrap.ServerBootstrap;
|
||||||
|
import io.netty.buffer.PooledByteBufAllocator;
|
||||||
import io.netty.channel.ChannelFuture;
|
import io.netty.channel.ChannelFuture;
|
||||||
|
import io.netty.channel.ChannelOption;
|
||||||
import io.netty.channel.EventLoopGroup;
|
import io.netty.channel.EventLoopGroup;
|
||||||
import io.netty.channel.nio.NioEventLoopGroup;
|
import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||||
@ -34,20 +36,20 @@ public class AutobahnServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void run() throws Exception {
|
public void run() throws Exception {
|
||||||
EventLoopGroup bossGroup = new NioEventLoopGroup();
|
EventLoopGroup group = new NioEventLoopGroup();
|
||||||
EventLoopGroup workerGroup = new NioEventLoopGroup();
|
|
||||||
try {
|
try {
|
||||||
ServerBootstrap b = new ServerBootstrap();
|
ServerBootstrap b = new ServerBootstrap();
|
||||||
b.group(bossGroup, workerGroup)
|
b.group(group)
|
||||||
.channel(NioServerSocketChannel.class)
|
.channel(NioServerSocketChannel.class)
|
||||||
|
.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
|
||||||
|
.option(ChannelOption.SO_BACKLOG, 1024)
|
||||||
.childHandler(new AutobahnServerInitializer());
|
.childHandler(new AutobahnServerInitializer());
|
||||||
|
|
||||||
ChannelFuture f = b.bind(port).sync();
|
ChannelFuture f = b.bind(port).sync();
|
||||||
System.out.println("Web Socket Server started at port " + port);
|
System.out.println("Web Socket Server started at port " + port);
|
||||||
f.channel().closeFuture().sync();
|
f.channel().closeFuture().sync();
|
||||||
} finally {
|
} finally {
|
||||||
bossGroup.shutdownGracefully();
|
group.shutdownGracefully();
|
||||||
workerGroup.shutdownGracefully();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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.WebSocketServerHandshaker;
|
||||||
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory;
|
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory;
|
||||||
import io.netty.util.CharsetUtil;
|
import io.netty.util.CharsetUtil;
|
||||||
|
import io.netty.util.ReferenceCountUtil;
|
||||||
import io.netty.util.internal.StringUtil;
|
import io.netty.util.internal.StringUtil;
|
||||||
|
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
@ -59,6 +60,8 @@ public class AutobahnServerHandler extends ChannelInboundHandlerAdapter {
|
|||||||
handleHttpRequest(ctx, (FullHttpRequest) msg);
|
handleHttpRequest(ctx, (FullHttpRequest) msg);
|
||||||
} else if (msg instanceof WebSocketFrame) {
|
} else if (msg instanceof WebSocketFrame) {
|
||||||
handleWebSocketFrame(ctx, (WebSocketFrame) msg);
|
handleWebSocketFrame(ctx, (WebSocketFrame) msg);
|
||||||
|
} else {
|
||||||
|
ReferenceCountUtil.release(msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,13 +107,13 @@ public class AutobahnServerHandler extends ChannelInboundHandlerAdapter {
|
|||||||
if (frame instanceof CloseWebSocketFrame) {
|
if (frame instanceof CloseWebSocketFrame) {
|
||||||
handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame);
|
handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame);
|
||||||
} else if (frame instanceof PingWebSocketFrame) {
|
} 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) {
|
} else if (frame instanceof TextWebSocketFrame) {
|
||||||
ctx.write(frame);
|
ctx.write(frame, ctx.voidPromise());
|
||||||
} else if (frame instanceof BinaryWebSocketFrame) {
|
} else if (frame instanceof BinaryWebSocketFrame) {
|
||||||
ctx.write(frame);
|
ctx.write(frame, ctx.voidPromise());
|
||||||
} else if (frame instanceof ContinuationWebSocketFrame) {
|
} else if (frame instanceof ContinuationWebSocketFrame) {
|
||||||
ctx.write(frame);
|
ctx.write(frame, ctx.voidPromise());
|
||||||
} else if (frame instanceof PongWebSocketFrame) {
|
} else if (frame instanceof PongWebSocketFrame) {
|
||||||
frame.release();
|
frame.release();
|
||||||
// Ignore
|
// Ignore
|
||||||
@ -139,7 +142,6 @@ public class AutobahnServerHandler extends ChannelInboundHandlerAdapter {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||||
cause.printStackTrace();
|
|
||||||
ctx.close();
|
ctx.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user