kryonet 侦听器未收到消息?

Message not being received by kryonet listener?

所以我在测试我的网络时遇到了问题,下面有很多代码,但我只能举一个例子。我遇到的问题是,当我发送消息时,我正在向服务器和客户端注册的侦听器似乎永远不会被调用。

lock.await(5000, TimeUnit.MILLISECONDS) 应该等到收到消息或在 5 秒时超时。然而,侦听器应该在设置防止测试失败的 response 变量后告诉锁继续 lock.countDown()

如您所料,这并没有发生,消息被发送,锁由于超时而继续并且测试失败,因为 response 为空。

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;
import com.esotericsoftware.minlog.Log;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static junit.framework.TestCase.fail;

public class TestMessage {
    private Server server;
    private Client client;
    private String response;
    private CountDownLatch lock = new CountDownLatch(1);

    @Before
    public void setUp() {
        Log.set(Log.LEVEL_DEBUG);

        server = new Server();
        try {
            Log.debug("Binding to port: " + 30454 + "... ");
            server.bind(30454);
            Log.debug("Bound to port" + 30454);
        } catch (IOException e) {
            Log.debug("failed");
            Log.error("Unable to bind to port: " + e.getMessage());
            server.stop();
            fail("Unable to start server");
        }
        Kryo kryo = server.getKryo();
        kryo.register(Message.class);
        Log.debug("Adding server listener");
        server.addListener(new TestListener());
        Log.debug("Starting server... ");
        server.start();
        Log.debug("Server started successfully");
    }

    @Test
    public void testPacketSending() throws IOException, InterruptedException {
        client = new Client();
        client.addListener(new TestListener());
        Kryo kryo = client.getKryo();
        kryo.register(Message.class);
        client.start();
        client.connect(5000, "127.0.0.1", 30454);

        client.sendTCP(new Message("RECEIVED"));

        lock.await(5000, TimeUnit.MILLISECONDS);

        Assert.assertNotNull(response);
    }

    private class TestListener extends Listener {
        @Override
        public void received(Connection connection, Object o) {
            Message m = (Message) o;
            response = m.message;
            Log.debug(m.message);
            lock.countDown();
        }
    }

    private class Message {
        String message;

        Message(String message) {
            this.message = message;
        }
    }
}

这里的问题是:

00:00 ERROR: [kryonet] Error reading TCP from connection: Connection 1
com.esotericsoftware.kryonet.KryoNetException: Error during deserialization.
    at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:141)
    at com.esotericsoftware.kryonet.Server.update(Server.java:205)
    at com.esotericsoftware.kryonet.Server.run(Server.java:372)
    at java.lang.Thread.run(Thread.java:745)
Caused by: com.esotericsoftware.kryo.KryoException: Class cannot be created (non-static member class): org.glytching.sandbox.kryo.TestMessage$MessageA
    at com.esotericsoftware.kryo.Kryo$DefaultInstantiatorStrategy.newInstantiatorOf(Kryo.java:1308)
    at com.esotericsoftware.kryo.Kryo.newInstantiator(Kryo.java:1127)
    at com.esotericsoftware.kryo.Kryo.newInstance(Kryo.java:1136)
    at com.esotericsoftware.kryo.serializers.FieldSerializer.create(FieldSerializer.java:562)
    at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:538)
    at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:816)
    at com.esotericsoftware.kryonet.KryoSerialization.read(KryoSerialization.java:55)
    at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:139)
    ... 3 more

Kryonet 无法反序列化为 Message 因为 (a) 它是一个非静态内部 class 并且 (b) 它没有 public 零参数构造函数。 FWIW,关于为什么 Kryonet 不支持非静态内部 classes 的序列化有一些 background here

如果您 (a) 将 Message 重构为它自己的 class 或使其成为静态的,并且 (b) 给它一个零参数构造函数,那么您的测试将通过。

使您的测试通过所需的最小更改是替换此...

private class Message {
    String message;

    Message(String message) {
        this.message = message;
    }
}

...有了这个:

private static class Message {
    String message;

    Message() {

    }

    Message(String message) {
        this.message = message;
    }
}

回复:

there's a lot of code below but this is as small as I could make an example

你问题中的复制案例绰绰有余,谢谢。