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

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

package dev.mayuna.timestop.networking.base;

import com.esotericsoftware.kryonet.Connection;
import dev.mayuna.timestop.networking.base.listener.TimeStopListenerManager;
import dev.mayuna.timestop.networking.base.translator.TimeStopTranslator;
import dev.mayuna.timestop.networking.base.translator.TimeStopTranslatorManager;
import lombok.Getter;
import lombok.Setter;

import java.security.Key;
import java.util.Timer;
import java.util.function.Consumer;

@Getter
@Setter
public class TimeStopConnection extends Connection implements TimeStopResponseHandler {

    protected final Timer timeoutTimer = new Timer();
    protected final EndpointConfig endpointConfig;
    protected final TimeStopListenerManager listenerManager;
    protected final TimeStopTranslatorManager translatorManager;

    protected Key publicKey;
    protected boolean encryptDataSentOverNetwork = false;

    /**
     * Creates a new connection with the given translator manager
     *
     * @param endpointConfig    Endpoint config
     * @param listenerManager   listener manager
     * @param translatorManager Translator manager
     */
    public TimeStopConnection(EndpointConfig endpointConfig, TimeStopListenerManager listenerManager, TimeStopTranslatorManager translatorManager) {
        super();

        this.endpointConfig = endpointConfig;
        this.listenerManager = listenerManager;
        this.translatorManager = translatorManager;
    }

    /**
     * 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 connection 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 */ public void sendTCPWithResponse(Object object, Class responseClass, Consumer onResponse) { this.createOneTimeListener(object, responseClass, onResponse); sendTCP(object); } /** * Sends the given object to the server and asynchronously waits for a response
Object will be translated before sending using * {@link TimeStopTranslatorManager}. If the timeout elapses, onTimeout will be called. * * @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 */ public void sendTCPWithResponse(Object object, Class responseClass, long timeout, Consumer onResponse, Runnable onTimeout) { this.createOneTimeListenerWithTimeout(object, responseClass, timeout, onResponse, onTimeout); sendTCP(object); } /** * Sends the given object to the server and asynchronously waits for a response
Object will be translated before sending using * {@link TimeStopTranslatorManager}. If the timeout elapses, onTimeout will be called. 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 */ public void sendTCPWithResponse(Object object, Class responseClass, Consumer onResponse, Runnable onTimeout) { this.createOneTimeListenerWithTimeout(object, responseClass, endpointConfig.getDefaultResponseTimeoutMillis(), onResponse, onTimeout); sendTCP(object); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy