Search in sources :

Example 1 with ServerDiscoveryHandler

use of com.esotericsoftware.kryonet.ServerDiscoveryHandler 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

Server (com.esotericsoftware.kryonet.Server)1 ServerDiscoveryHandler (com.esotericsoftware.kryonet.ServerDiscoveryHandler)1 DiscoveryResponsePacket (de.gg.engine.network.message.DiscoveryResponsePacket)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 DatagramChannel (java.nio.channels.DatagramChannel)1