forked from UnlegitDqrk/unlegitlibrary
More bug fixes
This commit is contained in:
@@ -13,7 +13,10 @@ import me.finn.unlegitlibrary.utils.DefaultMethodsOverrider;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class EventManager extends DefaultMethodsOverrider {
|
||||
|
||||
@@ -45,31 +48,50 @@ public class EventManager extends DefaultMethodsOverrider {
|
||||
eventListeners.put(listenerClass, clazz);
|
||||
}
|
||||
|
||||
public final void unregisterListener(Class<? extends EventListener> listenerClass) {
|
||||
public synchronized final void unregisterListener(Class<? extends EventListener> listenerClass) {
|
||||
if (!isListenerRegistered(listenerClass)) return;
|
||||
|
||||
Object clazz = eventListeners.get(listenerClass);
|
||||
|
||||
synchronized (registeredListener) {
|
||||
for (Class<? extends Event> eventClass : registeredListener.keySet()) {
|
||||
HashMap<EventPriority, HashMap<Object, Method>> priorityMap = registeredListener.get(eventClass);
|
||||
List<Class<? extends Event>> eventsToRemove = new ArrayList<>();
|
||||
|
||||
for (Map.Entry<Class<? extends Event>, HashMap<EventPriority, HashMap<Object, Method>>> entry : registeredListener.entrySet()) {
|
||||
Class<? extends Event> eventClass = entry.getKey();
|
||||
HashMap<EventPriority, HashMap<Object, Method>> priorityMap = entry.getValue();
|
||||
|
||||
if (priorityMap != null) {
|
||||
synchronized (priorityMap) {
|
||||
for (EventPriority priority : priorityMap.keySet()) {
|
||||
HashMap<Object, Method> listeners = priorityMap.get(priority);
|
||||
List<EventPriority> prioritiesToRemove = new ArrayList<>();
|
||||
|
||||
for (Map.Entry<EventPriority, HashMap<Object, Method>> priorityEntry : priorityMap.entrySet()) {
|
||||
EventPriority priority = priorityEntry.getKey();
|
||||
HashMap<Object, Method> listeners = priorityEntry.getValue();
|
||||
|
||||
if (listeners != null) {
|
||||
listeners.remove(clazz);
|
||||
if (listeners.isEmpty()) priorityMap.remove(priority);
|
||||
if (listeners.isEmpty()) {
|
||||
prioritiesToRemove.add(priority);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (priorityMap.isEmpty()) registeredListener.remove(eventClass);
|
||||
for (EventPriority priority : prioritiesToRemove) {
|
||||
priorityMap.remove(priority);
|
||||
}
|
||||
|
||||
if (priorityMap.isEmpty()) {
|
||||
eventsToRemove.add(eventClass);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (Class<? extends Event> eventClass : eventsToRemove) {
|
||||
registeredListener.remove(eventClass);
|
||||
}
|
||||
}
|
||||
|
||||
eventListeners.remove(listenerClass);
|
||||
}
|
||||
|
||||
|
@@ -111,10 +111,10 @@ public class NetworkClient extends DefaultMethodsOverrider {
|
||||
}
|
||||
|
||||
public synchronized final void connect() throws IOException, InterruptedException {
|
||||
try {
|
||||
if (isConnected()) return;
|
||||
if (debugLog) System.out.println("Connecting to server...");
|
||||
|
||||
try {
|
||||
socket = new Socket(host, port);
|
||||
socket.setTcpNoDelay(false);
|
||||
|
||||
@@ -132,7 +132,7 @@ public class NetworkClient extends DefaultMethodsOverrider {
|
||||
if (debugLog) System.out.println("Connected to Server. Attempts: " + attempt);
|
||||
} catch (SocketException exception) {
|
||||
if (isAutoReconnectEnabled()) reconnect();
|
||||
else throw exception;
|
||||
else if (!receiveThread.isInterrupted()) throw exception;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,9 +189,9 @@ public class NetworkClient extends DefaultMethodsOverrider {
|
||||
}
|
||||
|
||||
private final void receive() {
|
||||
if (!isConnected()) return;
|
||||
|
||||
try {
|
||||
if (!isConnected()) return;
|
||||
String command = "";
|
||||
|
||||
while (isConnected()) {
|
||||
@@ -232,20 +232,27 @@ public class NetworkClient extends DefaultMethodsOverrider {
|
||||
}
|
||||
} catch (EOFException exception) {
|
||||
attempt = 1;
|
||||
if (isAutoReconnectEnabled()) {
|
||||
if (maxAttempts == -1) reconnect();
|
||||
else if (attempt <= maxAttempts) reconnect();
|
||||
else {
|
||||
eventManager.executeEvent(new C_StoppedEvent(this));
|
||||
if (isAutoReconnectEnabled()) reconnect();
|
||||
else if (!receiveThread.isInterrupted()) {
|
||||
try {
|
||||
stop();
|
||||
} catch (IOException exception1) {
|
||||
exception.printStackTrace();
|
||||
exception1.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
eventManager.executeEvent(new C_StoppedEvent(this));
|
||||
exception.printStackTrace();
|
||||
}
|
||||
} catch (IOException | ClassNotFoundException exception) {
|
||||
eventManager.executeEvent(new C_StoppedEvent(this));
|
||||
} 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -256,7 +263,8 @@ public class NetworkClient extends DefaultMethodsOverrider {
|
||||
disconnect();
|
||||
} catch (IOException exception) {
|
||||
if (maxAttempts > 0 && attempt > maxAttempts) {
|
||||
exception.printStackTrace();
|
||||
eventManager.executeEvent(new C_StoppedEvent(this));
|
||||
if (!receiveThread.isInterrupted()) exception.printStackTrace();
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -270,14 +278,17 @@ public class NetworkClient extends DefaultMethodsOverrider {
|
||||
} catch (InterruptedException | IOException exception) {
|
||||
if (maxAttempts == -1) reconnect();
|
||||
else if (attempt <= maxAttempts) reconnect();
|
||||
else exception.printStackTrace();
|
||||
else {
|
||||
eventManager.executeEvent(new C_StoppedEvent(this));
|
||||
if (!receiveThread.isInterrupted()) exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
stop();
|
||||
} catch (IOException exception) {
|
||||
eventManager.executeEvent(new C_StoppedEvent(this));
|
||||
exception.printStackTrace();
|
||||
if (!receiveThread.isInterrupted()) exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -15,9 +15,7 @@ import me.finn.unlegitlibrary.network.system.server.events.server.S_StartedEvent
|
||||
import me.finn.unlegitlibrary.network.system.server.events.server.S_StoppedEvent;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.BindException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -57,8 +55,8 @@ public class NetworkServer {
|
||||
return packetHandler;
|
||||
}
|
||||
|
||||
public final boolean isAutoRestart() {
|
||||
return maxAttempts != 0;
|
||||
public final boolean isAutoRestartEnabled() {
|
||||
return maxAttempts != 0 && !incomingConnectionThread.isInterrupted();
|
||||
}
|
||||
|
||||
public final boolean isDebugLogEnabled() {
|
||||
@@ -83,10 +81,10 @@ public class NetworkServer {
|
||||
}
|
||||
|
||||
public synchronized final void start() throws IOException, InterruptedException {
|
||||
try {
|
||||
if (isRunning()) return;
|
||||
if (debugLog) System.out.println("Starting server...");
|
||||
|
||||
try {
|
||||
clientHandlers.clear();
|
||||
|
||||
serverSocket = new ServerSocket(port);
|
||||
@@ -97,13 +95,8 @@ public class NetworkServer {
|
||||
|
||||
if (debugLog) System.out.println("Server started on port " + port + ". Attempts: " + attempt);
|
||||
} catch (BindException exception) {
|
||||
if (isAutoRestart()) {
|
||||
if (attempt > maxAttempts && maxAttempts != -1) throw exception;
|
||||
if (debugLog) System.out.println("Failed to start! Retrying... (Attempt: " + attempt++ + ")");
|
||||
|
||||
Thread.sleep(attemptDelayInSec * 1000L);
|
||||
start();
|
||||
} else throw exception;
|
||||
if (isAutoRestartEnabled()) restart();
|
||||
else if (!incomingConnectionThread.isInterrupted()) throw exception;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,7 +104,9 @@ public class NetworkServer {
|
||||
if (!isRunning()) return;
|
||||
if (debugLog) System.out.println("Stopping server...");
|
||||
|
||||
clientHandlers.forEach(clientHandler -> {
|
||||
List<ClientHandler> handlersToDisconnect = new ArrayList<>(clientHandlers);
|
||||
|
||||
handlersToDisconnect.forEach(clientHandler -> {
|
||||
try {
|
||||
clientHandler.disconnect();
|
||||
} catch (IOException exception) {
|
||||
@@ -119,6 +114,7 @@ public class NetworkServer {
|
||||
}
|
||||
});
|
||||
|
||||
handlersToDisconnect.clear();
|
||||
clientHandlers.clear();
|
||||
|
||||
serverSocket.close();
|
||||
@@ -135,9 +131,9 @@ public class NetworkServer {
|
||||
}
|
||||
|
||||
private final void incomingConnection() {
|
||||
try {
|
||||
if (!isRunning()) return;
|
||||
|
||||
try {
|
||||
while (isRunning()) {
|
||||
Socket socket = serverSocket.accept();
|
||||
if (socket == null) continue;
|
||||
@@ -146,11 +142,51 @@ public class NetworkServer {
|
||||
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;
|
||||
|
Reference in New Issue
Block a user