Netty + NioDatagram(UDP)+ ProtoBuf问题 - 丢弃入站消息DatagramPacket

时间:2017-05-22 18:24:50

标签: udp netty protocol-buffers

我正在尝试使用带有NioDatagramChannel和protoBuf的netty构建UDP服务器作为消息。这是服务器实现:

EventLoopGroup group = new NioEventLoopGroup();
try {
  Bootstrap b = new Bootstrap();
  b.group(group)
      .channel(NioDatagramChannel.class)
      .option(ChannelOption.SO_BROADCAST, true)
      .handler(new UdpProtoBufServerChannelInitializer());

  b.bind(port).sync().channel().closeFuture().await();
} finally {
  group.shutdownGracefully();
}

UdpProtoBufServerChannelInitializer:

public class UdpProtoBufServerChannelInitializer extends ChannelInitializer<NioDatagramChannel> {

    static final Logger logger = Logger.getLogger(UdpProtoBufServerChannelInitializer.class);
    @Override
      protected void initChannel(NioDatagramChannel ch) throws Exception {
        ChannelPipeline p = ch.pipeline();
        p.addFirst(new ProtobufVarint32FrameDecoder());
        p.addFirst(new ProtobufDecoder(Message.getDefaultInstance()));
        p.addFirst(new ProtobufVarint32LengthFieldPrepender());
        p.addFirst(new ProtobufEncoder());

        p.addFirst(new UdpProtoBufServerHandler());
    }
  }

但是,在运行UdpProtoBufServerChannelInitializer之前从java客户端收到消息后,我看到以下错误。

DEBUG DefaultChannelPipeline:76 - 放弃的入站消息DatagramPacket(/127.0.0.1:60556 =&gt; 0.0.0.0 / 0.0.0:55559,PooledUnsafeHeapByteBuf(ridx:0,widx:34,cap:2048))管道的尾部。请检查您的管道配置。

此外,启动应用程序时不确定此异常是否相关:

    2017-05-22 14:31:09 DEBUG PlatformDependent0:91 - jdk.internal.misc.Unsafe.allocateUninitializedArray(int): unavailable
java.lang.ClassNotFoundException: jdk.internal.misc.Unsafe
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at io.netty.util.internal.PlatformDependent0$6.run(PlatformDependent0.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at io.netty.util.internal.PlatformDependent0.<clinit>(PlatformDependent0.java:288)
    at io.netty.util.internal.PlatformDependent.getSystemClassLoader(PlatformDependent.java:895)
    at io.netty.util.internal.PlatformDependent.isAndroid0(PlatformDependent.java:919)
    at io.netty.util.internal.PlatformDependent.<clinit>(PlatformDependent.java:70)
    at io.netty.util.ConstantPool.<init>(ConstantPool.java:32)
    at io.netty.util.Signal$1.<init>(Signal.java:27)
    at io.netty.util.Signal.<clinit>(Signal.java:27)
    at io.netty.util.concurrent.DefaultPromise.<clinit>(DefaultPromise.java:43)
    at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:36)
    at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:58)
    at io.netty.channel.MultithreadEventLoopGroup.<init>(MultithreadEventLoopGroup.java:52)
    at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:87)
    at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:82)
    at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:63)
    at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:51)
    at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:43)

2 个答案:

答案 0 :(得分:1)

谢谢大家。我终于找到了做到这一点的方法:

public class DatagramServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {

  @Override
  protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
      Message message = SocketProtos.Message.parseFrom(packet.content().nioBuffer());
      ctx.writeAndFlush(packet.retain());
  }

  @Override
  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
      cause.printStackTrace();
      ctx.close();
   }
}

只需使用NioDatagram UDP标准实现,然后将消息解析为byte []到ProtoBuf消息。

答案 1 :(得分:0)

好吧,看起来你的管道没有消费者。您应该将自定义处理程序放在处理数据的位置。您可以在使用p.addFirst(new ProtobufVarint32FrameDecoder());的{​​{1}}之前或p.addFirst(new MyHandler());之后使用p.addFirst(new UdpProtoBufServerHandler());执行此操作。

事实上,我可以假设您希望在代码中使用p.addLast(new MyHandler());而不是addLast。如果您使用addFirst,则会在管道addLast中声明订单时收到订单。在您的情况下(使用ProtobufVarint32FrameDecoder -> ProtobufDecoder -> ... -> UdpProtoBufServerHandler),您会看到错误的addFirst,因为您在第一个位置有一个处理程序,在结尾处有一个解码器。