Search in sources :

Example 1 with Connection

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

Example 2 with Connection

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

the class BaseGameClient method connect.

/**
 * Tries to connect the client to the server asynchronously. After it is
 * finished the given listener is called.
 *
 * @param callback
 *            The listener callback.
 * @param gameVersion
 *            the client's game version.
 * @param ip
 *            The server's ip address.
 * @param port
 *            The server's port.
 */
public void connect(ISuccessCallback callback, String gameVersion, String ip, int port) {
    Preconditions.checkNotNull(callback, "callback cannot be null.");
    Preconditions.checkNotNull(gameVersion, "game version cannot be null.");
    Preconditions.checkNotNull(ip, "ip cannot be null.");
    Log.info("Client", "--- Connecting to Server ---");
    client = new Client();
    client.start();
    TypeListener listener = new TypeListener();
    // CLIENT CONNECTION
    // On Server acceptance (stage 1 of connection)
    listener.addTypeHandler(ServerAcceptanceResponse.class, (con, msg) -> {
        client.sendTCP(new ClientHandshakeRequest(MachineIdentificationUtils.getHostname(), gameVersion));
    });
    // Server full
    listener.addTypeHandler(ServerRejectionResponse.class, (con, msg) -> {
        Log.info("Client", "Couldn't connect: Client was rejected (%s)", msg.getMessage());
        callback.onFailure(msg.getMessage());
    });
    // Server handshake (stage 2 of connection)
    listener.addTypeHandler(SuccessfulHandshakeResponse.class, (con, msg) -> {
        Log.info("Client", "Connection established. Local network ID is: %d", localClientId);
        localClientId = msg.getClientNetworkId();
        onSuccessfulHandshake();
        client.addListener(new Listener() {

            @Override
            public void disconnected(Connection connection) {
                Log.info("Client", "Connection closed!");
                onDisconnection();
            }
        });
        callback.onSuccess(null);
    });
    listener.addTypeHandler(FailedHandshakeResponse.class, (con, msg) -> {
        Log.info("Client", "Couldn't connect: Handshake was not successful (%s)", msg.getMsg());
        callback.onFailure(msg.getMsg());
    });
    // Ping
    listener.addTypeHandler(Ping.class, (con, msg) -> {
        if (msg.isReply) {
            this.ping = con.getReturnTripTime();
            Log.info("Client", "Ping: %d", ping);
        }
    });
    onCreation();
    client.addListener(listener);
    ThreadHandler.getInstance().executeRunnable(() -> {
        try {
            client.connect(6000, ip, port);
        // A successful connection further requires a proper handshake
        } catch (IOException e) {
            Log.error("Client", "Couldn't connect: %s", e);
            callback.onFailure("Couldn't connect: " + e.getMessage());
        }
    });
}
Also used : TypeListener(com.esotericsoftware.kryonet.Listener.TypeListener) Listener(com.esotericsoftware.kryonet.Listener) TypeListener(com.esotericsoftware.kryonet.Listener.TypeListener) Connection(com.esotericsoftware.kryonet.Connection) IOException(java.io.IOException) Client(com.esotericsoftware.kryonet.Client) ClientHandshakeRequest(de.gg.engine.network.message.ClientHandshakeRequest)

Aggregations

Connection (com.esotericsoftware.kryonet.Connection)2 TypeListener (com.esotericsoftware.kryonet.Listener.TypeListener)2 IOException (java.io.IOException)2 Client (com.esotericsoftware.kryonet.Client)1 Listener (com.esotericsoftware.kryonet.Listener)1 ConnectionListener (com.esotericsoftware.kryonet.Listener.ConnectionListener)1 Server (com.esotericsoftware.kryonet.Server)1 ClientHandshakeRequest (de.gg.engine.network.message.ClientHandshakeRequest)1