Search in sources :

Example 1 with ClientHandshakeRequest

use of de.gg.engine.network.message.ClientHandshakeRequest 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

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