Created
June 18, 2013 21:46
-
-
Save adamrofer/5809753 to your computer and use it in GitHub Desktop.
Netty 4.0.0.CR5 UDP pipeline example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void run() throws Exception { | |
EventLoopGroup udpGroup = new NioEventLoopGroup(); | |
try { | |
Bootstrap b = new Bootstrap(); | |
b.group(udpGroup) | |
.channel(NioDatagramChannel.class) | |
.handler(new ChannelInitializer<DatagramChannel>() { | |
@Override | |
protected void initChannel(DatagramChannel ch) throws Exception { | |
ChannelPipeline p = ch.pipeline(); | |
p.addLast("udpDecoder", new MessageToMessageDecoder<DatagramPacket>() { | |
@Override | |
protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, MessageList<Object> out) throws Exception { | |
out.add(msg.content()); | |
msg.retain(); | |
} | |
}); | |
p.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(65535, 2, 2)); | |
} | |
}); | |
b.bind(port).sync().channel().closeFuture().sync(); | |
} finally { | |
udpGroup.shutdownGracefully(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment