Search code examples
javaunity-game-enginenetworkingnettygame-loop

Core game logic loop to send data to channels in Netty


I want to send world state every 100 msec to all channels. But it calls only once.

My code:

        public class IncomeMessageTcpHandler extends SimpleChannelInboundHandler<byte[]> {

@Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();

        channel.eventLoop().scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("send");
                channel.writeAndFlush(GameLogic.Instance.flushMessageData());
            }
        }, 100, 100, TimeUnit.MILLISECONDS);
    }
}

Now the method call only once. I use 4.1.13.Final

    public class UnityServerTcpChannelInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    public void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline p = ch.pipeline();

        p.addLast("frameDecoder", new FixedLengthFrameDecoder(6));
        p.addLast("bytesDecoder", new ByteArrayDecoder());

        p.addLast("bytesEncoder", new ByteArrayEncoder());

        p.addLast(new IncomeMessageTcpHandler());
    }
}

When I comment channel.writeAndFlush(GameLogic.Instance.flushMessageData()); task run every 100 msec


Solution

  • Omg... my fault. My method GameLogic.Instance.flushMessageData() generates NullPointerException but I did not know that Runnable do not throw any exception. That's why it stops running without any warn