Search in sources :

Example 1 with Server

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

Example 2 with Server

use of com.esotericsoftware.kryonet.Server in project ProjektGG by eskalon.

the class BaseGameServer method start.

/**
 * Sets up a server asynchronously. After it is finished the callback is
 * informed.
 *
 * @param callback
 *            the callback that is informed when the server is started.
 */
public void start(ISuccessCallback callback) {
    Preconditions.checkNotNull(callback, "callback cannot be null.");
    Log.info("Server", "--- Server is starting ---");
    this.server = new Server();
    this.server.start();
    // ON NEW CONNECTION & ON DICONNECTED
    this.server.addListener(new ConnectionListener() {

        @Override
        public void connected(Connection con) {
            onClientConnected(con);
        }

        @Override
        public void disconnected(Connection con) {
            onClientDisconnected(con);
        }
    });
    TypeListener typeListener = new TypeListener();
    typeListener.addTypeHandler(ClientHandshakeRequest.class, (con, msg) -> onClientHandshake(con, msg));
    server.addListener(typeListener);
    onCreation();
    ThreadHandler.getInstance().executeRunnable(() -> {
        try {
            // Start the server
            server.bind(serverSetup.getPort());
            Log.info("Server", "Server started");
            // Create & start the broadcast server
            if (serverSetup.isPublic()) {
                startBroadcastServer();
            }
            // Host successfully started
            callback.onSuccess(null);
        } catch (IOException | IllegalArgumentException e2) {
            Log.error("Server", "Server could not be started: %s", e2);
            // Something went wrong
            callback.onFailure(e2);
        }
    });
}
Also used : Server(com.esotericsoftware.kryonet.Server) TypeListener(com.esotericsoftware.kryonet.Listener.TypeListener) Connection(com.esotericsoftware.kryonet.Connection) ConnectionListener(com.esotericsoftware.kryonet.Listener.ConnectionListener) IOException(java.io.IOException)

Aggregations

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