如何在Netty中使用UDP协议的protobuf有效负载?你能用自定义protobuf协议给我一个例子吗? TCP协议只有一个例子。
public void bind(int port)抛出异常{
EventLoopGroup bossGroup = new NioEventLoopGroup();
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) {
ch.pipeline().addLast(new ProtobufVarint32FrameDecoder());
ch.pipeline().addLast(new ProtobufDecoder(RichManProto.RichMan.getDefaultInstance()));
ch.pipeline().addLast(new ProtoBufServerHandler());
}
});
ChannelFuture f = b.bind(port).sync();
System.out.println("init start");
f.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
答案 0 :(得分:0)
我也有同样的问题。但是我要重构代码以接收转换为byte []的DatagramPacket,然后使用parseFrom(byte [])方法反序列化回ProtoBuf对象。