Search in sources :

Example 1 with DiscoveryResponsePacket

use of de.gg.engine.network.message.DiscoveryResponsePacket in project ProjektGG by eskalon.

the class GameServerDiscoveryTest method testServer.

@Test
public void testServer() throws TimeoutException, InterruptedException {
    com.esotericsoftware.minlog.Log.INFO();
    waiter = new Waiter();
    sdh = new ServerDiscoveryHandler<>(DiscoveryResponsePacket.class, 2500);
    assertThrows(IllegalArgumentException.class, () -> {
        sdh.discoverHosts(-563, new HostDiscoveryListener<>() {

            @Override
            public void onHostDiscovered(String address, DiscoveryResponsePacket datagramPacket) {
            }
        });
    });
    assertThrows(NullPointerException.class, () -> {
        sdh.discoverHosts(123, null);
    });
    ServerSetup serverSetup = new ServerSetup(gameName, maxPlayerCount, port, true, serverVersion, true);
    GameSessionSetup sessionSetup = new GameSessionSetup(GameDifficulty.EASY, GameMap.BAMBERG, 25);
    server = new GameServer(serverSetup, sessionSetup, null, Arrays.asList(stub, stub, stub));
    server.start(new ISuccessCallback() {

        @Override
        public void onSuccess(Object param) {
            waiter.resume();
        }

        @Override
        public void onFailure(Object param) {
            waiter.fail((String) param);
        }
    });
    // Wait for resume() to be called
    waiter.await(8000);
    testServerDiscovery(waiter);
    server.stop();
}
Also used : DiscoveryResponsePacket(de.gg.engine.network.message.DiscoveryResponsePacket) GameSessionSetup(de.gg.game.session.GameSessionSetup) ServerSetup(de.gg.engine.network.ServerSetup) ISuccessCallback(de.damios.guacamole.ISuccessCallback) Waiter(net.jodah.concurrentunit.Waiter) BaseGameServer(de.gg.engine.network.BaseGameServer) Test(org.junit.jupiter.api.Test) LibgdxUnitTest(de.gg.game.LibgdxUnitTest)

Example 2 with DiscoveryResponsePacket

use of de.gg.engine.network.message.DiscoveryResponsePacket in project ProjektGG by eskalon.

the class BaseGameServer method startBroadcastServer.

private void startBroadcastServer() {
    broadcastServer = new Server();
    broadcastServer.start();
    broadcastServer.getKryo().register(DiscoveryResponsePacket.class);
    broadcastServer.setDiscoveryHandler(new ServerDiscoveryHandler() {

        @Override
        public boolean onDiscoverHost(DatagramChannel datagramChannel, InetSocketAddress fromAddress) throws IOException {
            DiscoveryResponsePacket packet = new DiscoveryResponsePacket(serverSetup.getPort(), serverSetup.getGameName(), players.size(), serverSetup.getMaxPlayerCount());
            ByteBuffer buffer = ByteBuffer.allocate(256);
            broadcastServer.getSerialization().write(null, buffer, packet);
            buffer.flip();
            datagramChannel.send(buffer, fromAddress);
            return true;
        }
    });
    try {
        broadcastServer.bind(0, UDP_DISCOVER_PORT);
        Log.info("Server", "Broadcast server started");
    } catch (IOException e1) {
        Log.error("Server", "Broadcast server couldn't be started: %s", e1);
    }
}
Also used : Server(com.esotericsoftware.kryonet.Server) DiscoveryResponsePacket(de.gg.engine.network.message.DiscoveryResponsePacket) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) ServerDiscoveryHandler(com.esotericsoftware.kryonet.ServerDiscoveryHandler)

Aggregations

DiscoveryResponsePacket (de.gg.engine.network.message.DiscoveryResponsePacket)2 Server (com.esotericsoftware.kryonet.Server)1 ServerDiscoveryHandler (com.esotericsoftware.kryonet.ServerDiscoveryHandler)1 ISuccessCallback (de.damios.guacamole.ISuccessCallback)1 BaseGameServer (de.gg.engine.network.BaseGameServer)1 ServerSetup (de.gg.engine.network.ServerSetup)1 LibgdxUnitTest (de.gg.game.LibgdxUnitTest)1 GameSessionSetup (de.gg.game.session.GameSessionSetup)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 DatagramChannel (java.nio.channels.DatagramChannel)1 Waiter (net.jodah.concurrentunit.Waiter)1 Test (org.junit.jupiter.api.Test)1