Bug fixes and new network system
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>me.finn.unlegitlibrary</groupId>
|
||||
<artifactId>unlegitlibrary</artifactId>
|
||||
<version>1.5.12</version>
|
||||
<version>1.5.13</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
|
@@ -85,12 +85,12 @@ public class AddonLoader extends DefaultMethodsOverrider {
|
||||
addons.forEach(this::disableAddon);
|
||||
}
|
||||
|
||||
public final void registerEventListener(Addon addon, Class<? extends EventListener> eventListener) throws InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
|
||||
public final void registerEventListener(Addon addon, EventListener eventListener) throws InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
|
||||
if (!addons.contains(addon)) return;
|
||||
addon.registerEventListener(eventListener);
|
||||
}
|
||||
|
||||
public final void unregisterEventListener(Addon addon, Class<? extends EventListener> eventListener) {
|
||||
public final void unregisterEventListener(Addon addon, EventListener eventListener) {
|
||||
if (!addons.contains(addon)) return;
|
||||
addon.unregisterEventListener(eventListener);
|
||||
}
|
||||
|
@@ -40,11 +40,11 @@ public abstract class Addon {
|
||||
eventManager.executeEvent(event);
|
||||
}
|
||||
|
||||
public final void registerEventListener(Class<? extends EventListener> eventListener) throws InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
|
||||
public final void registerEventListener(EventListener eventListener) throws InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
|
||||
eventManager.registerListener(eventListener);
|
||||
}
|
||||
|
||||
public final void unregisterEventListener(Class<? extends EventListener> eventListener) {
|
||||
public final void unregisterEventListener(EventListener eventListener) {
|
||||
eventManager.unregisterListener(eventListener);
|
||||
}
|
||||
|
||||
|
@@ -8,6 +8,7 @@
|
||||
|
||||
package me.finn.unlegitlibrary.command;
|
||||
|
||||
import javax.management.InstanceAlreadyExistsException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@@ -29,13 +30,17 @@ public abstract class Command {
|
||||
return new ArrayList<>(aliases);
|
||||
}
|
||||
|
||||
public Command(CommandManager commandManager, String name, String description, String usage, List<CommandPermission> permissions, List<String> aliases) {
|
||||
public Command(CommandManager commandManager, String name, String description, String usage, List<CommandPermission> permissions, List<String> aliases) throws InstanceAlreadyExistsException {
|
||||
this.commandManager = commandManager;
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.usage = usage;
|
||||
this.permissions = permissions;
|
||||
this.aliases = aliases;
|
||||
|
||||
boolean exists = commandManager.getCommand(name) != null;
|
||||
if (!exists) for (String alias : aliases) exists = commandManager.getCommand(alias) != null;
|
||||
if (exists) throw new InstanceAlreadyExistsException("Command with this name or some alias alreadx exists!");
|
||||
}
|
||||
|
||||
public abstract void execute(CommandExecutor commandExecutor, String label, String[] args);
|
||||
|
@@ -21,14 +21,12 @@ import java.util.Map;
|
||||
public class EventManager extends DefaultMethodsOverrider {
|
||||
|
||||
private final HashMap<Class<? extends Event>, HashMap<EventPriority, HashMap<Object, Method>>> registeredListener = new HashMap<>();
|
||||
private final HashMap<Class<? extends EventListener>, Object> eventListeners = new HashMap<>();
|
||||
private final HashMap<EventListener, Object> eventListeners = new HashMap<>();
|
||||
|
||||
public final void registerListener(Class<? extends EventListener> listenerClass) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
|
||||
public final void registerListener(EventListener listenerClass) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
|
||||
if (isListenerRegistered(listenerClass)) return;
|
||||
|
||||
Object clazz = listenerClass.getDeclaredConstructor().newInstance();
|
||||
|
||||
for (Method method : clazz.getClass().getDeclaredMethods()) {
|
||||
for (Method method : listenerClass.getClass().getDeclaredMethods()) {
|
||||
Listener listener = method.getAnnotation(Listener.class);
|
||||
|
||||
if (listener == null) continue;
|
||||
@@ -39,16 +37,16 @@ public class EventManager extends DefaultMethodsOverrider {
|
||||
HashMap<EventPriority, HashMap<Object, Method>> list = registeredListener.getOrDefault(eventClass, new HashMap<>());
|
||||
HashMap<Object, Method> set = list.getOrDefault(listener.priority(), new HashMap<>());
|
||||
|
||||
set.put(clazz, method);
|
||||
set.put(listenerClass, method);
|
||||
list.put(listener.priority(), set);
|
||||
registeredListener.put(eventClass, list);
|
||||
}
|
||||
}
|
||||
|
||||
eventListeners.put(listenerClass, clazz);
|
||||
eventListeners.put(listenerClass, listenerClass);
|
||||
}
|
||||
|
||||
public synchronized final void unregisterListener(Class<? extends EventListener> listenerClass) {
|
||||
public synchronized final void unregisterListener(EventListener listenerClass) {
|
||||
if (!isListenerRegistered(listenerClass)) return;
|
||||
|
||||
Object clazz = eventListeners.get(listenerClass);
|
||||
@@ -95,7 +93,7 @@ public class EventManager extends DefaultMethodsOverrider {
|
||||
eventListeners.remove(listenerClass);
|
||||
}
|
||||
|
||||
public final boolean isListenerRegistered(Class<? extends EventListener> listenerClass) {
|
||||
public final boolean isListenerRegistered(EventListener listenerClass) {
|
||||
return eventListeners.containsKey(listenerClass);
|
||||
}
|
||||
|
||||
@@ -103,7 +101,7 @@ public class EventManager extends DefaultMethodsOverrider {
|
||||
HashMap<EventPriority, HashMap<Object, Method>> list = registeredListener.getOrDefault(event.getClass(), new HashMap<>());
|
||||
|
||||
list.getOrDefault(EventPriority.LOWEST, new HashMap<>()).forEach((k, v) -> {
|
||||
if (!isListenerRegistered((Class<? extends EventListener>) k.getClass())) return;
|
||||
if (!isListenerRegistered((EventListener) k)) return;
|
||||
|
||||
try {
|
||||
v.invoke(k, event);
|
||||
@@ -113,7 +111,7 @@ public class EventManager extends DefaultMethodsOverrider {
|
||||
});
|
||||
|
||||
list.getOrDefault(EventPriority.LOW, new HashMap<>()).forEach((k, v) -> {
|
||||
if (!isListenerRegistered((Class<? extends EventListener>) k.getClass())) return;
|
||||
if (!isListenerRegistered((EventListener) k)) return;
|
||||
|
||||
try {
|
||||
v.invoke(k, event);
|
||||
@@ -123,7 +121,7 @@ public class EventManager extends DefaultMethodsOverrider {
|
||||
});
|
||||
|
||||
list.getOrDefault(EventPriority.NORMAL, new HashMap<>()).forEach((k, v) -> {
|
||||
if (!isListenerRegistered((Class<? extends EventListener>) k.getClass())) return;
|
||||
if (!isListenerRegistered((EventListener) k)) return;
|
||||
|
||||
try {
|
||||
v.invoke(k, event);
|
||||
@@ -133,7 +131,7 @@ public class EventManager extends DefaultMethodsOverrider {
|
||||
});
|
||||
|
||||
list.getOrDefault(EventPriority.HIGH, new HashMap<>()).forEach((k, v) -> {
|
||||
if (!isListenerRegistered((Class<? extends EventListener>) k.getClass())) return;
|
||||
if (!isListenerRegistered((EventListener) k)) return;
|
||||
|
||||
try {
|
||||
v.invoke(k, event);
|
||||
@@ -143,7 +141,7 @@ public class EventManager extends DefaultMethodsOverrider {
|
||||
});
|
||||
|
||||
list.getOrDefault(EventPriority.HIGHEST, new HashMap<>()).forEach((k, v) -> {
|
||||
if (!isListenerRegistered((Class<? extends EventListener>) k.getClass())) return;
|
||||
if (!isListenerRegistered((EventListener) k)) return;
|
||||
|
||||
try {
|
||||
v.invoke(k, event);
|
||||
|
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
* Copyright (C) 2025 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
|
||||
@@ -9,17 +9,12 @@
|
||||
package me.finn.unlegitlibrary.network.system.client;
|
||||
|
||||
import me.finn.unlegitlibrary.event.EventManager;
|
||||
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_ReceiveThreadFailedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.packets.impl.ClientDisconnectPacket;
|
||||
import me.finn.unlegitlibrary.network.system.client.events.*;
|
||||
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;
|
||||
@@ -27,12 +22,11 @@ import me.finn.unlegitlibrary.utils.Logger;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.ConnectException;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
|
||||
public class NetworkClient extends DefaultMethodsOverrider {
|
||||
|
||||
public final class NetworkClient {
|
||||
public static class ClientBuilder extends DefaultMethodsOverrider {
|
||||
private String host;
|
||||
private int port;
|
||||
@@ -45,7 +39,7 @@ public class NetworkClient extends DefaultMethodsOverrider {
|
||||
private int reconnectDelay = 3000;
|
||||
private int timeout = 0;
|
||||
|
||||
public final NetworkClient build() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
|
||||
public final NetworkClient build() {
|
||||
return new NetworkClient(host, port ,packetHandler, eventManager, logger, maxReconnectAttempts, reconnectDelay, timeout);
|
||||
}
|
||||
|
||||
@@ -97,18 +91,58 @@ public class NetworkClient extends DefaultMethodsOverrider {
|
||||
private final EventManager eventManager;
|
||||
private final Logger logger;
|
||||
|
||||
private Socket socket;
|
||||
private int timeout;
|
||||
|
||||
private ObjectOutputStream outputStream;
|
||||
private ObjectInputStream inputStream;
|
||||
|
||||
private int clientID;
|
||||
|
||||
private int currentAttempts;
|
||||
private final int maxReconnectAttempts;
|
||||
private final int reconnectDelay;
|
||||
|
||||
private Socket socket;
|
||||
private int timeout;
|
||||
private ObjectOutputStream outputStream;
|
||||
private ObjectInputStream inputStream;
|
||||
private int clientID = -1;
|
||||
public final Thread receiveThread = new Thread(this::receive);
|
||||
private final Thread receiveThread = new Thread(this::receive);
|
||||
|
||||
private NetworkClient(String host, int port, PacketHandler packetHandler, EventManager eventManager, Logger logger, int reconnectAttempts, int reconnectDelay, int timeout) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
|
||||
public int getClientID() {
|
||||
return clientID;
|
||||
}
|
||||
|
||||
public Socket getSocket() {
|
||||
return socket;
|
||||
}
|
||||
|
||||
public EventManager getEventManager() {
|
||||
return eventManager;
|
||||
}
|
||||
|
||||
public PacketHandler getPacketHandler() {
|
||||
return packetHandler;
|
||||
}
|
||||
|
||||
public Logger getLogger() {
|
||||
return logger;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
return socket != null && socket.isConnected() && !socket.isClosed() && socket.isBound()
|
||||
&& receiveThread.isAlive() && !receiveThread.isInterrupted();
|
||||
}
|
||||
|
||||
public boolean isAutoReconnectEnabled() {
|
||||
return maxReconnectAttempts != 0;
|
||||
}
|
||||
|
||||
private NetworkClient(String host, int port, PacketHandler packetHandler, EventManager eventManager, Logger logger, int reconnectAttempts, int reconnectDelay, int timeout) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.clientID = -1;
|
||||
@@ -123,112 +157,10 @@ public class NetworkClient extends DefaultMethodsOverrider {
|
||||
this.currentAttempts = 0;
|
||||
|
||||
this.packetHandler.setClientInstance(this);
|
||||
this.packetHandler.registerPacket(ClientDisconnectPacket.class);
|
||||
this.packetHandler.registerPacket(ClientIDPacket.class);
|
||||
this.packetHandler.registerPacket(new ClientIDPacket());
|
||||
}
|
||||
|
||||
public final int getClientID() {
|
||||
return clientID;
|
||||
}
|
||||
|
||||
public final Socket getSocket() {
|
||||
return socket;
|
||||
}
|
||||
|
||||
public final EventManager getEventManager() {
|
||||
return eventManager;
|
||||
}
|
||||
|
||||
public final String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
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() {
|
||||
return socket != null && socket.isConnected() && !socket.isClosed() && socket.isBound()
|
||||
&& receiveThread.isAlive() && !receiveThread.isInterrupted();
|
||||
}
|
||||
|
||||
public final Thread getReceiveThread() {
|
||||
return receiveThread;
|
||||
}
|
||||
|
||||
public final void setClientID(int clientID) {
|
||||
if (this.clientID == -1) {
|
||||
this.clientID = clientID;
|
||||
eventManager.executeEvent(new C_ConnectedEvent(this));
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized boolean disconnect(boolean sendDisconnectPacket) {
|
||||
if (logger == null) System.out.println("Disconnecting from server...");
|
||||
else logger.info("Disconnecting from server...");
|
||||
|
||||
receiveThread.interrupt();
|
||||
|
||||
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() {
|
||||
public synchronized boolean connect() throws ConnectException {
|
||||
if (isConnected()) return false;
|
||||
|
||||
if (logger == null) System.out.println("Trying to connect to " + host + ":" + port + "...");
|
||||
@@ -248,10 +180,12 @@ public class NetworkClient extends DefaultMethodsOverrider {
|
||||
if (logger == null) System.out.println("Connected to " + host + ":" + port + " (Attempts: " + currentAttempts + ")");
|
||||
else logger.info("Connected to " + host + ":" + port + " (Attempts: " + currentAttempts + ")");
|
||||
|
||||
eventManager.executeEvent(new ClientConnectedEvent(this));
|
||||
|
||||
currentAttempts = 0;
|
||||
return true;
|
||||
} catch (IOException exception) {
|
||||
if (maxReconnectAttempts != 0) {
|
||||
if (isAutoReconnectEnabled()) {
|
||||
try {
|
||||
Thread.sleep(reconnectDelay);
|
||||
} catch (InterruptedException sleepThreadException) {
|
||||
@@ -260,32 +194,11 @@ public class NetworkClient extends DefaultMethodsOverrider {
|
||||
}
|
||||
|
||||
currentAttempts++;
|
||||
if (currentAttempts <= maxReconnectAttempts || maxReconnectAttempts < 0) return connect();
|
||||
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;
|
||||
throw new ConnectException("Failed to connect to " + host + ":" + port);
|
||||
}
|
||||
|
||||
private void receive() {
|
||||
@@ -296,23 +209,87 @@ public class NetworkClient extends DefaultMethodsOverrider {
|
||||
Object received = inputStream.readObject();
|
||||
|
||||
if (received instanceof Integer) {
|
||||
int id = (Integer) received;
|
||||
Packet packet = packetHandler.getPacketByID(id);
|
||||
if (packetHandler.handlePacket(id, packet, inputStream))
|
||||
eventManager.executeEvent(new C_PacketReceivedEvent(this, packet));
|
||||
else eventManager.executeEvent(new C_PacketFailedReceivedEvent(this, packet, null));
|
||||
int packetID = (Integer) received;
|
||||
if (packetHandler.isPacketIDRegistered(packetID)) {
|
||||
Packet packet = packetHandler.getPacketByID(packetID);
|
||||
if (packetHandler.handlePacket(packetID, packet, inputStream))
|
||||
eventManager.executeEvent(new C_PacketReceivedEvent(this, packet));
|
||||
else
|
||||
eventManager.executeEvent(new C_PacketReceivedFailedEvent(this, packet));
|
||||
} else eventManager.executeEvent(new C_UnknownObjectReceivedEvent(this, received));
|
||||
} else eventManager.executeEvent(new C_UnknownObjectReceivedEvent(this, received));
|
||||
} catch (SocketException ignored) {
|
||||
disconnect(false);
|
||||
try {
|
||||
disconnect();
|
||||
} catch (ConnectException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
} catch (Exception exception) {
|
||||
exception.printStackTrace();
|
||||
return;
|
||||
} catch (IOException | ClassNotFoundException exception) {
|
||||
if (logger == null) System.err.println("Receive thread failed: " + exception.getMessage());
|
||||
else logger.exception("Receive thread failed", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
eventManager.executeEvent(new C_ReceiveThreadFailedEvent(this, exception));
|
||||
|
||||
public void setClientID(int clientID) {
|
||||
if (this.clientID == -1) this.clientID = clientID;
|
||||
}
|
||||
|
||||
public boolean sendPacket(Packet packet) throws IOException, ClassNotFoundException {
|
||||
if (!isConnected()) return false;
|
||||
|
||||
try {
|
||||
if (packetHandler.sendPacket(packet, outputStream)) {
|
||||
eventManager.executeEvent(new C_PacketSendEvent(this, packet));
|
||||
return true;
|
||||
} else {
|
||||
eventManager.executeEvent(new C_PacketSendFailedEvent(this, packet));
|
||||
return false;
|
||||
}
|
||||
} catch (IOException | ClassNotFoundException exception) {
|
||||
throw exception;
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized boolean disconnect() throws ConnectException {
|
||||
boolean wasConnected = isConnected();
|
||||
|
||||
if (wasConnected) {
|
||||
if (logger == null) System.out.println("Disconnecting from server...");
|
||||
else logger.info("Disconnecting from server...");
|
||||
}
|
||||
|
||||
if (receiveThread.isAlive() && !receiveThread.isInterrupted()) receiveThread.interrupt();
|
||||
|
||||
if (wasConnected) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
disconnect(false);
|
||||
outputStream = null;
|
||||
inputStream = null;
|
||||
socket = null;
|
||||
|
||||
currentAttempts = 0;
|
||||
|
||||
if (wasConnected) {
|
||||
if (logger == null) System.out.println("Disconnected from server");
|
||||
else logger.info("Disconnected from server");
|
||||
}
|
||||
|
||||
eventManager.executeEvent(new ClientDisconnectedEvent(this));
|
||||
|
||||
clientID = -1;
|
||||
if (isAutoReconnectEnabled() && (currentAttempts < maxReconnectAttempts || maxReconnectAttempts < 0))
|
||||
return connect();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (C) 2025 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.client.events;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
|
||||
public class C_PacketReceivedEvent extends Event {
|
||||
public final NetworkClient networkClient;
|
||||
public final Packet packet;
|
||||
|
||||
public C_PacketReceivedEvent(NetworkClient networkClient, Packet packet) {
|
||||
this.networkClient = networkClient;
|
||||
this.packet = packet;
|
||||
}
|
||||
}
|
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (C) 2025 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.client.events;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
|
||||
public class C_PacketReceivedFailedEvent extends Event {
|
||||
public final NetworkClient networkClient;
|
||||
public final Packet packet;
|
||||
|
||||
public C_PacketReceivedFailedEvent(NetworkClient networkClient, Packet packet) {
|
||||
this.networkClient = networkClient;
|
||||
this.packet = packet;
|
||||
}
|
||||
}
|
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (C) 2025 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.client.events;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
|
||||
public class C_PacketSendEvent extends Event {
|
||||
public final NetworkClient networkClient;
|
||||
public final Packet packet;
|
||||
|
||||
public C_PacketSendEvent(NetworkClient networkClient, Packet packet) {
|
||||
this.networkClient = networkClient;
|
||||
this.packet = packet;
|
||||
}
|
||||
}
|
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (C) 2025 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.client.events;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
|
||||
public class C_PacketSendFailedEvent extends Event {
|
||||
public final NetworkClient networkClient;
|
||||
public final Packet packet;
|
||||
|
||||
public C_PacketSendFailedEvent(NetworkClient networkClient, Packet packet) {
|
||||
this.networkClient = networkClient;
|
||||
this.packet = packet;
|
||||
}
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright (C) 2025 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.client.events;
|
||||
|
||||
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 received;
|
||||
|
||||
public C_UnknownObjectReceivedEvent(NetworkClient networkClient, Object received) {
|
||||
this.networkClient = networkClient;
|
||||
this.received = received;
|
||||
}
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright (C) 2025 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.client.events;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
|
||||
public class ClientConnectedEvent extends Event {
|
||||
public final NetworkClient client;
|
||||
|
||||
public ClientConnectedEvent(NetworkClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright (C) 2025 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.client.events;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
|
||||
public class ClientDisconnectedEvent extends Event {
|
||||
public final NetworkClient client;
|
||||
|
||||
public ClientDisconnectedEvent(NetworkClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
}
|
@@ -1,46 +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.client.events.received;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
|
||||
public class C_PacketFailedReceivedEvent extends Event {
|
||||
|
||||
public final NetworkClient client;
|
||||
public final Packet packet;
|
||||
public final Exception exception;
|
||||
|
||||
public C_PacketFailedReceivedEvent(NetworkClient client, Packet packet, Exception exception) {
|
||||
this.client = client;
|
||||
this.packet = packet;
|
||||
this.exception = exception;
|
||||
}
|
||||
|
||||
@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.client.events.received;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
|
||||
public class C_PacketReceivedEvent extends Event {
|
||||
|
||||
public final NetworkClient client;
|
||||
public final Packet packet;
|
||||
|
||||
public C_PacketReceivedEvent(NetworkClient client, Packet packet) {
|
||||
this.client = client;
|
||||
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.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 client;
|
||||
public final Object received;
|
||||
|
||||
public C_UnknownObjectReceivedEvent(NetworkClient client, Object received) {
|
||||
this.client = client;
|
||||
this.received = received;
|
||||
}
|
||||
|
||||
@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,46 +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.client.events.send;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
|
||||
public class C_PacketFailedSendEvent extends Event {
|
||||
|
||||
public final NetworkClient client;
|
||||
public final Packet packet;
|
||||
public final Exception exception;
|
||||
|
||||
public C_PacketFailedSendEvent(NetworkClient client, Packet packet, Exception exception) {
|
||||
this.client = client;
|
||||
this.packet = packet;
|
||||
this.exception = exception;
|
||||
}
|
||||
|
||||
@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.client.events.send;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
|
||||
public class C_PacketSendEvent extends Event {
|
||||
|
||||
public final NetworkClient client;
|
||||
public final Packet packet;
|
||||
|
||||
public C_PacketSendEvent(NetworkClient client, Packet packet) {
|
||||
this.client = client;
|
||||
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,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.client.events.state;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
|
||||
public class C_ConnectedEvent extends Event {
|
||||
|
||||
public final NetworkClient client;
|
||||
|
||||
public C_ConnectedEvent(NetworkClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
@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.client.events.state;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
|
||||
public class C_DisconnectedEvent extends Event {
|
||||
|
||||
public final NetworkClient client;
|
||||
|
||||
public C_DisconnectedEvent(NetworkClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
@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.client.events.state;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
|
||||
public class C_ReceiveThreadFailedEvent extends Event {
|
||||
|
||||
public final NetworkClient client;
|
||||
public final Exception exception;
|
||||
|
||||
public C_ReceiveThreadFailedEvent(NetworkClient client, Exception exception) {
|
||||
this.client = client;
|
||||
this.exception = exception;
|
||||
}
|
||||
|
||||
@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,13 +1,5 @@
|
||||
/*
|
||||
* 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
|
||||
* Copyright (C) 2025 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
|
||||
@@ -35,5 +27,4 @@ public abstract class Packet {
|
||||
public abstract void write(PacketHandler packetHandler, ObjectOutputStream outputStream) throws IOException, ClassNotFoundException;
|
||||
|
||||
public abstract void read(PacketHandler packetHandler, ObjectInputStream outputStream) throws IOException, ClassNotFoundException;
|
||||
}
|
||||
|
||||
}
|
@@ -1,13 +1,5 @@
|
||||
/*
|
||||
* 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
|
||||
* Copyright (C) 2025 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
|
||||
@@ -23,13 +15,12 @@ import me.finn.unlegitlibrary.utils.DefaultMethodsOverrider;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class PacketHandler extends DefaultMethodsOverrider {
|
||||
public final class PacketHandler extends DefaultMethodsOverrider {
|
||||
|
||||
private final Map<Integer, Class<? extends Packet>> packets = new HashMap<>();
|
||||
private final Map<Integer, Packet> packets = new HashMap<>();
|
||||
|
||||
private NetworkClient clientInstance;
|
||||
private NetworkServer serverInstance;
|
||||
@@ -55,24 +46,15 @@ public class PacketHandler extends DefaultMethodsOverrider {
|
||||
}
|
||||
|
||||
public final Packet getPacketByID(int id) {
|
||||
Class<? extends Packet> packetClass = packets.get(id);
|
||||
if (packetClass == null) return null;
|
||||
try {
|
||||
return packetClass.getDeclaredConstructor().newInstance();
|
||||
} catch (InstantiationException | InvocationTargetException | IllegalAccessException |
|
||||
NoSuchMethodException exception) {
|
||||
return null;
|
||||
}
|
||||
return packets.get(id);
|
||||
}
|
||||
|
||||
public final boolean registerPacket(Class<? extends Packet> packetClass) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
|
||||
Packet packet = packetClass.getDeclaredConstructor().newInstance();
|
||||
public final boolean registerPacket(Packet packet) {
|
||||
int id = packet.getPacketID();
|
||||
|
||||
if (!(packet instanceof SystemPacket) && isPacketIDRegistered(id)) return false;
|
||||
else if (isPacketIDRegistered(id)) packets.remove(id);
|
||||
if (isPacketIDRegistered(id)) return false;
|
||||
|
||||
packets.put(id, packetClass);
|
||||
packets.put(id, packet);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@@ -1,15 +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.packets;
|
||||
|
||||
public abstract class SystemPacket extends Packet {
|
||||
protected SystemPacket(int id) {
|
||||
super(id);
|
||||
}
|
||||
}
|
@@ -1,48 +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.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);
|
||||
}
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
* Copyright (C) 2025 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
|
||||
@@ -8,18 +8,16 @@
|
||||
|
||||
package me.finn.unlegitlibrary.network.system.packets.impl;
|
||||
|
||||
import me.finn.unlegitlibrary.network.system.client.NetworkClient;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
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 class ClientIDPacket extends Packet {
|
||||
public ClientIDPacket() {
|
||||
super(-1);
|
||||
super(0);
|
||||
}
|
||||
|
||||
private int clientID;
|
||||
@@ -36,7 +34,6 @@ public class ClientIDPacket extends SystemPacket {
|
||||
|
||||
@Override
|
||||
public void read(PacketHandler packetHandler, ObjectInputStream outputStream) throws IOException, ClassNotFoundException {
|
||||
clientID = outputStream.readInt();
|
||||
packetHandler.getClientInstance().setClientID(clientID);
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
* Copyright (C) 2025 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
|
||||
@@ -8,17 +8,9 @@
|
||||
|
||||
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 me.finn.unlegitlibrary.network.system.packets.impl.ClientIDPacket;
|
||||
import me.finn.unlegitlibrary.network.system.server.events.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
@@ -28,7 +20,7 @@ import java.net.SocketException;
|
||||
|
||||
public class ConnectionHandler {
|
||||
|
||||
private NetworkServer server;
|
||||
public final NetworkServer networkServer;
|
||||
private Socket socket;
|
||||
private int clientID;
|
||||
|
||||
@@ -36,8 +28,17 @@ public class ConnectionHandler {
|
||||
private ObjectInputStream inputStream;
|
||||
public final Thread receiveThread = new Thread(this::receive);
|
||||
|
||||
public ConnectionHandler(NetworkServer server, Socket socket, int clientID) throws IOException {
|
||||
this.server = server;
|
||||
public int getClientID() {
|
||||
return clientID;
|
||||
}
|
||||
|
||||
public final boolean isConnected() {
|
||||
return networkServer.isRunning() && socket != null && socket.isConnected() && !socket.isClosed() && socket.isBound()
|
||||
&& receiveThread.isAlive() && !receiveThread.isInterrupted();
|
||||
}
|
||||
|
||||
public ConnectionHandler(NetworkServer server, Socket socket, int clientID) throws IOException, ClassNotFoundException {
|
||||
this.networkServer = server;
|
||||
this.socket = socket;
|
||||
this.clientID = clientID;
|
||||
|
||||
@@ -46,64 +47,29 @@ public class ConnectionHandler {
|
||||
|
||||
receiveThread.start();
|
||||
|
||||
sendPacket(new ClientIDPacket(clientID));
|
||||
sendPacket(new ClientIDPacket());
|
||||
networkServer.getEventManager().executeEvent(new ConnectionHandlerConnectedEvent(this));
|
||||
}
|
||||
|
||||
public final int getClientID() {
|
||||
return clientID;
|
||||
}
|
||||
public synchronized boolean disconnect() {
|
||||
boolean wasConnected = isConnected();
|
||||
|
||||
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));
|
||||
if (wasConnected) {
|
||||
if (networkServer.getLogger() == null)
|
||||
System.out.println("Client ID '" + clientID + "' is disconnecting from server...");
|
||||
else networkServer.getLogger().info("Client ID '" + clientID + "' is disconnecting from server...");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public synchronized boolean disconnect(boolean sendDisconnectPacket) {
|
||||
if (server.getLogger() == null) System.out.println("Client ID '" + clientID + "' is disconnecting from server...");
|
||||
else server.getLogger().info("Client ID '" + clientID + "' is disconnecting from server...");
|
||||
|
||||
receiveThread.interrupt();
|
||||
|
||||
if (isConnected()) {
|
||||
if (sendDisconnectPacket) sendPacket(new ClientDisconnectPacket(clientID, false));
|
||||
if (receiveThread.isAlive() && !receiveThread.isInterrupted()) receiveThread.interrupt();
|
||||
|
||||
if (wasConnected) {
|
||||
try {
|
||||
outputStream.close();
|
||||
inputStream.close();
|
||||
socket.close();
|
||||
} catch (IOException exception) {
|
||||
if (server.getLogger() == null) System.err.println("Client ID '" + clientID + "' failed to close socket: " + exception.getMessage());
|
||||
else server.getLogger().exception("Client ID '" + clientID + "' failed to close socket", exception);
|
||||
if (networkServer.getLogger() == null) System.err.println("Client ID '" + clientID + "' failed to close socket: " + exception.getMessage());
|
||||
else networkServer.getLogger().exception("Client ID '" + clientID + "' failed to close socket", exception);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,16 +77,31 @@ public class ConnectionHandler {
|
||||
inputStream = null;
|
||||
socket = null;
|
||||
|
||||
server.getConnectionHandlers().remove(this);
|
||||
networkServer.getConnectionHandlers().remove(this);
|
||||
|
||||
if (wasConnected) {
|
||||
if (networkServer.getLogger() == null)
|
||||
System.out.println("Client ID '" + clientID + "' disconnected from server");
|
||||
else networkServer.getLogger().info("Client ID '" + clientID + "' disconnected from server");
|
||||
}
|
||||
|
||||
networkServer.getEventManager().executeEvent(new ConnectionHandlerDisconnectedEvent(this));
|
||||
clientID = -1;
|
||||
|
||||
server.getEventManager().executeEvent(new S_ConnectionHandlerDisconnectedEvent(this));
|
||||
if (server.getLogger() == null) System.out.println("Client ID '" + clientID + "' disconnected from server");
|
||||
else server.getLogger().info("Client ID '" + clientID + "' disconnected from server");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean sendPacket(Packet packet) throws IOException, ClassNotFoundException {
|
||||
if (!isConnected()) return false;
|
||||
|
||||
if (networkServer.getPacketHandler().sendPacket(packet, outputStream)) {
|
||||
networkServer.getEventManager().executeEvent(new S_PacketSendEvent(packet, this));
|
||||
return true;
|
||||
} else {
|
||||
networkServer.getEventManager().executeEvent(new S_PacketSendFailedEvent(packet, this));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void receive() {
|
||||
if (!isConnected()) return;
|
||||
|
||||
@@ -129,23 +110,21 @@ public class ConnectionHandler {
|
||||
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));
|
||||
int packetID = (Integer) received;
|
||||
if (networkServer.getPacketHandler().isPacketIDRegistered(packetID)) {
|
||||
Packet packet = networkServer.getPacketHandler().getPacketByID(packetID);
|
||||
if (networkServer.getPacketHandler().handlePacket(packetID, packet, inputStream))
|
||||
networkServer.getEventManager().executeEvent(new S_PacketReceivedEvent(this, packet));
|
||||
else
|
||||
networkServer.getEventManager().executeEvent(new S_PacketReceivedFailedEvent(this, packet));
|
||||
} else networkServer.getEventManager().executeEvent(new S_UnknownObjectReceivedEvent(received, this));
|
||||
} else networkServer.getEventManager().executeEvent(new S_UnknownObjectReceivedEvent(received, this));
|
||||
} catch (SocketException ignored) {
|
||||
disconnect(false);
|
||||
disconnect();
|
||||
} catch (Exception exception) {
|
||||
exception.printStackTrace();
|
||||
return;
|
||||
} catch (IOException | ClassNotFoundException exception) {
|
||||
if (server.getLogger() == null) System.err.println("Client ID '" + clientID + "' received thread failed: " + exception.getMessage());
|
||||
else server.getLogger().exception("Client ID '" + clientID + "' received thread failed", exception);
|
||||
|
||||
server.getEventManager().executeEvent(new S_ReceiveThreadFailedEvent(this, exception));
|
||||
}
|
||||
}
|
||||
|
||||
disconnect(false);
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
* Copyright (C) 2025 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
|
||||
@@ -9,29 +9,21 @@
|
||||
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.connection.S_ConnectionHandlerConnectedEvent;
|
||||
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.packets.impl.ClientIDPacket;
|
||||
import me.finn.unlegitlibrary.network.system.server.events.IncomingConnectionEvent;
|
||||
import me.finn.unlegitlibrary.utils.DefaultMethodsOverrider;
|
||||
import me.finn.unlegitlibrary.utils.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
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 extends DefaultMethodsOverrider {
|
||||
|
||||
public final class NetworkServer {
|
||||
public static class ServerBuilder extends DefaultMethodsOverrider {
|
||||
private int port;
|
||||
|
||||
@@ -43,7 +35,7 @@ public class NetworkServer extends DefaultMethodsOverrider {
|
||||
private int restartDelay = 3000;
|
||||
private int timeout = 0;
|
||||
|
||||
public final NetworkServer build() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
|
||||
public final NetworkServer build() {
|
||||
return new NetworkServer(port, packetHandler, eventManager, logger, maxRestartAttempts, restartDelay, timeout);
|
||||
}
|
||||
|
||||
@@ -82,7 +74,7 @@ public class NetworkServer extends DefaultMethodsOverrider {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private final int port;
|
||||
|
||||
private final PacketHandler packetHandler;
|
||||
@@ -99,44 +91,23 @@ public class NetworkServer extends DefaultMethodsOverrider {
|
||||
|
||||
private ServerSocket serverSocket;
|
||||
|
||||
private 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() {
|
||||
public Logger getLogger() {
|
||||
return logger;
|
||||
}
|
||||
|
||||
public final EventManager getEventManager() {
|
||||
public EventManager getEventManager() {
|
||||
return eventManager;
|
||||
}
|
||||
|
||||
public final int getPort() {
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public final ServerSocket getServerSocket() {
|
||||
return serverSocket;
|
||||
}
|
||||
|
||||
public final PacketHandler getPacketHandler() {
|
||||
public PacketHandler getPacketHandler() {
|
||||
return packetHandler;
|
||||
}
|
||||
|
||||
public final List<ConnectionHandler> getConnectionHandlers() {
|
||||
public List<ConnectionHandler> getConnectionHandlers() {
|
||||
return connectionHandlers;
|
||||
}
|
||||
|
||||
@@ -154,24 +125,20 @@ public class NetworkServer extends DefaultMethodsOverrider {
|
||||
return null;
|
||||
}
|
||||
|
||||
public synchronized final boolean stop() {
|
||||
if (!isRunning()) return false;
|
||||
private NetworkServer(int port, PacketHandler packetHandler, EventManager eventManager, Logger logger, int maxRestartAttempts, int restartDelay, int timeout) {
|
||||
this.port = port;
|
||||
this.timeout = timeout;
|
||||
|
||||
if (logger == null) System.out.println("Trying to stop server...");
|
||||
else logger.info("Trying to stop server...");
|
||||
this.packetHandler = packetHandler;
|
||||
this.eventManager = eventManager;
|
||||
this.logger = logger;
|
||||
|
||||
new ArrayList<>(connectionHandlers).forEach(connectionHandler -> connectionHandler.disconnect(true));
|
||||
connectionHandlers.clear();
|
||||
this.maxRestartAttempts = maxRestartAttempts;
|
||||
this.restartDelay = restartDelay;
|
||||
this.currentAttempts = 0;
|
||||
|
||||
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;
|
||||
this.packetHandler.setServerInstance(this);
|
||||
this.packetHandler.registerPacket(new ClientIDPacket());
|
||||
}
|
||||
|
||||
public synchronized final boolean start() {
|
||||
@@ -191,8 +158,6 @@ public class NetworkServer extends DefaultMethodsOverrider {
|
||||
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) {
|
||||
@@ -214,21 +179,17 @@ public class NetworkServer extends DefaultMethodsOverrider {
|
||||
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) {
|
||||
public boolean broadcastPacket(Packet packet) {
|
||||
AtomicBoolean toReturn = new AtomicBoolean(false);
|
||||
connectionHandlers.forEach(connectionHandler -> toReturn.set(connectionHandler.sendPacket(packet)));
|
||||
connectionHandlers.forEach(connectionHandler -> {
|
||||
try {
|
||||
if (!toReturn.get()) return;
|
||||
toReturn.set(connectionHandler.sendPacket(packet));
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
toReturn.set(false);
|
||||
}
|
||||
});
|
||||
|
||||
return toReturn.get();
|
||||
}
|
||||
|
||||
@@ -244,25 +205,36 @@ public class NetworkServer extends DefaultMethodsOverrider {
|
||||
if (logger == null) System.out.println("Accepted connection from " + socket.getRemoteSocketAddress());
|
||||
else logger.info("Accepted connection from " + socket.getRemoteSocketAddress());
|
||||
|
||||
S_IncomingConnectionEvent incomingConnectionEvent = new S_IncomingConnectionEvent(this, socket);
|
||||
IncomingConnectionEvent incomingConnectionEvent = new IncomingConnectionEvent(this, socket);
|
||||
|
||||
eventManager.executeEvent(incomingConnectionEvent);
|
||||
|
||||
if (incomingConnectionEvent.isCancelled()) {
|
||||
socket.close();
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
|
||||
ConnectionHandler connectionHandler = new ConnectionHandler(this, socket, connectionHandlers.size() + 1);
|
||||
connectionHandlers.add(connectionHandler);
|
||||
eventManager.executeEvent(new S_ConnectionHandlerConnectedEvent(connectionHandler));
|
||||
}
|
||||
} 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));
|
||||
} catch (IOException | ClassNotFoundException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
stop();
|
||||
public final boolean sendPacket(int clientID, Packet packet) throws IOException, ClassNotFoundException {
|
||||
return getConnectionHandlerByID(clientID).sendPacket(packet);
|
||||
}
|
||||
|
||||
public final boolean sendPacket(Packet packet, int clientID) throws IOException, ClassNotFoundException {
|
||||
return sendPacket(clientID, packet);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
EventManager eventManager = new EventManager();
|
||||
PacketHandler packetHandler = new PacketHandler();
|
||||
|
||||
NetworkServer networkServer = new ServerBuilder().setMaxReconnectAttempts(2).setEventManager(eventManager).setPacketHandler(packetHandler).setPort(1918).build();
|
||||
networkServer.start();
|
||||
}
|
||||
}
|
||||
|
@@ -1,21 +1,20 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
* Copyright (C) 2025 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;
|
||||
package me.finn.unlegitlibrary.network.system.server.events;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.server.ConnectionHandler;
|
||||
|
||||
public class S_ConnectionHandlerConnectedEvent extends Event {
|
||||
|
||||
public final class ConnectionHandlerConnectedEvent extends Event {
|
||||
public final ConnectionHandler connectionHandler;
|
||||
|
||||
public S_ConnectionHandlerConnectedEvent(ConnectionHandler connectionHandler) {
|
||||
public ConnectionHandlerConnectedEvent(ConnectionHandler connectionHandler) {
|
||||
this.connectionHandler = connectionHandler;
|
||||
}
|
||||
}
|
@@ -1,21 +1,20 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
* Copyright (C) 2025 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;
|
||||
package me.finn.unlegitlibrary.network.system.server.events;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.server.ConnectionHandler;
|
||||
|
||||
public class S_ConnectionHandlerDisconnectedEvent extends Event {
|
||||
|
||||
public final class ConnectionHandlerDisconnectedEvent extends Event {
|
||||
public final ConnectionHandler connectionHandler;
|
||||
|
||||
public S_ConnectionHandlerDisconnectedEvent(ConnectionHandler connectionHandler) {
|
||||
public ConnectionHandlerDisconnectedEvent(ConnectionHandler connectionHandler) {
|
||||
this.connectionHandler = connectionHandler;
|
||||
}
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright (C) 2025 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;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.CancellableEvent;
|
||||
import me.finn.unlegitlibrary.network.system.server.NetworkServer;
|
||||
|
||||
import java.net.Socket;
|
||||
|
||||
public class IncomingConnectionEvent extends CancellableEvent {
|
||||
public final NetworkServer server;
|
||||
public final Socket socket;
|
||||
|
||||
public IncomingConnectionEvent(NetworkServer server, Socket socket) {
|
||||
this.server = server;
|
||||
this.socket = socket;
|
||||
}
|
||||
}
|
@@ -1,19 +1,18 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
* Copyright (C) 2025 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;
|
||||
package me.finn.unlegitlibrary.network.system.server.events;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.server.ConnectionHandler;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
import me.finn.unlegitlibrary.network.system.server.ConnectionHandler;
|
||||
|
||||
public class S_PacketReceivedEvent extends Event {
|
||||
|
||||
public final ConnectionHandler connectionHandler;
|
||||
public final Packet packet;
|
||||
|
@@ -1,26 +1,23 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
* Copyright (C) 2025 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;
|
||||
package me.finn.unlegitlibrary.network.system.server.events;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.server.ConnectionHandler;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
import me.finn.unlegitlibrary.network.system.server.ConnectionHandler;
|
||||
|
||||
public class S_PacketFailedReceivedEvent extends Event {
|
||||
|
||||
public class S_PacketReceivedFailedEvent extends Event {
|
||||
public final ConnectionHandler connectionHandler;
|
||||
public final Packet packet;
|
||||
public final Exception exception;
|
||||
|
||||
public S_PacketFailedReceivedEvent(ConnectionHandler connectionHandler, Packet packet, Exception exception) {
|
||||
public S_PacketReceivedFailedEvent(ConnectionHandler connectionHandler, Packet packet) {
|
||||
this.connectionHandler = connectionHandler;
|
||||
this.packet = packet;
|
||||
this.exception = exception;
|
||||
}
|
||||
}
|
@@ -1,32 +1,23 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
* Copyright (C) 2025 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;
|
||||
package me.finn.unlegitlibrary.network.system.server.events;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.server.ConnectionHandler;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
import me.finn.unlegitlibrary.network.system.server.ConnectionHandler;
|
||||
|
||||
public class S_PacketSendEvent extends Event {
|
||||
|
||||
public final ConnectionHandler connectionHandler;
|
||||
public final Packet packet;
|
||||
public final ConnectionHandler connectionHandler;
|
||||
|
||||
public S_PacketSendEvent(ConnectionHandler connectionHandler, Packet packet) {
|
||||
this.connectionHandler = connectionHandler;
|
||||
public S_PacketSendEvent(Packet packet, ConnectionHandler connectionHandler) {
|
||||
this.packet = packet;
|
||||
this.connectionHandler = connectionHandler;
|
||||
}
|
||||
}
|
@@ -1,22 +1,23 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
* Copyright (C) 2025 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;
|
||||
package me.finn.unlegitlibrary.network.system.server.events;
|
||||
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
import me.finn.unlegitlibrary.network.system.server.ConnectionHandler;
|
||||
|
||||
public class S_ReceiveThreadFailedEvent extends Event {
|
||||
public class S_PacketSendFailedEvent extends Event {
|
||||
public final Packet packet;
|
||||
public final ConnectionHandler connectionHandler;
|
||||
public final Exception exception;
|
||||
|
||||
public S_ReceiveThreadFailedEvent(ConnectionHandler connectionHandler, Exception exception) {
|
||||
public S_PacketSendFailedEvent(Packet packet, ConnectionHandler connectionHandler) {
|
||||
this.packet = packet;
|
||||
this.connectionHandler = connectionHandler;
|
||||
this.exception = exception;
|
||||
}
|
||||
}
|
@@ -1,22 +1,23 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
* Copyright (C) 2025 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;
|
||||
package me.finn.unlegitlibrary.network.system.server.events;
|
||||
|
||||
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;
|
||||
public final Object received;
|
||||
public final ConnectionHandler connectionHandler;
|
||||
|
||||
public S_UnknownObjectReceivedEvent(Object received, ConnectionHandler connectionHandler) {
|
||||
this.received = received;
|
||||
this.connectionHandler = connectionHandler;
|
||||
}
|
||||
}
|
@@ -1,34 +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
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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.CancellableEvent;
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
import me.finn.unlegitlibrary.network.system.server.NetworkServer;
|
||||
|
||||
import java.net.Socket;
|
||||
|
||||
public class S_IncomingConnectionEvent extends CancellableEvent {
|
||||
|
||||
public final NetworkServer server;
|
||||
public final Socket socket;
|
||||
|
||||
public S_IncomingConnectionEvent(NetworkServer server, Socket socket) {
|
||||
this.server = server;
|
||||
this.socket = socket;
|
||||
}
|
||||
}
|
@@ -1,31 +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
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
@@ -1,34 +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
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
@@ -1,37 +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
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
@@ -1,37 +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
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
@@ -189,7 +189,7 @@ public final class Logger {
|
||||
}
|
||||
}
|
||||
|
||||
public final void debug(String debug) throws IOException {
|
||||
public final void debug(String debug) {
|
||||
// Get current date and time
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
|
||||
Date date = new Date();
|
||||
|
Reference in New Issue
Block a user