Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package dev.mayuna.timestop.networking.base;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;
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.listener.TimeStopResponseListener;
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 lombok.NonNull;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
/**
* TimeStopServer
*/
@Getter
public class TimeStopServer extends Server implements Listener, TimeStopResponseHandler {
protected final Timer timeoutTimer = new Timer();
protected final EndpointConfig endpointConfig;
protected TimeStopListenerManager listenerManager;
protected TimeStopTranslatorManager translatorManager;
/**
* Creates a new server with the given endpoint config
*
* @param endpointConfig Endpoint config
* @param writeBufferSize Write buffer size
* @param objectBufferSize Object buffer size
*/
public TimeStopServer(EndpointConfig endpointConfig, int writeBufferSize, int objectBufferSize) {
super(writeBufferSize, objectBufferSize);
this.endpointConfig = endpointConfig;
prepare();
}
/**
* Creates a new server with the given endpoint config
*
* @param endpointConfig Endpoint config
*/
public TimeStopServer(EndpointConfig endpointConfig) {
this(endpointConfig, NetworkConstants.WRITE_BUFFER_SIZE, NetworkConstants.OBJECT_BUFFER_SIZE);
}
/**
* Prepares the server for usage
*/
protected void prepare() {
Log.info("Preparing server...");
// Listener & translator manager
listenerManager = new TimeStopListenerManager(endpointConfig.getMaxThreads());
translatorManager = new TimeStopTranslatorManager(endpointConfig.isCloseConnectionsOnTranslationException());
// Register classes
TimeStopSerialization.register(getKryo());
// Register self listener
addListener(this);
}
/**
* Creates a new TimeStopConnection with current listener manager and translator manager
*
* @return Connection
*/
@Override
protected Connection newConnection() {
return new TimeStopConnection(endpointConfig, listenerManager, translatorManager);
}
/**
* Processes received objects. Translates them using {@link TimeStopTranslatorManager} and then passes them to {@link TimeStopListenerManager}.
*
* @param connection Connection
* @param object 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);
}
/**
* Sends the given object to the server Object will be translated before sending using {@link TimeStopTranslatorManager}.
*
* @param connection Connection
* @param object Object to send
*/
public void sendToTCP(Connection connection, Object object) {
sendToTCP(connection.getID(), object);
}
/**
* Sends the given object to the server and waits for a response Object will be translated before sending using
* {@link TimeStopTranslatorManager}.
*
* @param connection Connection
* @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 sendToTCPWithResponse(Connection connection, Object object, Class responseClass, Consumer onResponse) {
this.createOneTimeListener(object, responseClass, onResponse);
sendToTCP(connection, 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 connection Connection
* @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 sendToTCPWithResponse(Connection connection, Object object, Class responseClass, long timeout, Consumer onResponse, Runnable onTimeout) {
this.createOneTimeListenerWithTimeout(object, responseClass, timeout, onResponse, onTimeout);
sendToTCP(connection, 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 connection Connection
* @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 sendToTCPWithResponse(Connection connection, Object object, Class responseClass, Consumer onResponse, Runnable onTimeout) {
this.createOneTimeListenerWithTimeout(object, responseClass, endpointConfig.getDefaultResponseTimeoutMillis(), onResponse, onTimeout);
sendToTCP(connection, object);
}
}