All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.mayuna.timestop.networking.base.TimeStopClient Maven / Gradle / Ivy

package dev.mayuna.timestop.networking.base;

import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.minlog.Log;
import dev.mayuna.timestop.networking.NetworkConstants;
import dev.mayuna.timestop.networking.base.listener.TimeStopListenerManager;
import dev.mayuna.timestop.networking.base.serialization.TimeStopSerialization;
import dev.mayuna.timestop.networking.base.translator.TimeStopTranslator;
import dev.mayuna.timestop.networking.base.translator.TimeStopTranslatorManager;
import lombok.Getter;

import java.io.IOException;
import java.net.InetAddress;
import java.util.Timer;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Consumer;

/**
 * TimeStopClient
* Usage:
*
 *     TimeStopClient client = new TimeStopClient(endpointConfig);
 *     client.start(); // Must be called before connecting
 *     client#connect()
 * 
*/ @Getter public class TimeStopClient extends Client implements Listener, TimeStopResponseHandler { protected final Timer timeoutTimer = new Timer(); protected final EndpointConfig endpointConfig; protected TimeStopListenerManager listenerManager; protected TimeStopTranslatorManager translatorManager; /** * Creates a new client with the given endpoint config * * @param endpointConfig Endpoint config * @param writeBufferSize Write buffer size * @param objectBufferSize Object buffer size */ public TimeStopClient(EndpointConfig endpointConfig, int writeBufferSize, int objectBufferSize) { super(writeBufferSize, objectBufferSize); this.endpointConfig = endpointConfig; prepare(); } /** * Creates a new client with the given endpoint config * * @param endpointConfig Endpoint config */ public TimeStopClient(EndpointConfig endpointConfig) { this(endpointConfig, NetworkConstants.WRITE_BUFFER_SIZE, NetworkConstants.OBJECT_BUFFER_SIZE); } /** * Prepares the client for usage */ protected void prepare() { Log.info("Preparing client..."); // Listener & translator manager listenerManager = new TimeStopListenerManager(endpointConfig.getMaxThreads()); translatorManager = new TimeStopTranslatorManager(endpointConfig.isCloseConnectionsOnTranslationException()); // Register classes TimeStopSerialization.register(getKryo()); // Register self listener addListener(this); } /** * Opens a TCP only client. * * @see #connectAsync(int, InetAddress, int) */ public CompletableFuture connectAsync(String host, int tcpPort) { return connectAsync(0, host, tcpPort); } /** * Opens a TCP only client. * * @see #connectAsync(int, InetAddress, int, int) */ public CompletableFuture connectAsync(int timeout, String host, int tcpPort) { return connectAsync(timeout, host, tcpPort, -1); } /** * Opens a TCP and UDP client. * * @see #connectAsync(int, InetAddress, int, int) */ public CompletableFuture connectAsync(int timeout, String host, int tcpPort, int udpPort) { CompletableFuture future = new CompletableFuture<>(); CompletableFuture.runAsync(() -> { try { connect(timeout, InetAddress.getByName(host), tcpPort, udpPort); future.complete(true); } catch (IOException e) { future.completeExceptionally(e); } }); return future; } /** * Opens a TCP only client. * * @see #connectAsync(int, InetAddress, int, int) */ public CompletableFuture connectAsync(int timeout, InetAddress host, int tcpPort) { return connectAsync(timeout, host, tcpPort, -1); } /** * Opens a TCP and UDP client. Blocks until the connection is complete or * the timeout is reached. A timeout of zero is interpreted as an infinite * timeout. * * @param timeout Timeout * @param host Host * @param tcpPort TCP port * @param udpPort UDP port (-1 for no UDP) * * @return True if the connection was successful, otherwise is completed exceptionally (see {@link #connect(int, InetAddress, int, int)} */ public CompletableFuture connectAsync(int timeout, InetAddress host, int tcpPort, int udpPort) { CompletableFuture future = new CompletableFuture<>(); CompletableFuture.runAsync(() -> { try { connect(timeout, host, tcpPort, udpPort); future.complete(true); } catch (IOException e) { future.completeExceptionally(e); } }); return future; } /** * Opens a TCP and UDP client. Blocks until the connection is complete or * the timeout is reached. A timeout of zero is interpreted as an infinite * timeout. * * @param executor Executor * @param timeout Timeout * @param host Host * @param tcpPort TCP port * @param udpPort UDP port * * @return True if the connection was successful, otherwise is completed exceptionally (see {@link #connect(int, InetAddress, int, int)} */ public CompletableFuture connectAsync(Executor executor, int timeout, InetAddress host, int tcpPort, int udpPort) { CompletableFuture future = new CompletableFuture<>(); executor.execute(() -> { try { connect(timeout, host, tcpPort, udpPort); future.complete(true); } catch (IOException e) { future.completeExceptionally(e); } }); return future; } /** * Sends the given object to the server
Object will be translated before sending using {@link TimeStopTranslatorManager}. * * @param object Object to send * * @return Number of bytes sent (0 when object was translated to null) */ @Override public int sendTCP(Object object) { object = translatorManager.process(new TimeStopTranslator.Context(this, TimeStopTranslator.Context.Way.OUTBOUND), object); if (object == null) { return 0; } return super.sendTCP(object); } /** * Sends the given object to the server
Object will be translated before sending using {@link TimeStopTranslatorManager}. * * @param object Object to send * * @return Number of bytes sent (0 when object was translated to null) */ @Override public int sendUDP(Object object) { object = translatorManager.process(new TimeStopTranslator.Context(this, TimeStopTranslator.Context.Way.OUTBOUND), object); if (object == null) { return 0; } return super.sendUDP(object); } /** * Sends the given object to the server and waits for a response
Object will be translated before sending using * {@link TimeStopTranslatorManager}. * * @param object Object to send * @param responseClass Class of the response * @param onResponse Consumer that will be called when the response is received * @param Type of the response * * @return Number of bytes sent (0 when object was translated to null) */ public int sendTCPWithResponse(Object object, Class responseClass, Consumer onResponse) { this.createOneTimeListener(object, responseClass, onResponse); return sendTCP(object); } /** * Sends the given object to the server and waits for a response
Object will be translated before sending using * {@link TimeStopTranslatorManager}. * * @param object Object to send * @param responseClass Class of the response * @param timeout Timeout in milliseconds * @param onResponse Consumer that will be called when the response is received * @param onTimeout Runnable that will be called when the timeout elapsed * @param Type of the response * * @return Number of bytes sent (0 when object was translated to null) */ public int sendTCPWithResponse(Object object, Class responseClass, long timeout, Consumer onResponse, Runnable onTimeout) { this.createOneTimeListenerWithTimeout(object, responseClass, timeout, onResponse, onTimeout); return sendTCP(object); } /** * Sends the given object to the server and waits for a response
Object will be translated before sending using * {@link TimeStopTranslatorManager}. Default timeout will be used, from {@link EndpointConfig} * * @param object Object to send * @param responseClass Class of the response * @param onResponse Consumer that will be called when the response is received * @param onTimeout Runnable that will be called when the timeout elapsed * @param Type of the response * * @return Number of bytes sent (0 when object was translated to null) */ public int sendTCPWithResponse(Object object, Class responseClass, Consumer onResponse, Runnable onTimeout) { this.createOneTimeListenerWithTimeout(object, responseClass, endpointConfig.getDefaultResponseTimeoutMillis(), onResponse, onTimeout); return sendTCP(object); } @Override public void received(Connection connection, Object object) { object = translatorManager.process(new TimeStopTranslator.Context(connection, TimeStopTranslator.Context.Way.INBOUND), object); if (object == null) { return; } listenerManager.process(connection, object); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy