[Kryonet] Exception when casting received Object (Bug?)

I have this small piece of code in my class which extends Listener:

@Override
public void received(Connection connection, Object object)
{
    Packet packet = (Packet) object;
    server.addPacket(new ClientPacket(connection.getID(), packet));
}

Whenever I receive an Object, I cast it to an Interface called Packet with a method handle() which every packet implement. I then add it to a ConcurrentLinkedQueue for future processing.

Still, after i spam a few keys which send UDP packets to the server, the following Exception is thrown:


Exception in thread "Server" java.lang.ClassCastException: com.esotericsoftware.kryonet.FrameworkMessage$KeepAlive cannot be cast to com.xkynar.game.net.packet.Packet
at com.xkynar.game.net.ServerSocket.received(ServerSocket.java:70)
at com.esotericsoftware.kryonet.Server$1.received(Server.java:61) at com.esotericsoftware.kryonet.Connection.notifyReceived(Connection.java:246)
at com.esotericsoftware.kryonet.Server.update(Server.java:208)
at com.esotericsoftware.kryonet.Server.run(Server.java:356) at java.lang.Thread.run(Unknown Source)

The exception occurs in the cast, that is:

Packet packet = (Packet) object;

How can this be possible? What the hell is “FrameworkMessage$KeepAlive” to begin with? Why is it entering my received listener?
Is it my mistake or a bug?
Thank you.

I have never used kryonet, but the best guess would be that KeepAlive is a packet which kryonet sends without your knowledge to not close the connection after certain period of time, or something like that.

Assuming that Packet is an interface or class that you created, you should do this:


@Override
public void received(Connection connection, Object object)
{
    System.out.println(object.getClass().getSimpleName()); // you will see that you receive packets you didn't send.

    if(object instanceof Packet) {
        Packet packet = (Packet) object;
        server.addPacket(new ClientPacket(connection.getID(), packet));
    }
}

-_- I feel angry I didnt remember of that. Maybe Its my lack of experience with networking libraries. That solved it, thanks alot.