Search in sources :

Example 1 with ConnectionListener

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

Connection (com.esotericsoftware.kryonet.Connection)1 ConnectionListener (com.esotericsoftware.kryonet.Listener.ConnectionListener)1 TypeListener (com.esotericsoftware.kryonet.Listener.TypeListener)1 Server (com.esotericsoftware.kryonet.Server)1 IOException (java.io.IOException)1