forked from UnlegitDqrk/unlegitlibrary
NetworkSystem rework
This commit is contained in:
3
.idea/copyright/profiles_settings.xml
generated
Normal file
3
.idea/copyright/profiles_settings.xml
generated
Normal file
@@ -0,0 +1,3 @@
|
||||
<component name="CopyrightManager">
|
||||
<settings default="UnlegitDqrk" />
|
||||
</component>
|
@@ -9,56 +9,121 @@
|
||||
package me.finn.unlegitlibrary.network.system.client;
|
||||
|
||||
import me.finn.unlegitlibrary.event.EventManager;
|
||||
import me.finn.unlegitlibrary.network.system.client.events.packets.received.C_PacketFailedReceivedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.client.events.packets.received.C_PacketReceivedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.client.events.packets.received.C_UnknownObjectReceivedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.client.events.packets.send.C_PacketFailedSendEvent;
|
||||
import me.finn.unlegitlibrary.network.system.client.events.packets.send.C_PacketSendEvent;
|
||||
import me.finn.unlegitlibrary.network.system.client.events.received.C_PacketFailedReceivedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.client.events.received.C_PacketReceivedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.client.events.received.C_UnknownObjectReceivedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.client.events.send.C_PacketFailedSendEvent;
|
||||
import me.finn.unlegitlibrary.network.system.client.events.send.C_PacketSendEvent;
|
||||
import me.finn.unlegitlibrary.network.system.client.events.state.C_ConnectedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.client.events.state.C_DisconnectedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.client.events.state.C_StoppedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.client.events.state.C_ReceiveThreadFailedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
import me.finn.unlegitlibrary.network.system.packets.PacketHandler;
|
||||
import me.finn.unlegitlibrary.network.system.packets.impl.ClientDisconnectPacket;
|
||||
import me.finn.unlegitlibrary.network.system.packets.impl.ClientIDPacket;
|
||||
import me.finn.unlegitlibrary.utils.DefaultMethodsOverrider;
|
||||
import me.finn.unlegitlibrary.utils.Logger;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
|
||||
public class NetworkClient extends DefaultMethodsOverrider {
|
||||
|
||||
public static class ClientBuilder extends DefaultMethodsOverrider {
|
||||
private String host;
|
||||
private int port;
|
||||
|
||||
private PacketHandler packetHandler;
|
||||
private EventManager eventManager;
|
||||
private Logger logger;
|
||||
|
||||
private int maxReconnectAttempts = 0;
|
||||
private int reconnectDelay = 3000;
|
||||
private int timeout = 3000;
|
||||
|
||||
public final NetworkClient build() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
|
||||
return new NetworkClient(host, port ,packetHandler, eventManager, logger, maxReconnectAttempts, reconnectDelay, timeout);
|
||||
}
|
||||
|
||||
public final ClientBuilder setEventManager(EventManager eventManager) {
|
||||
this.eventManager = eventManager;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setHost(String host) {
|
||||
this.host = host;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setLogger(Logger logger) {
|
||||
this.logger = logger;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setMaxReconnectAttempts(int maxReconnectAttempts) {
|
||||
this.maxReconnectAttempts = maxReconnectAttempts;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setPacketHandler(PacketHandler packetHandler) {
|
||||
this.packetHandler = packetHandler;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setPort(int port) {
|
||||
this.port = port;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setReconnectDelay(int reconnectDelay) {
|
||||
this.reconnectDelay = reconnectDelay;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setTimeout(int timeout) {
|
||||
this.timeout = timeout;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
private final String host;
|
||||
private final int port;
|
||||
|
||||
private final PacketHandler packetHandler;
|
||||
private final EventManager eventManager;
|
||||
private final Logger logger;
|
||||
|
||||
private final boolean debugLog;
|
||||
private final int maxAttempts;
|
||||
private final int attemptDelayInSec;
|
||||
private int currentAttempts;
|
||||
private final int maxReconnectAttempts;
|
||||
private final int reconnectDelay;
|
||||
|
||||
private Socket socket;
|
||||
private ObjectOutputStream objectOutputStream;
|
||||
private ObjectInputStream objectInputStream;
|
||||
private int timeout;
|
||||
private ObjectOutputStream outputStream;
|
||||
private ObjectInputStream inputStream;
|
||||
private int clientID = -1;
|
||||
private int attempt = 1;
|
||||
private boolean needClientID = false;
|
||||
private final Thread receiveThread = new Thread(this::receive);
|
||||
public final Thread receiveThread = new Thread(this::receive);
|
||||
|
||||
private NetworkClient(String host, int port, PacketHandler packetHandler, EventManager eventManager, boolean debugLog, int maxAttempts, int attemptDelayInSec) {
|
||||
private NetworkClient(String host, int port, PacketHandler packetHandler, EventManager eventManager, Logger logger, int reconnectAttempts, int reconnectDelay, int timeout) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.clientID = -1;
|
||||
this.timeout = timeout;
|
||||
|
||||
this.packetHandler = packetHandler;
|
||||
this.eventManager = eventManager;
|
||||
this.debugLog = debugLog;
|
||||
this.logger = logger;
|
||||
|
||||
this.maxAttempts = maxAttempts;
|
||||
this.attemptDelayInSec = attemptDelayInSec;
|
||||
this.attempt = 1;
|
||||
this.maxReconnectAttempts = reconnectAttempts;
|
||||
this.reconnectDelay = reconnectDelay;
|
||||
this.currentAttempts = 0;
|
||||
|
||||
this.packetHandler.setClientInstance(this);
|
||||
this.packetHandler.registerPacket(ClientDisconnectPacket.class);
|
||||
this.packetHandler.registerPacket(ClientIDPacket.class);
|
||||
}
|
||||
|
||||
public final int getClientID() {
|
||||
@@ -69,40 +134,36 @@ public class NetworkClient extends DefaultMethodsOverrider {
|
||||
return socket;
|
||||
}
|
||||
|
||||
public final ObjectOutputStream getObjectOutputStream() {
|
||||
return objectOutputStream;
|
||||
}
|
||||
|
||||
public final ObjectInputStream getObjectInputStream() {
|
||||
return objectInputStream;
|
||||
}
|
||||
|
||||
public final boolean isDebugLogEnabled() {
|
||||
return debugLog;
|
||||
}
|
||||
|
||||
public final boolean isAutoReconnectEnabled() {
|
||||
return maxAttempts != 0;
|
||||
}
|
||||
|
||||
public final PacketHandler getPacketHandler() {
|
||||
return packetHandler;
|
||||
}
|
||||
|
||||
public final boolean isNeedClientID() {
|
||||
return needClientID;
|
||||
}
|
||||
|
||||
public final int getPort() {
|
||||
return port;
|
||||
public final EventManager getEventManager() {
|
||||
return eventManager;
|
||||
}
|
||||
|
||||
public final String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public final Thread getReceiveThread() {
|
||||
return receiveThread;
|
||||
public final int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public final ObjectInputStream getInputStream() {
|
||||
return inputStream;
|
||||
}
|
||||
|
||||
public final ObjectOutputStream getOutputStream() {
|
||||
return outputStream;
|
||||
}
|
||||
|
||||
public final PacketHandler getPacketHandler() {
|
||||
return packetHandler;
|
||||
}
|
||||
|
||||
public final boolean isAutoReconnectEnabled() {
|
||||
return maxReconnectAttempts != 0;
|
||||
}
|
||||
|
||||
public final Logger getLogger() {
|
||||
return logger;
|
||||
}
|
||||
|
||||
public final boolean isConnected() {
|
||||
@@ -110,236 +171,144 @@ public class NetworkClient extends DefaultMethodsOverrider {
|
||||
&& receiveThread.isAlive() && !receiveThread.isInterrupted();
|
||||
}
|
||||
|
||||
public synchronized final void connect() throws IOException, InterruptedException {
|
||||
try {
|
||||
if (isConnected()) return;
|
||||
if (debugLog) System.out.println("Connecting to server...");
|
||||
public final Thread getReceiveThread() {
|
||||
return receiveThread;
|
||||
}
|
||||
|
||||
socket = new Socket(host, port);
|
||||
socket.setTcpNoDelay(false);
|
||||
|
||||
objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
|
||||
objectInputStream = new ObjectInputStream(socket.getInputStream());
|
||||
|
||||
needClientID = true;
|
||||
receiveThread.start();
|
||||
|
||||
objectOutputStream.writeObject("c2s_connect");
|
||||
objectOutputStream.writeObject(clientID);
|
||||
objectOutputStream.flush();
|
||||
|
||||
attempt = 1;
|
||||
if (debugLog) System.out.println("Connected to Server. Attempts: " + attempt);
|
||||
} catch (SocketException exception) {
|
||||
if (isAutoReconnectEnabled()) reconnect();
|
||||
else if (!receiveThread.isInterrupted()) throw exception;
|
||||
public final void setClientID(int clientID) {
|
||||
if (this.clientID == -1) {
|
||||
this.clientID = clientID;
|
||||
eventManager.executeEvent(new C_ConnectedEvent(this));
|
||||
}
|
||||
}
|
||||
|
||||
public final EventManager getEventManager() {
|
||||
return eventManager;
|
||||
}
|
||||
public synchronized boolean disconnect(boolean sendDisconnectPacket) {
|
||||
if (logger == null) System.out.println("Disconnecting from server...");
|
||||
else logger.info("Disconnecting from server...");
|
||||
|
||||
public synchronized final void disconnect() throws IOException {
|
||||
if (debugLog) System.out.println("Disconnecting from server...");
|
||||
|
||||
if (isConnected()) {
|
||||
objectOutputStream.writeObject("c2s_disconnect");
|
||||
objectOutputStream.writeObject(clientID);
|
||||
objectOutputStream.flush();
|
||||
}
|
||||
|
||||
eventManager.executeEvent(new C_DisconnectedEvent(this));
|
||||
if (debugLog) System.out.println("Disconnected from server.");
|
||||
stop();
|
||||
}
|
||||
|
||||
private synchronized final void stop() throws IOException {
|
||||
if (debugLog) System.out.println("Stopping client...");
|
||||
|
||||
if (isConnected()) {
|
||||
objectOutputStream.close();
|
||||
objectInputStream.close();
|
||||
socket.close();
|
||||
}
|
||||
|
||||
objectOutputStream = null;
|
||||
objectInputStream = null;
|
||||
socket = null;
|
||||
|
||||
needClientID = false;
|
||||
clientID = -1;
|
||||
attempt = 1;
|
||||
receiveThread.interrupt();
|
||||
|
||||
eventManager.executeEvent(new C_StoppedEvent(this));
|
||||
if (debugLog) System.out.println("Client stopped.");
|
||||
}
|
||||
|
||||
public final boolean sendPacket(Packet packet) throws IOException, ClassNotFoundException {
|
||||
if (!isConnected()) return false;
|
||||
|
||||
if (packetHandler.sendPacket(packet, objectOutputStream)) {
|
||||
eventManager.executeEvent(new C_PacketSendEvent(this, packet));
|
||||
return true;
|
||||
} else {
|
||||
eventManager.executeEvent(new C_PacketFailedSendEvent(this, packet));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private final void receive() {
|
||||
if (isConnected()) {
|
||||
if (sendDisconnectPacket) sendPacket(new ClientDisconnectPacket(clientID, true));
|
||||
|
||||
try {
|
||||
outputStream.close();
|
||||
inputStream.close();
|
||||
socket.close();
|
||||
} catch (IOException exception) {
|
||||
if (logger == null) System.err.println("Failed to close socket: " + exception.getMessage());
|
||||
else logger.exception("Failed to close socket", exception);
|
||||
}
|
||||
}
|
||||
|
||||
outputStream = null;
|
||||
inputStream = null;
|
||||
socket = null;
|
||||
|
||||
clientID = -1;
|
||||
currentAttempts = 0;
|
||||
|
||||
eventManager.executeEvent(new C_DisconnectedEvent(this));
|
||||
if (logger == null) System.out.println("Disconnected from server");
|
||||
else logger.info("Disconnected from server...");
|
||||
|
||||
if (maxReconnectAttempts != 0) {
|
||||
try {
|
||||
Thread.sleep(reconnectDelay);
|
||||
} catch (InterruptedException sleepThreadException) {
|
||||
if (logger == null) System.err.println("Reconnect exception: " + sleepThreadException.getMessage());
|
||||
else logger.exception("Reconnect exception", sleepThreadException);
|
||||
}
|
||||
|
||||
currentAttempts++;
|
||||
if (currentAttempts <= maxReconnectAttempts || maxReconnectAttempts < 0) return connect();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public synchronized final boolean connect() {
|
||||
if (isConnected()) return false;
|
||||
|
||||
if (logger == null) System.out.println("Trying to connect to " + host + ":" + port);
|
||||
else logger.info("Trying to connect to " + host + ":" + port);
|
||||
|
||||
try {
|
||||
socket = new Socket(host, port);
|
||||
socket.setTcpNoDelay(true);
|
||||
socket.setSoTimeout(timeout);
|
||||
|
||||
outputStream = new ObjectOutputStream(socket.getOutputStream());
|
||||
inputStream = new ObjectInputStream(socket.getInputStream());
|
||||
|
||||
receiveThread.start();
|
||||
|
||||
if (currentAttempts == 0) currentAttempts++;
|
||||
if (logger == null) System.out.println("Connected to " + host + ":" + port + " (Attempts: " + currentAttempts + ")");
|
||||
else logger.info("Connected to " + host + ":" + port + " (Attempts: " + currentAttempts + ")");
|
||||
|
||||
currentAttempts = 0;
|
||||
return true;
|
||||
} catch (IOException exception) {
|
||||
if (maxReconnectAttempts != 0) {
|
||||
try {
|
||||
Thread.sleep(reconnectDelay);
|
||||
} catch (InterruptedException sleepThreadException) {
|
||||
if (logger == null) System.err.println("Reconnect exception: " + sleepThreadException.getMessage());
|
||||
else logger.exception("Reconnect exception", sleepThreadException);
|
||||
}
|
||||
|
||||
currentAttempts++;
|
||||
if (currentAttempts <= maxReconnectAttempts || maxReconnectAttempts < 0) return connect();
|
||||
}
|
||||
|
||||
if (logger == null) System.err.println("Failed to connect to " + host + ":" + port + ": " + exception.getMessage());
|
||||
else logger.exception("Failed to connect to " + host + ":" + port, exception);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public final boolean sendPacket(Packet packet) {
|
||||
if (!isConnected()) return false;
|
||||
|
||||
try {
|
||||
if (packetHandler.sendPacket(packet, outputStream)) {
|
||||
eventManager.executeEvent(new C_PacketSendEvent(this, packet));
|
||||
return true;
|
||||
} else eventManager.executeEvent(new C_PacketFailedSendEvent(this, packet, null));
|
||||
} catch (IOException | ClassNotFoundException exception) {
|
||||
if (logger == null) System.err.println("Failed to send packet: " + exception.getMessage());
|
||||
else logger.exception("Failed to connect to send packet", exception);
|
||||
|
||||
eventManager.executeEvent(new C_PacketFailedSendEvent(this, packet, exception));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void receive() {
|
||||
if (!isConnected()) return;
|
||||
String command = "";
|
||||
|
||||
while (isConnected()) {
|
||||
Object received = objectInputStream.readObject();
|
||||
|
||||
if (received instanceof String) {
|
||||
command = (String) received;
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
Object received = inputStream.readObject();
|
||||
|
||||
if (received instanceof Integer) {
|
||||
int id = (Integer) received;
|
||||
if (command.equalsIgnoreCase("s2c_connect")) {
|
||||
clientID = id;
|
||||
command = "";
|
||||
|
||||
eventManager.executeEvent(new C_ConnectedEvent(this));
|
||||
continue;
|
||||
} else if (command.equalsIgnoreCase("s2c_disconnect")) {
|
||||
if (clientID != id) continue;
|
||||
eventManager.executeEvent(new C_DisconnectedEvent(this));
|
||||
command = "";
|
||||
|
||||
stop();
|
||||
break;
|
||||
} else if (packetHandler.getPacketByID(id) != null) {
|
||||
command = "";
|
||||
Packet packet = packetHandler.getPacketByID(id);
|
||||
|
||||
if (packetHandler.handlePacket(id, packet, objectInputStream))
|
||||
if (packetHandler.handlePacket(id, packet, inputStream))
|
||||
eventManager.executeEvent(new C_PacketReceivedEvent(this, packet));
|
||||
else eventManager.executeEvent(new C_PacketFailedReceivedEvent(this, packet));
|
||||
else eventManager.executeEvent(new C_PacketFailedReceivedEvent(this, packet, null));
|
||||
} else eventManager.executeEvent(new C_UnknownObjectReceivedEvent(this, received));
|
||||
} catch (IOException | ClassNotFoundException exception) {
|
||||
if (logger == null) System.err.println("Receive thread failed: " + exception.getMessage());
|
||||
else logger.exception("Receive thread failed", exception);
|
||||
|
||||
continue;
|
||||
eventManager.executeEvent(new C_ReceiveThreadFailedEvent(this, exception));
|
||||
}
|
||||
}
|
||||
|
||||
eventManager.executeEvent(new C_UnknownObjectReceivedEvent(this, received));
|
||||
}
|
||||
} catch (EOFException exception) {
|
||||
attempt = 1;
|
||||
if (isAutoReconnectEnabled()) reconnect();
|
||||
else if (!receiveThread.isInterrupted()) {
|
||||
try {
|
||||
stop();
|
||||
} catch (IOException exception1) {
|
||||
exception.printStackTrace();
|
||||
exception1.printStackTrace();
|
||||
}
|
||||
}
|
||||
} catch (ClassNotFoundException exception) {
|
||||
exception.printStackTrace();
|
||||
} catch (IOException exception) {
|
||||
eventManager.executeEvent(new C_StoppedEvent(this));
|
||||
if (!receiveThread.isInterrupted()) {
|
||||
try {
|
||||
stop();
|
||||
} catch (IOException exception1) {
|
||||
exception.printStackTrace();
|
||||
exception1.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private final void reconnect() {
|
||||
if (isAutoReconnectEnabled()) {
|
||||
if (isConnected()) {
|
||||
try {
|
||||
disconnect();
|
||||
} catch (IOException exception) {
|
||||
if (maxAttempts > 0 && attempt > maxAttempts) {
|
||||
eventManager.executeEvent(new C_StoppedEvent(this));
|
||||
if (!receiveThread.isInterrupted()) exception.printStackTrace();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (debugLog) System.out.println("Trying to reconnect... (Attempt: " + attempt++ + ")");
|
||||
|
||||
try {
|
||||
Thread.sleep(attemptDelayInSec * 1000L);
|
||||
connect();
|
||||
} catch (InterruptedException | IOException exception) {
|
||||
if (maxAttempts == -1) reconnect();
|
||||
else if (attempt <= maxAttempts) reconnect();
|
||||
else {
|
||||
eventManager.executeEvent(new C_StoppedEvent(this));
|
||||
if (!receiveThread.isInterrupted()) exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
stop();
|
||||
} catch (IOException exception) {
|
||||
eventManager.executeEvent(new C_StoppedEvent(this));
|
||||
if (!receiveThread.isInterrupted()) exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class ClientBuilder {
|
||||
private int port;
|
||||
private String host;
|
||||
private PacketHandler packetHandler = new PacketHandler();
|
||||
private EventManager eventManager = new EventManager();
|
||||
private boolean debugLog = false;
|
||||
private int maxAttempts = 0;
|
||||
private int attemptDelayInSec = 1;
|
||||
|
||||
public final ClientBuilder enableDebugLog() {
|
||||
this.debugLog = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setEventManager(EventManager eventManager) {
|
||||
this.eventManager = eventManager;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setPort(int port) {
|
||||
this.port = port;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setHost(String host) {
|
||||
this.host = host;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setPacketHandler(PacketHandler packetHandler) {
|
||||
this.packetHandler = packetHandler;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setAttemptDelayInSeconds(int attemptDelayInSec) {
|
||||
this.attemptDelayInSec = attemptDelayInSec;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setMaxAttempts(int maxAttempts) {
|
||||
this.maxAttempts = maxAttempts;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final NetworkClient build() {
|
||||
return new NetworkClient(host, port, packetHandler, eventManager, debugLog, maxAttempts, attemptDelayInSec);
|
||||
}
|
||||
disconnect(false);
|
||||
}
|
||||
}
|
||||
|
@@ -6,7 +6,7 @@
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.client.events.packets.received;
|
||||
package me.finn.unlegitlibrary.network.system.client.events.received;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
@@ -14,12 +14,14 @@ import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
|
||||
public class C_PacketFailedReceivedEvent extends Event {
|
||||
|
||||
public final NetworkClient networkClient;
|
||||
public final NetworkClient client;
|
||||
public final Packet packet;
|
||||
public final Exception exception;
|
||||
|
||||
public C_PacketFailedReceivedEvent(NetworkClient networkClient, Packet packet) {
|
||||
this.networkClient = networkClient;
|
||||
public C_PacketFailedReceivedEvent(NetworkClient client, Packet packet, Exception exception) {
|
||||
this.client = client;
|
||||
this.packet = packet;
|
||||
this.exception = exception;
|
||||
}
|
||||
|
||||
@Override
|
@@ -6,7 +6,7 @@
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.client.events.packets.received;
|
||||
package me.finn.unlegitlibrary.network.system.client.events.received;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
@@ -14,11 +14,11 @@ import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
|
||||
public class C_PacketReceivedEvent extends Event {
|
||||
|
||||
public final NetworkClient networkClient;
|
||||
public final NetworkClient client;
|
||||
public final Packet packet;
|
||||
|
||||
public C_PacketReceivedEvent(NetworkClient networkClient, Packet packet) {
|
||||
this.networkClient = networkClient;
|
||||
public C_PacketReceivedEvent(NetworkClient client, Packet packet) {
|
||||
this.client = client;
|
||||
this.packet = packet;
|
||||
}
|
||||
|
@@ -6,18 +6,19 @@
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.client.events.packets.received;
|
||||
package me.finn.unlegitlibrary.network.system.client.events.received;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
|
||||
public class C_UnknownObjectReceivedEvent extends Event {
|
||||
public final NetworkClient networkClient;
|
||||
public final Object object;
|
||||
|
||||
public C_UnknownObjectReceivedEvent(NetworkClient networkClient, Object object) {
|
||||
this.networkClient = networkClient;
|
||||
this.object = object;
|
||||
public final NetworkClient client;
|
||||
public final Object received;
|
||||
|
||||
public C_UnknownObjectReceivedEvent(NetworkClient client, Object received) {
|
||||
this.client = client;
|
||||
this.received = received;
|
||||
}
|
||||
|
||||
@Override
|
@@ -6,7 +6,7 @@
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.client.events.packets.send;
|
||||
package me.finn.unlegitlibrary.network.system.client.events.send;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
@@ -14,12 +14,14 @@ import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
|
||||
public class C_PacketFailedSendEvent extends Event {
|
||||
|
||||
public final NetworkClient networkClient;
|
||||
public final NetworkClient client;
|
||||
public final Packet packet;
|
||||
public final Exception exception;
|
||||
|
||||
public C_PacketFailedSendEvent(NetworkClient networkClient, Packet packet) {
|
||||
this.networkClient = networkClient;
|
||||
public C_PacketFailedSendEvent(NetworkClient client, Packet packet, Exception exception) {
|
||||
this.client = client;
|
||||
this.packet = packet;
|
||||
this.exception = exception;
|
||||
}
|
||||
|
||||
@Override
|
@@ -6,7 +6,7 @@
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.client.events.packets.send;
|
||||
package me.finn.unlegitlibrary.network.system.client.events.send;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
@@ -14,11 +14,11 @@ import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
|
||||
public class C_PacketSendEvent extends Event {
|
||||
|
||||
public final NetworkClient networkClient;
|
||||
public final NetworkClient client;
|
||||
public final Packet packet;
|
||||
|
||||
public C_PacketSendEvent(NetworkClient networkClient, Packet packet) {
|
||||
this.networkClient = networkClient;
|
||||
public C_PacketSendEvent(NetworkClient client, Packet packet) {
|
||||
this.client = client;
|
||||
this.packet = packet;
|
||||
}
|
||||
|
@@ -13,10 +13,10 @@ import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
|
||||
public class C_ConnectedEvent extends Event {
|
||||
|
||||
public final NetworkClient networkClient;
|
||||
public final NetworkClient client;
|
||||
|
||||
public C_ConnectedEvent(NetworkClient networkClient) {
|
||||
this.networkClient = networkClient;
|
||||
public C_ConnectedEvent(NetworkClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -13,10 +13,10 @@ import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
|
||||
public class C_DisconnectedEvent extends Event {
|
||||
|
||||
public final NetworkClient networkClient;
|
||||
public final NetworkClient client;
|
||||
|
||||
public C_DisconnectedEvent(NetworkClient networkClient) {
|
||||
this.networkClient = networkClient;
|
||||
public C_DisconnectedEvent(NetworkClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -11,12 +11,14 @@ package me.finn.unlegitlibrary.network.system.client.events.state;
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
|
||||
public class C_StoppedEvent extends Event {
|
||||
public class C_ReceiveThreadFailedEvent extends Event {
|
||||
|
||||
public final NetworkClient networkClient;
|
||||
public final NetworkClient client;
|
||||
public final Exception exception;
|
||||
|
||||
public C_StoppedEvent(NetworkClient networkClient) {
|
||||
this.networkClient = networkClient;
|
||||
public C_ReceiveThreadFailedEvent(NetworkClient client, Exception exception) {
|
||||
this.client = client;
|
||||
this.exception = exception;
|
||||
}
|
||||
|
||||
@Override
|
@@ -6,6 +6,14 @@
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.packets;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -24,8 +32,8 @@ public abstract class Packet {
|
||||
return id;
|
||||
}
|
||||
|
||||
public abstract void write(ObjectOutputStream outputStream) throws IOException, ClassNotFoundException;
|
||||
public abstract void write(PacketHandler packetHandler, ObjectOutputStream outputStream) throws IOException, ClassNotFoundException;
|
||||
|
||||
public abstract void read(ObjectInputStream outputStream) throws IOException, ClassNotFoundException;
|
||||
public abstract void read(PacketHandler packetHandler, ObjectInputStream outputStream) throws IOException, ClassNotFoundException;
|
||||
}
|
||||
|
||||
|
@@ -6,8 +6,18 @@
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.packets;
|
||||
|
||||
import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
import me.finn.unlegitlibrary.network.system.server.NetworkServer;
|
||||
import me.finn.unlegitlibrary.utils.DefaultMethodsOverrider;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -21,6 +31,25 @@ public class PacketHandler extends DefaultMethodsOverrider {
|
||||
|
||||
private final Map<Integer, Class<? extends Packet>> packets = new HashMap<>();
|
||||
|
||||
private NetworkClient clientInstance;
|
||||
private NetworkServer serverInstance;
|
||||
|
||||
public final NetworkClient getClientInstance() {
|
||||
return clientInstance;
|
||||
}
|
||||
|
||||
public final NetworkServer getServerInstance() {
|
||||
return serverInstance;
|
||||
}
|
||||
|
||||
public void setClientInstance(NetworkClient clientInstance) {
|
||||
if (this.clientInstance == null) this.clientInstance = clientInstance;
|
||||
}
|
||||
|
||||
public void setServerInstance(NetworkServer serverInstance) {
|
||||
if (this.serverInstance == null) this.serverInstance = serverInstance;
|
||||
}
|
||||
|
||||
public final boolean isPacketIDRegistered(int id) {
|
||||
return packets.containsKey(id);
|
||||
}
|
||||
@@ -40,7 +69,9 @@ public class PacketHandler extends DefaultMethodsOverrider {
|
||||
Packet packet = packetClass.getDeclaredConstructor().newInstance();
|
||||
int id = packet.getPacketID();
|
||||
|
||||
if (isPacketIDRegistered(id)) return false;
|
||||
if (!(packet instanceof SystemPacket) && isPacketIDRegistered(id)) return false;
|
||||
else if (isPacketIDRegistered(id)) packets.remove(id);
|
||||
|
||||
packets.put(id, packetClass);
|
||||
return true;
|
||||
}
|
||||
@@ -49,7 +80,7 @@ public class PacketHandler extends DefaultMethodsOverrider {
|
||||
if (!isPacketIDRegistered(id) || (packet != null && id != packet.getPacketID()) || (packet != null && !isPacketIDRegistered(packet.getPacketID())))
|
||||
return false;
|
||||
|
||||
packet.read(inputStream);
|
||||
packet.read(this, inputStream);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -58,7 +89,7 @@ public class PacketHandler extends DefaultMethodsOverrider {
|
||||
if (!isPacketIDRegistered(id)) return false;
|
||||
|
||||
outputStream.writeObject(id);
|
||||
packet.write(outputStream);
|
||||
packet.write(this, outputStream);
|
||||
outputStream.flush();
|
||||
|
||||
return true;
|
||||
|
@@ -0,0 +1,15 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.packets;
|
||||
|
||||
public abstract class SystemPacket extends Packet {
|
||||
protected SystemPacket(int id) {
|
||||
super(id);
|
||||
}
|
||||
}
|
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.packets.impl;
|
||||
|
||||
import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
import me.finn.unlegitlibrary.network.system.packets.PacketHandler;
|
||||
import me.finn.unlegitlibrary.network.system.packets.SystemPacket;
|
||||
import me.finn.unlegitlibrary.network.system.server.NetworkServer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
public class ClientDisconnectPacket extends SystemPacket {
|
||||
public ClientDisconnectPacket() {
|
||||
super(-2);
|
||||
}
|
||||
|
||||
private int clientID;
|
||||
private boolean fromClient;
|
||||
|
||||
public ClientDisconnectPacket(int clientID, boolean fromClient) {
|
||||
this();
|
||||
this.clientID = clientID;
|
||||
this.fromClient = fromClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(PacketHandler packetHandler, ObjectOutputStream outputStream) throws IOException, ClassNotFoundException {
|
||||
outputStream.writeBoolean(fromClient);
|
||||
outputStream.writeInt(clientID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(PacketHandler packetHandler, ObjectInputStream outputStream) throws IOException, ClassNotFoundException {
|
||||
fromClient = outputStream.readBoolean();
|
||||
clientID = outputStream.readInt();
|
||||
|
||||
if (fromClient) packetHandler.getServerInstance().getConnectionHandlerByID(clientID).disconnect(false);
|
||||
else packetHandler.getClientInstance().disconnect(false);
|
||||
}
|
||||
}
|
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.packets.impl;
|
||||
|
||||
import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
import me.finn.unlegitlibrary.network.system.packets.PacketHandler;
|
||||
import me.finn.unlegitlibrary.network.system.packets.SystemPacket;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
public class ClientIDPacket extends SystemPacket {
|
||||
|
||||
public ClientIDPacket() {
|
||||
super(-1);
|
||||
}
|
||||
|
||||
private int clientID;
|
||||
|
||||
public ClientIDPacket(int clientID) {
|
||||
this();
|
||||
this.clientID = clientID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(PacketHandler packetHandler, ObjectOutputStream outputStream) throws IOException, ClassNotFoundException {
|
||||
outputStream.writeInt(clientID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(PacketHandler packetHandler, ObjectInputStream outputStream) throws IOException, ClassNotFoundException {
|
||||
clientID = outputStream.readInt();
|
||||
packetHandler.getClientInstance().setClientID(clientID);
|
||||
}
|
||||
}
|
@@ -1,181 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.server;
|
||||
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
import me.finn.unlegitlibrary.network.system.server.events.client.packets.received.S_PacketFailedReceivedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.server.events.client.packets.received.S_PacketReceivedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.server.events.client.packets.received.S_UnknownObjectReceivedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.server.events.client.packets.send.S_PacketFailedSendEvent;
|
||||
import me.finn.unlegitlibrary.network.system.server.events.client.packets.send.S_PacketSendEvent;
|
||||
import me.finn.unlegitlibrary.network.system.server.events.client.state.S_ClientConnectedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.server.events.client.state.S_ClientDisconnectedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.server.events.client.state.S_ClientStoppedEvent;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
|
||||
public class ClientHandler {
|
||||
|
||||
private final NetworkServer networkServer;
|
||||
private Socket socket;
|
||||
private ObjectOutputStream objectOutputStream;
|
||||
private ObjectInputStream objectInputStream;
|
||||
private int clientID; private final Thread receiveThread = new Thread(this::receive);
|
||||
|
||||
public ClientHandler(NetworkServer networkServer, Socket socket, int clientID) throws IOException {
|
||||
this.networkServer = networkServer;
|
||||
this.socket = socket;
|
||||
this.clientID = clientID;
|
||||
|
||||
objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
|
||||
objectInputStream = new ObjectInputStream(socket.getInputStream());
|
||||
|
||||
receiveThread.start();
|
||||
}
|
||||
|
||||
public final int getClientID() {
|
||||
return clientID;
|
||||
}
|
||||
|
||||
public final Socket getSocket() {
|
||||
return socket;
|
||||
}
|
||||
|
||||
public final NetworkServer getNetworkServer() {
|
||||
return networkServer;
|
||||
}
|
||||
|
||||
public final ObjectOutputStream getObjectOutputStream() {
|
||||
return objectOutputStream;
|
||||
}
|
||||
|
||||
public final ObjectInputStream getObjectInputStream() {
|
||||
return objectInputStream;
|
||||
}
|
||||
|
||||
public final Thread getReceiveThread() {
|
||||
return receiveThread;
|
||||
}
|
||||
|
||||
public final boolean isConnected() {
|
||||
return networkServer.isRunning() && socket != null && socket.isConnected() && !socket.isClosed() && socket.isBound()
|
||||
&& receiveThread.isAlive() && !receiveThread.isInterrupted();
|
||||
}
|
||||
|
||||
public synchronized final void disconnect() throws IOException {
|
||||
if (isConnected()) {
|
||||
objectOutputStream.writeUTF("s2c_disconnect");
|
||||
objectOutputStream.writeInt(clientID);
|
||||
objectOutputStream.flush();
|
||||
}
|
||||
|
||||
networkServer.getEventManager().executeEvent(new S_ClientDisconnectedEvent(this));
|
||||
stop();
|
||||
}
|
||||
|
||||
private synchronized final void stop() throws IOException {
|
||||
if (isConnected()) {
|
||||
objectOutputStream.close();
|
||||
objectInputStream.close();
|
||||
socket.close();
|
||||
}
|
||||
|
||||
objectOutputStream = null;
|
||||
objectInputStream = null;
|
||||
socket = null;
|
||||
|
||||
clientID = -1;
|
||||
receiveThread.interrupt();
|
||||
|
||||
synchronized (networkServer.getClientHandlers()) { networkServer.getClientHandlers().remove(this); }
|
||||
networkServer.getEventManager().executeEvent(new S_ClientStoppedEvent(this));
|
||||
}
|
||||
|
||||
public final boolean sendPacket(Packet packet) throws IOException, ClassNotFoundException {
|
||||
if (!isConnected()) return false;
|
||||
|
||||
if (networkServer.getPacketHandler().sendPacket(packet, objectOutputStream)) {
|
||||
networkServer.getEventManager().executeEvent(new S_PacketSendEvent(this, packet));
|
||||
return true;
|
||||
} else {
|
||||
networkServer.getEventManager().executeEvent(new S_PacketFailedSendEvent(this, packet));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private final void receive() {
|
||||
if (!isConnected()) return;
|
||||
|
||||
try {
|
||||
String command = "";
|
||||
|
||||
while (isConnected()) {
|
||||
Object received = objectInputStream.readObject();
|
||||
|
||||
if (received instanceof String) {
|
||||
command = (String) received;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (received instanceof Integer) {
|
||||
int id = (Integer) received;
|
||||
if (command.equalsIgnoreCase("c2s_connect")) {
|
||||
if (clientID == id) continue;
|
||||
command = "";
|
||||
|
||||
objectOutputStream.writeObject("s2c_connect");
|
||||
objectOutputStream.writeObject(clientID);
|
||||
objectOutputStream.flush();
|
||||
|
||||
networkServer.getEventManager().executeEvent(new S_ClientConnectedEvent(this));
|
||||
continue;
|
||||
} else if (command.equalsIgnoreCase("c2s_disconnect")) {
|
||||
if (clientID != id) continue;
|
||||
networkServer.getEventManager().executeEvent(new S_ClientDisconnectedEvent(this));
|
||||
command = "";
|
||||
|
||||
stop();
|
||||
break;
|
||||
} else if (networkServer.getPacketHandler().getPacketByID(id) != null) {
|
||||
command = "";
|
||||
Packet packet = networkServer.getPacketHandler().getPacketByID(id);
|
||||
|
||||
if (networkServer.getPacketHandler().handlePacket(id, packet, objectInputStream))
|
||||
networkServer.getEventManager().executeEvent(new S_PacketReceivedEvent(this, packet));
|
||||
else
|
||||
networkServer.getEventManager().executeEvent(new S_PacketFailedReceivedEvent(this, packet));
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
networkServer.getEventManager().executeEvent(new S_UnknownObjectReceivedEvent(this, received));
|
||||
}
|
||||
} catch (SocketException exception) {
|
||||
networkServer.getEventManager().executeEvent(new S_ClientDisconnectedEvent(this));
|
||||
try {
|
||||
stop();
|
||||
} catch (IOException ioException) {
|
||||
networkServer.getClientHandlers().remove(this);
|
||||
networkServer.getEventManager().executeEvent(new S_ClientStoppedEvent(this));
|
||||
|
||||
exception.printStackTrace();
|
||||
ioException.printStackTrace();
|
||||
}
|
||||
} catch (IOException | ClassNotFoundException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.server;
|
||||
|
||||
import me.finn.unlegitlibrary.network.system.packets.impl.ClientIDPacket;
|
||||
import me.finn.unlegitlibrary.network.system.packets.impl.ClientDisconnectPacket;
|
||||
import me.finn.unlegitlibrary.network.system.server.events.packets.received.S_PacketFailedReceivedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.server.events.packets.received.S_PacketReceivedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.server.events.packets.received.S_ReceiveThreadFailedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.server.events.packets.received.S_UnknownObjectReceivedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.server.events.packets.send.S_PacketFailedSendEvent;
|
||||
import me.finn.unlegitlibrary.network.system.server.events.packets.send.S_PacketSendEvent;
|
||||
import me.finn.unlegitlibrary.network.system.server.events.state.connection.S_ConnectionHandlerConnectedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.server.events.state.connection.S_ConnectionHandlerDisconnectedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.Socket;
|
||||
|
||||
public class ConnectionHandler {
|
||||
|
||||
private NetworkServer server;
|
||||
private Socket socket;
|
||||
private int clientID;
|
||||
|
||||
private ObjectOutputStream outputStream;
|
||||
private ObjectInputStream inputStream;
|
||||
public final Thread receiveThread = new Thread(this::receive);
|
||||
|
||||
public ConnectionHandler(NetworkServer server, Socket socket, int clientID) throws IOException {
|
||||
this.server = server;
|
||||
this.socket = socket;
|
||||
this.clientID = clientID;
|
||||
|
||||
outputStream = new ObjectOutputStream(socket.getOutputStream());
|
||||
inputStream = new ObjectInputStream(socket.getInputStream());
|
||||
|
||||
receiveThread.start();
|
||||
|
||||
sendPacket(new ClientIDPacket(clientID));
|
||||
server.getEventManager().executeEvent(new S_ConnectionHandlerConnectedEvent(this));
|
||||
}
|
||||
|
||||
public final int getClientID() {
|
||||
return clientID;
|
||||
}
|
||||
|
||||
public final NetworkServer getServer() {
|
||||
return server;
|
||||
}
|
||||
|
||||
public final Socket getSocket() {
|
||||
return socket;
|
||||
}
|
||||
|
||||
public final Thread getReceiveThread() {
|
||||
return receiveThread;
|
||||
}
|
||||
|
||||
public final boolean isConnected() {
|
||||
return server.isRunning() && socket != null && socket.isConnected() && !socket.isClosed() && socket.isBound()
|
||||
&& receiveThread.isAlive() && !receiveThread.isInterrupted();
|
||||
}
|
||||
|
||||
public final boolean sendPacket(Packet packet) {
|
||||
if (!isConnected()) return false;
|
||||
|
||||
try {
|
||||
if (server.getPacketHandler().sendPacket(packet, outputStream)) {
|
||||
server.getEventManager().executeEvent(new S_PacketSendEvent(this, packet));
|
||||
return true;
|
||||
} else server.getEventManager().executeEvent(new S_PacketFailedSendEvent(this, packet, null));
|
||||
} catch (IOException | ClassNotFoundException exception) {
|
||||
if (server.getLogger() == null) System.err.println("Failed to send packet: " + exception.getMessage());
|
||||
else server.getLogger().exception("Failed to connect to send packet", exception);
|
||||
|
||||
server.getEventManager().executeEvent(new S_PacketFailedSendEvent(this, packet, exception));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public synchronized boolean disconnect(boolean sendDisconnectPacket) {
|
||||
if (server.getLogger() == null) System.out.println("Disconnecting from server...");
|
||||
else server.getLogger().info("Disconnecting from server...");
|
||||
|
||||
receiveThread.interrupt();
|
||||
|
||||
if (isConnected()) {
|
||||
if (sendDisconnectPacket) sendPacket(new ClientDisconnectPacket(clientID, false));
|
||||
|
||||
try {
|
||||
outputStream.close();
|
||||
inputStream.close();
|
||||
socket.close();
|
||||
} catch (IOException exception) {
|
||||
if (server.getLogger() == null) System.err.println("Failed to close socket: " + exception.getMessage());
|
||||
else server.getLogger().exception("Failed to close socket", exception);
|
||||
}
|
||||
}
|
||||
|
||||
outputStream = null;
|
||||
inputStream = null;
|
||||
socket = null;
|
||||
|
||||
server.getConnectionHandlers().remove(this);
|
||||
clientID = -1;
|
||||
|
||||
server.getEventManager().executeEvent(new S_ConnectionHandlerDisconnectedEvent(this));
|
||||
if (server.getLogger() == null) System.out.println("Disconnected from server");
|
||||
else server.getLogger().info("Disconnected from server...");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void receive() {
|
||||
if (!isConnected()) return;
|
||||
|
||||
while (isConnected()) {
|
||||
try {
|
||||
Object received = inputStream.readObject();
|
||||
|
||||
if (received instanceof Integer) {
|
||||
int id = (Integer) received;
|
||||
Packet packet = server.getPacketHandler().getPacketByID(id);
|
||||
if (server.getPacketHandler().handlePacket(id, packet, inputStream))
|
||||
server.getEventManager().executeEvent(new S_PacketReceivedEvent(this, packet));
|
||||
else server.getEventManager().executeEvent(new S_PacketFailedReceivedEvent(this, packet, null));
|
||||
} else server.getEventManager().executeEvent(new S_UnknownObjectReceivedEvent(this, received));
|
||||
} catch (IOException | ClassNotFoundException exception) {
|
||||
if (server.getLogger() == null) System.err.println("Receive thread failed: " + exception.getMessage());
|
||||
else server.getLogger().exception("Receive thread failed", exception);
|
||||
|
||||
server.getEventManager().executeEvent(new S_ReceiveThreadFailedEvent(this, exception));
|
||||
}
|
||||
}
|
||||
|
||||
disconnect(false);
|
||||
}
|
||||
}
|
@@ -9,209 +9,41 @@
|
||||
package me.finn.unlegitlibrary.network.system.server;
|
||||
|
||||
import me.finn.unlegitlibrary.event.EventManager;
|
||||
import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
import me.finn.unlegitlibrary.network.system.packets.impl.ClientDisconnectPacket;
|
||||
import me.finn.unlegitlibrary.network.system.packets.impl.ClientIDPacket;
|
||||
import me.finn.unlegitlibrary.network.system.server.events.connection.S_IncomingConnectionEvent;
|
||||
import me.finn.unlegitlibrary.network.system.server.events.connection.S_IncomingConnectionThreadFailedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.server.events.state.server.S_StartedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.server.events.state.server.S_StoppedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
import me.finn.unlegitlibrary.network.system.packets.PacketHandler;
|
||||
import me.finn.unlegitlibrary.network.system.server.events.server.S_StartedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.server.events.server.S_StoppedEvent;
|
||||
import me.finn.unlegitlibrary.utils.DefaultMethodsOverrider;
|
||||
import me.finn.unlegitlibrary.utils.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.*;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class NetworkServer {
|
||||
private final int port;
|
||||
private final boolean debugLog;
|
||||
public class NetworkServer extends DefaultMethodsOverrider {
|
||||
|
||||
private final PacketHandler packetHandler;
|
||||
private final EventManager eventManager;
|
||||
|
||||
private final int maxAttempts;
|
||||
private final int attemptDelayInSec;
|
||||
|
||||
private final List<ClientHandler> clientHandlers = new ArrayList<>();
|
||||
private final Thread incomingConnectionThread = new Thread(this::incomingConnection);
|
||||
|
||||
private ServerSocket serverSocket;
|
||||
private int attempt;
|
||||
|
||||
private NetworkServer(int port, boolean debugLog, PacketHandler packetHandler, EventManager eventManager, int maxAttempts, int attemptDelayInSec) {
|
||||
this.port = port;
|
||||
this.debugLog = debugLog;
|
||||
|
||||
this.packetHandler = packetHandler;
|
||||
this.eventManager = eventManager;
|
||||
|
||||
this.maxAttempts = maxAttempts;
|
||||
this.attemptDelayInSec = attemptDelayInSec;
|
||||
this.attempt = 1;
|
||||
}
|
||||
|
||||
public final int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public final PacketHandler getPacketHandler() {
|
||||
return packetHandler;
|
||||
}
|
||||
|
||||
public final boolean isAutoRestartEnabled() {
|
||||
return maxAttempts != 0 && !incomingConnectionThread.isInterrupted();
|
||||
}
|
||||
|
||||
public final boolean isDebugLogEnabled() {
|
||||
return debugLog;
|
||||
}
|
||||
|
||||
public final ServerSocket getServerSocket() {
|
||||
return serverSocket;
|
||||
}
|
||||
|
||||
public final List<ClientHandler> getClientHandlers() {
|
||||
return clientHandlers;
|
||||
}
|
||||
|
||||
public final Thread getIncomingConnectionThread() {
|
||||
return incomingConnectionThread;
|
||||
}
|
||||
|
||||
public final boolean isRunning() {
|
||||
return serverSocket != null && !serverSocket.isClosed() && serverSocket.isBound() &&
|
||||
incomingConnectionThread.isAlive() && !incomingConnectionThread.isInterrupted();
|
||||
}
|
||||
|
||||
public synchronized final void start() throws IOException, InterruptedException {
|
||||
try {
|
||||
if (isRunning()) return;
|
||||
if (debugLog) System.out.println("Starting server...");
|
||||
|
||||
clientHandlers.clear();
|
||||
|
||||
serverSocket = new ServerSocket(port);
|
||||
incomingConnectionThread.start();
|
||||
|
||||
attempt = 1;
|
||||
eventManager.executeEvent(new S_StartedEvent(this));
|
||||
|
||||
if (debugLog) System.out.println("Server started on port " + port + ". Attempts: " + attempt);
|
||||
} catch (BindException exception) {
|
||||
if (isAutoRestartEnabled()) restart();
|
||||
else if (!incomingConnectionThread.isInterrupted()) throw exception;
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized final void stop() throws IOException {
|
||||
if (!isRunning()) return;
|
||||
if (debugLog) System.out.println("Stopping server...");
|
||||
|
||||
List<ClientHandler> handlersToDisconnect = new ArrayList<>(clientHandlers);
|
||||
|
||||
handlersToDisconnect.forEach(clientHandler -> {
|
||||
try {
|
||||
clientHandler.disconnect();
|
||||
} catch (IOException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
});
|
||||
|
||||
handlersToDisconnect.clear();
|
||||
clientHandlers.clear();
|
||||
|
||||
serverSocket.close();
|
||||
incomingConnectionThread.interrupt();
|
||||
|
||||
serverSocket = null;
|
||||
|
||||
eventManager.executeEvent(new S_StoppedEvent(this));
|
||||
if (debugLog) System.out.println("Server stopped.");
|
||||
}
|
||||
|
||||
public final boolean sendPacket(ClientHandler clientHandler, Packet packet) throws IOException, ClassNotFoundException {
|
||||
return clientHandler.sendPacket(packet);
|
||||
}
|
||||
|
||||
private final void incomingConnection() {
|
||||
try {
|
||||
if (!isRunning()) return;
|
||||
|
||||
while (isRunning()) {
|
||||
Socket socket = serverSocket.accept();
|
||||
if (socket == null) continue;
|
||||
|
||||
socket.setTcpNoDelay(false);
|
||||
if (debugLog) System.out.println("New incoming connection...");
|
||||
clientHandlers.add(new ClientHandler(this, socket, clientHandlers.size() + 1));
|
||||
}
|
||||
} catch (SocketException exception) {
|
||||
if (isAutoRestartEnabled()) restart();
|
||||
else if (!incomingConnectionThread.isInterrupted()) exception.printStackTrace();
|
||||
} catch (IOException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private final void restart() {
|
||||
if (isAutoRestartEnabled()) {
|
||||
if (isRunning()) {
|
||||
try {
|
||||
stop();
|
||||
} catch (IOException exception) {
|
||||
if (maxAttempts > 0 && attempt > maxAttempts) {
|
||||
eventManager.executeEvent(new S_StoppedEvent(this));
|
||||
exception.printStackTrace();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (debugLog) System.out.println("Trying to restart... (Attempt: " + attempt++ + ")");
|
||||
|
||||
try {
|
||||
Thread.sleep(attemptDelayInSec * 1000L);
|
||||
start();
|
||||
} catch (InterruptedException | IOException exception) {
|
||||
if (maxAttempts == -1) restart();
|
||||
else if (attempt <= maxAttempts) restart();
|
||||
else {
|
||||
eventManager.executeEvent(new S_StoppedEvent(this));
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
stop();
|
||||
} catch (IOException exception) {
|
||||
eventManager.executeEvent(new S_StoppedEvent(this));
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public final ClientHandler getClientHandlerByID(int id) {
|
||||
for (ClientHandler clientHandler : clientHandlers) if (clientHandler.getClientID() == id) return clientHandler;
|
||||
return null;
|
||||
}
|
||||
|
||||
public final EventManager getEventManager() {
|
||||
return eventManager;
|
||||
}
|
||||
|
||||
public static class ServerBuilder {
|
||||
public static class ServerBuilder extends DefaultMethodsOverrider {
|
||||
private int port;
|
||||
private boolean debugLog = false;
|
||||
private PacketHandler packetHandler = new PacketHandler();
|
||||
private EventManager eventManager = new EventManager();
|
||||
private int maxAttempts = 0;
|
||||
private int attemptDelayInSec = 1;
|
||||
|
||||
public final ServerBuilder enableDebugLog() {
|
||||
this.debugLog = true;
|
||||
return this;
|
||||
}
|
||||
private PacketHandler packetHandler;
|
||||
private EventManager eventManager;
|
||||
private Logger logger;
|
||||
|
||||
public final ServerBuilder setPort(int port) {
|
||||
this.port = port;
|
||||
return this;
|
||||
private int maxRestartAttempts = 0;
|
||||
private int restartDelay = 3000;
|
||||
private int timeout = 3000;
|
||||
|
||||
public final NetworkServer build() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
|
||||
return new NetworkServer(port, packetHandler, eventManager, logger, maxRestartAttempts, restartDelay, timeout);
|
||||
}
|
||||
|
||||
public final ServerBuilder setEventManager(EventManager eventManager) {
|
||||
@@ -219,23 +51,207 @@ public class NetworkServer {
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ServerBuilder setLogger(Logger logger) {
|
||||
this.logger = logger;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ServerBuilder setMaxReconnectAttempts(int maxRestartAttempts) {
|
||||
this.maxRestartAttempts = maxRestartAttempts;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ServerBuilder setPacketHandler(PacketHandler packetHandler) {
|
||||
this.packetHandler = packetHandler;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ServerBuilder setAttemptDelayInSeconds(int attemptDelayInSec) {
|
||||
this.attemptDelayInSec = attemptDelayInSec;
|
||||
public final ServerBuilder setPort(int port) {
|
||||
this.port = port;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ServerBuilder setMaxAttempts(int maxAttempts) {
|
||||
this.maxAttempts = maxAttempts;
|
||||
public final ServerBuilder setReconnectDelay(int reconnectDelay) {
|
||||
this.restartDelay = reconnectDelay;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final NetworkServer build() {
|
||||
return new NetworkServer(port, debugLog, packetHandler, eventManager, maxAttempts, attemptDelayInSec);
|
||||
public final ServerBuilder setTimeout(int timeout) {
|
||||
this.timeout = timeout;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
private final int port;
|
||||
|
||||
private final PacketHandler packetHandler;
|
||||
private final EventManager eventManager;
|
||||
private final Logger logger;
|
||||
|
||||
private int currentAttempts;
|
||||
private final int timeout;
|
||||
private final int maxRestartAttempts;
|
||||
private final int restartDelay;
|
||||
|
||||
private final List<ConnectionHandler> connectionHandlers = new ArrayList<>();
|
||||
public final Thread incomingConnectionThread = new Thread(this::incomingConnection);
|
||||
|
||||
private ServerSocket serverSocket;
|
||||
|
||||
public NetworkServer(int port, PacketHandler packetHandler, EventManager eventManager, Logger logger, int maxRestartAttempts, int restartDelay, int timeout) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
|
||||
this.port = port;
|
||||
this.timeout = timeout;
|
||||
|
||||
this.packetHandler = packetHandler;
|
||||
this.eventManager = eventManager;
|
||||
this.logger = logger;
|
||||
|
||||
this.maxRestartAttempts = maxRestartAttempts;
|
||||
this.restartDelay = restartDelay;
|
||||
this.currentAttempts = 0;
|
||||
|
||||
this.packetHandler.setServerInstance(this);
|
||||
this.packetHandler.registerPacket(ClientDisconnectPacket.class);
|
||||
this.packetHandler.registerPacket(ClientIDPacket.class);
|
||||
}
|
||||
|
||||
public final Logger getLogger() {
|
||||
return logger;
|
||||
}
|
||||
|
||||
public final EventManager getEventManager() {
|
||||
return eventManager;
|
||||
}
|
||||
|
||||
public final int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public final ServerSocket getServerSocket() {
|
||||
return serverSocket;
|
||||
}
|
||||
|
||||
public final PacketHandler getPacketHandler() {
|
||||
return packetHandler;
|
||||
}
|
||||
|
||||
public final List<ConnectionHandler> getConnectionHandlers() {
|
||||
return connectionHandlers;
|
||||
}
|
||||
|
||||
public final boolean isAutoRestartEnabled() {
|
||||
return maxRestartAttempts != 0;
|
||||
}
|
||||
|
||||
public final boolean isRunning() {
|
||||
return serverSocket != null && !serverSocket.isClosed() && serverSocket.isBound() &&
|
||||
incomingConnectionThread.isAlive() && !incomingConnectionThread.isInterrupted();
|
||||
}
|
||||
|
||||
public final ConnectionHandler getConnectionHandlerByID(int clientID) {
|
||||
return connectionHandlers.get(clientID);
|
||||
}
|
||||
|
||||
public synchronized final boolean stop() {
|
||||
if (!isRunning()) return false;
|
||||
|
||||
if (logger == null) System.out.println("Trying to stop server");
|
||||
else logger.info("Trying to stop server");
|
||||
|
||||
new ArrayList<>(connectionHandlers).forEach(connectionHandler -> connectionHandler.disconnect(true));
|
||||
connectionHandlers.clear();
|
||||
|
||||
incomingConnectionThread.interrupt();
|
||||
serverSocket = null;
|
||||
currentAttempts = 0;
|
||||
|
||||
if (logger == null) System.out.println("Server stopped");
|
||||
else logger.info("Server stopped");
|
||||
|
||||
eventManager.executeEvent(new S_StoppedEvent(this));
|
||||
return true;
|
||||
}
|
||||
|
||||
public synchronized final boolean start() {
|
||||
if (isRunning()) return false;
|
||||
|
||||
if (logger == null) System.out.println("Trying to start on port " + port);
|
||||
else logger.info("Trying to start on port " + port);
|
||||
|
||||
try {
|
||||
serverSocket = new ServerSocket(port);
|
||||
serverSocket.setSoTimeout(timeout);
|
||||
|
||||
incomingConnectionThread.start();
|
||||
|
||||
if (currentAttempts == 0) currentAttempts++;
|
||||
if (logger == null) System.out.println("Started ad port" + port + " (Attempts: " + currentAttempts + ")");
|
||||
else logger.info("Started at port " + port + " (Attempts: " + currentAttempts + ")");
|
||||
|
||||
currentAttempts = 0;
|
||||
|
||||
eventManager.executeEvent(new S_StartedEvent(this));
|
||||
return true;
|
||||
} catch (IOException exception) {
|
||||
if (maxRestartAttempts != 0) {
|
||||
try {
|
||||
Thread.sleep(restartDelay);
|
||||
} catch (InterruptedException sleepThreadException) {
|
||||
if (logger == null) System.err.println("Restart exception: " + sleepThreadException.getMessage());
|
||||
else logger.exception("Restart exception", sleepThreadException);
|
||||
}
|
||||
|
||||
currentAttempts++;
|
||||
if (currentAttempts <= maxRestartAttempts || maxRestartAttempts < 0) return start();
|
||||
}
|
||||
|
||||
if (logger == null) System.err.println("Failed to start on port " + port + ": " + exception.getMessage());
|
||||
else logger.exception("Failed to start on port " + port, exception);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public final boolean sendPacket(int clientID, Packet packet) {
|
||||
return getConnectionHandlerByID(clientID).sendPacket(packet);
|
||||
}
|
||||
|
||||
public final boolean sendPacket(Packet packet, int clientID) {
|
||||
return sendPacket(clientID, packet);
|
||||
}
|
||||
|
||||
public final Thread getIncomingConnectionThread() {
|
||||
return incomingConnectionThread;
|
||||
}
|
||||
|
||||
public final boolean broadcastPacket(Packet packet) {
|
||||
AtomicBoolean toReturn = new AtomicBoolean(false);
|
||||
connectionHandlers.forEach(connectionHandler -> toReturn.set(connectionHandler.sendPacket(packet)));
|
||||
return toReturn.get();
|
||||
}
|
||||
|
||||
private void incomingConnection() {
|
||||
if (!isRunning()) return;
|
||||
|
||||
try {
|
||||
while (isRunning()) {
|
||||
Socket socket = serverSocket.accept();
|
||||
socket.setTcpNoDelay(true);
|
||||
socket.setSoTimeout(timeout);
|
||||
|
||||
if (logger == null) System.out.println("Accepted connection from " + socket.getRemoteSocketAddress());
|
||||
else logger.info("Accepted connection from " + socket.getRemoteSocketAddress());
|
||||
|
||||
eventManager.executeEvent(new S_IncomingConnectionEvent(this, socket));
|
||||
connectionHandlers.add(new ConnectionHandler(this, socket, connectionHandlers.size() + 1));
|
||||
}
|
||||
} catch (IOException exception) {
|
||||
if (logger == null) System.err.println("Accept exception: " + exception.getMessage());
|
||||
else logger.exception("Accept exception", exception);
|
||||
|
||||
eventManager.executeEvent(new S_IncomingConnectionThreadFailedEvent(this, exception));
|
||||
}
|
||||
|
||||
stop();
|
||||
}
|
||||
}
|
||||
|
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.server.events.client.packets.received;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
import me.finn.unlegitlibrary.network.system.server.ClientHandler;
|
||||
|
||||
public class S_PacketFailedReceivedEvent extends Event {
|
||||
|
||||
public final ClientHandler clientHandler;
|
||||
public final Packet packet;
|
||||
|
||||
public S_PacketFailedReceivedEvent(ClientHandler clientHandler, Packet packet) {
|
||||
this.clientHandler = clientHandler;
|
||||
this.packet = packet;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final Object clone() throws CloneNotSupportedException {
|
||||
return super.clone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean equals(Object obj) {
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int hashCode() {
|
||||
return super.hashCode();
|
||||
}
|
||||
}
|
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.server.events.client.packets.received;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
import me.finn.unlegitlibrary.network.system.server.ClientHandler;
|
||||
|
||||
public class S_PacketReceivedEvent extends Event {
|
||||
|
||||
public final ClientHandler clientHandler;
|
||||
public final Packet packet;
|
||||
|
||||
public S_PacketReceivedEvent(ClientHandler clientHandler, Packet packet) {
|
||||
this.clientHandler = clientHandler;
|
||||
this.packet = packet;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final Object clone() throws CloneNotSupportedException {
|
||||
return super.clone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean equals(Object obj) {
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int hashCode() {
|
||||
return super.hashCode();
|
||||
}
|
||||
}
|
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.server.events.client.packets.received;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.server.ClientHandler;
|
||||
|
||||
public class S_UnknownObjectReceivedEvent extends Event {
|
||||
|
||||
public final ClientHandler clientHandler;
|
||||
public final Object object;
|
||||
|
||||
public S_UnknownObjectReceivedEvent(ClientHandler clientHandler, Object object) {
|
||||
this.clientHandler = clientHandler;
|
||||
this.object = object;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final Object clone() throws CloneNotSupportedException {
|
||||
return super.clone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean equals(Object obj) {
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int hashCode() {
|
||||
return super.hashCode();
|
||||
}
|
||||
}
|
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.server.events.client.packets.send;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
import me.finn.unlegitlibrary.network.system.server.ClientHandler;
|
||||
|
||||
public class S_PacketFailedSendEvent extends Event {
|
||||
|
||||
public final ClientHandler clientHandler;
|
||||
public final Packet packet;
|
||||
|
||||
public S_PacketFailedSendEvent(ClientHandler clientHandler, Packet packet) {
|
||||
this.clientHandler = clientHandler;
|
||||
this.packet = packet;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final Object clone() throws CloneNotSupportedException {
|
||||
return super.clone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean equals(Object obj) {
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int hashCode() {
|
||||
return super.hashCode();
|
||||
}
|
||||
}
|
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.server.events.client.packets.send;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
import me.finn.unlegitlibrary.network.system.server.ClientHandler;
|
||||
|
||||
public class S_PacketSendEvent extends Event {
|
||||
|
||||
public final ClientHandler clientHandler;
|
||||
public final Packet packet;
|
||||
|
||||
public S_PacketSendEvent(ClientHandler clientHandler, Packet packet) {
|
||||
this.clientHandler = clientHandler;
|
||||
this.packet = packet;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final Object clone() throws CloneNotSupportedException {
|
||||
return super.clone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean equals(Object obj) {
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int hashCode() {
|
||||
return super.hashCode();
|
||||
}
|
||||
}
|
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.server.events.client.state;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.server.ClientHandler;
|
||||
|
||||
public class S_ClientConnectedEvent extends Event {
|
||||
public final ClientHandler clientHandler;
|
||||
|
||||
public S_ClientConnectedEvent(ClientHandler clientHandler) {
|
||||
this.clientHandler = clientHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final Object clone() throws CloneNotSupportedException {
|
||||
return super.clone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean equals(Object obj) {
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int hashCode() {
|
||||
return super.hashCode();
|
||||
}
|
||||
}
|
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.server.events.client.state;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.server.ClientHandler;
|
||||
|
||||
public class S_ClientDisconnectedEvent extends Event {
|
||||
public final ClientHandler clientHandler;
|
||||
|
||||
public S_ClientDisconnectedEvent(ClientHandler clientHandler) {
|
||||
this.clientHandler = clientHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final Object clone() throws CloneNotSupportedException {
|
||||
return super.clone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean equals(Object obj) {
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int hashCode() {
|
||||
return super.hashCode();
|
||||
}
|
||||
}
|
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.server.events.client.state;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.server.ClientHandler;
|
||||
|
||||
public class S_ClientStoppedEvent extends Event {
|
||||
public final ClientHandler clientHandler;
|
||||
|
||||
public S_ClientStoppedEvent(ClientHandler clientHandler) {
|
||||
this.clientHandler = clientHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final Object clone() throws CloneNotSupportedException {
|
||||
return super.clone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean equals(Object obj) {
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int hashCode() {
|
||||
return super.hashCode();
|
||||
}
|
||||
}
|
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.server.events.connection;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.server.NetworkServer;
|
||||
|
||||
import java.net.Socket;
|
||||
|
||||
public class S_IncomingConnectionEvent extends Event {
|
||||
|
||||
public final NetworkServer server;
|
||||
public final Socket socket;
|
||||
|
||||
public S_IncomingConnectionEvent(NetworkServer server, Socket socket) {
|
||||
this.server = server;
|
||||
this.socket = socket;
|
||||
}
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.server.events.connection;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.server.NetworkServer;
|
||||
|
||||
public class S_IncomingConnectionThreadFailedEvent extends Event {
|
||||
|
||||
public final NetworkServer server;
|
||||
public final Exception exception;
|
||||
|
||||
public S_IncomingConnectionThreadFailedEvent(NetworkServer server, Exception exception) {
|
||||
this.server = server;
|
||||
this.exception = exception;
|
||||
}
|
||||
}
|
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.server.events.packets.received;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.server.ConnectionHandler;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
|
||||
public class S_PacketFailedReceivedEvent extends Event {
|
||||
|
||||
public final ConnectionHandler connectionHandler;
|
||||
public final Packet packet;
|
||||
public final Exception exception;
|
||||
|
||||
public S_PacketFailedReceivedEvent(ConnectionHandler connectionHandler, Packet packet, Exception exception) {
|
||||
this.connectionHandler = connectionHandler;
|
||||
this.packet = packet;
|
||||
this.exception = exception;
|
||||
}
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.server.events.packets.received;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.server.ConnectionHandler;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
|
||||
public class S_PacketReceivedEvent extends Event {
|
||||
|
||||
public final ConnectionHandler connectionHandler;
|
||||
public final Packet packet;
|
||||
|
||||
public S_PacketReceivedEvent(ConnectionHandler connectionHandler, Packet packet) {
|
||||
this.connectionHandler = connectionHandler;
|
||||
this.packet = packet;
|
||||
}
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.server.events.packets.received;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.server.ConnectionHandler;
|
||||
|
||||
public class S_ReceiveThreadFailedEvent extends Event {
|
||||
public final ConnectionHandler connectionHandler;
|
||||
public final Exception exception;
|
||||
|
||||
public S_ReceiveThreadFailedEvent(ConnectionHandler connectionHandler, Exception exception) {
|
||||
this.connectionHandler = connectionHandler;
|
||||
this.exception = exception;
|
||||
}
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.server.events.packets.received;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.server.ConnectionHandler;
|
||||
|
||||
public class S_UnknownObjectReceivedEvent extends Event {
|
||||
public final ConnectionHandler connectionHandler;
|
||||
public final Object received;
|
||||
|
||||
public S_UnknownObjectReceivedEvent(ConnectionHandler connectionHandler, Object received) {
|
||||
this.connectionHandler = connectionHandler;
|
||||
this.received = received;
|
||||
}
|
||||
}
|
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.server.events.packets.send;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.server.ConnectionHandler;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
|
||||
public class S_PacketFailedSendEvent extends Event {
|
||||
|
||||
public final ConnectionHandler connectionHandler;
|
||||
public final Packet packet;
|
||||
public final Exception exception;
|
||||
|
||||
public S_PacketFailedSendEvent(ConnectionHandler connectionHandler, Packet packet, Exception exception) {
|
||||
this.connectionHandler = connectionHandler;
|
||||
this.packet = packet;
|
||||
this.exception = exception;
|
||||
}
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.server.events.packets.send;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.server.ConnectionHandler;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
|
||||
public class S_PacketSendEvent extends Event {
|
||||
|
||||
public final ConnectionHandler connectionHandler;
|
||||
public final Packet packet;
|
||||
|
||||
public S_PacketSendEvent(ConnectionHandler connectionHandler, Packet packet) {
|
||||
this.connectionHandler = connectionHandler;
|
||||
this.packet = packet;
|
||||
}
|
||||
}
|
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.server.events.server;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.server.NetworkServer;
|
||||
|
||||
public class S_StartedEvent extends Event {
|
||||
|
||||
public final NetworkServer networkServer;
|
||||
|
||||
public S_StartedEvent(NetworkServer networkServer) {
|
||||
this.networkServer = networkServer;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final Object clone() throws CloneNotSupportedException {
|
||||
return super.clone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean equals(Object obj) {
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int hashCode() {
|
||||
return super.hashCode();
|
||||
}
|
||||
}
|
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.server.events.server;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.server.NetworkServer;
|
||||
|
||||
public class S_StoppedEvent extends Event {
|
||||
|
||||
public final NetworkServer networkServer;
|
||||
|
||||
public S_StoppedEvent(NetworkServer networkServer) {
|
||||
this.networkServer = networkServer;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final Object clone() throws CloneNotSupportedException {
|
||||
return super.clone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean equals(Object obj) {
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int hashCode() {
|
||||
return super.hashCode();
|
||||
}
|
||||
}
|
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.server.events.state.connection;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.server.ConnectionHandler;
|
||||
|
||||
public class S_ConnectionHandlerConnectedEvent extends Event {
|
||||
|
||||
public final ConnectionHandler connectionHandler;
|
||||
|
||||
public S_ConnectionHandlerConnectedEvent(ConnectionHandler connectionHandler) {
|
||||
this.connectionHandler = connectionHandler;
|
||||
}
|
||||
}
|
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.server.events.state.connection;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.server.ConnectionHandler;
|
||||
|
||||
public class S_ConnectionHandlerDisconnectedEvent extends Event {
|
||||
|
||||
public final ConnectionHandler connectionHandler;
|
||||
|
||||
public S_ConnectionHandlerDisconnectedEvent(ConnectionHandler connectionHandler) {
|
||||
this.connectionHandler = connectionHandler;
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.server.events.state.server;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.server.NetworkServer;
|
||||
|
||||
public class S_StartedEvent extends Event {
|
||||
|
||||
public final NetworkServer server;
|
||||
|
||||
public S_StartedEvent(NetworkServer server) {
|
||||
this.server = server;
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.server.events.state.server;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.server.NetworkServer;
|
||||
|
||||
public class S_StoppedEvent extends Event {
|
||||
|
||||
public final NetworkServer server;
|
||||
|
||||
public S_StoppedEvent(NetworkServer server) {
|
||||
this.server = server;
|
||||
}
|
||||
}
|
175
src/main/java/me/finn/unlegitlibrary/utils/Logger.java
Normal file
175
src/main/java/me/finn/unlegitlibrary/utils/Logger.java
Normal file
@@ -0,0 +1,175 @@
|
||||
package me.finn.unlegitlibrary.utils;
|
||||
|
||||
import me.finn.unlegitlibrary.file.FileUtils;
|
||||
import me.finn.unlegitlibrary.string.color.ConsoleColor;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Logger by shock9 Interactive
|
||||
*/
|
||||
public final class Logger {
|
||||
private File logFolder;
|
||||
private File latestLogFile;
|
||||
|
||||
private static boolean isInitialized = false;
|
||||
|
||||
public Logger(File logFolder) throws IOException {
|
||||
// Basic setup for log folder and latest log file
|
||||
this.logFolder = logFolder;
|
||||
latestLogFile = new File(logFolder, "log-latest.txt");
|
||||
|
||||
logFolder.mkdir();
|
||||
|
||||
if (latestLogFile.exists()) latestLogFile.delete();
|
||||
latestLogFile.createNewFile();
|
||||
|
||||
isInitialized = true;
|
||||
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||
try {
|
||||
shutdown();
|
||||
} catch (IOException exception) {
|
||||
exception("Failed to shutdown logger", exception);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
// Renaming latest log to current date and yeah
|
||||
public final void shutdown() throws IOException {
|
||||
if (!isInitialized) return;
|
||||
|
||||
// Get current date and time
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy_HH.mm.ss");
|
||||
Date date = new Date();
|
||||
|
||||
String timeStamp = formatter.format(date);
|
||||
|
||||
// Backup latest log file to current date and time
|
||||
File backupLogFile = new File(logFolder, "log-" + timeStamp + ".txt");
|
||||
backupLogFile.createNewFile();
|
||||
FileUtils.copyFile(latestLogFile, backupLogFile, true);
|
||||
FileUtils.writeFile(backupLogFile, FileUtils.readFile(latestLogFile));
|
||||
|
||||
isInitialized = false;
|
||||
}
|
||||
|
||||
private final void writeToLog(String log) throws IOException {
|
||||
if (isInitialized)
|
||||
FileUtils.writeFile(latestLogFile, FileUtils.readFile(latestLogFile) + System.lineSeparator() + log);
|
||||
}
|
||||
|
||||
public final void log(String string) {
|
||||
// Get current date and time
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
|
||||
Date date = new Date();
|
||||
|
||||
String timeStamp = formatter.format(date);
|
||||
|
||||
// Writing log
|
||||
String log = timeStamp + " [LOG] " + string;
|
||||
|
||||
try {
|
||||
writeToLog(log);
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
public final void info(String info) {
|
||||
// Get current date and time
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
|
||||
Date date = new Date();
|
||||
|
||||
String timeStamp = formatter.format(date);
|
||||
|
||||
// Writing log
|
||||
String log = timeStamp + " [INFO] " + info;
|
||||
|
||||
System.out.println(ConsoleColor.WHITE + log + ConsoleColor.RESET);
|
||||
|
||||
try {
|
||||
writeToLog(log);
|
||||
} catch (IOException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public final void warn(String warn) {
|
||||
// Get current date and time
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
|
||||
Date date = new Date();
|
||||
|
||||
String timeStamp = formatter.format(date);
|
||||
|
||||
// Writing log
|
||||
String log = timeStamp + " [WARN] " + warn;
|
||||
|
||||
System.out.println(ConsoleColor.YELLOW + log + ConsoleColor.RESET);
|
||||
|
||||
try {
|
||||
writeToLog(log);
|
||||
} catch (IOException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public final void error(String error) {
|
||||
// Get current date and time
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
|
||||
Date date = new Date();
|
||||
|
||||
String timeStamp = formatter.format(date);
|
||||
|
||||
// Writing log
|
||||
String log = timeStamp + " [ERROR] " + error;
|
||||
|
||||
System.out.println(ConsoleColor.RED + log + ConsoleColor.RESET);
|
||||
|
||||
try {
|
||||
writeToLog(log);
|
||||
} catch (IOException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public final void exception(String infoLine, Exception exception) {
|
||||
// Get current date and time
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
|
||||
Date date = new Date();
|
||||
|
||||
String timeStamp = formatter.format(date);
|
||||
|
||||
// Writing log
|
||||
String log = timeStamp + " [EXCEPTION-INFO] " + infoLine + System.lineSeparator() + timeStamp + " [EXCEPTION-MESSAGE] " + exception.getMessage();
|
||||
|
||||
System.out.println(ConsoleColor.RED + log + ConsoleColor.RESET);
|
||||
|
||||
try {
|
||||
writeToLog(log);
|
||||
} catch (IOException exception1) {
|
||||
exception1.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public final void debug(String debug) throws IOException {
|
||||
// Get current date and time
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
|
||||
Date date = new Date();
|
||||
|
||||
String timeStamp = formatter.format(date);
|
||||
|
||||
// Writing log
|
||||
String log = timeStamp + " [DEBUG] " + debug;
|
||||
|
||||
System.out.println(ConsoleColor.BLUE + log + ConsoleColor.RESET);
|
||||
|
||||
try {
|
||||
writeToLog(log);
|
||||
} catch (IOException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user