Skip to content

Commit

Permalink
fix: Multi Instance Network server/client not starting
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Oct 29, 2024
1 parent fbcfa0f commit 5ff2827
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 13 deletions.
6 changes: 0 additions & 6 deletions src/main/java/xyz/nifeather/morph/network/ReasonCodes.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ private void prepareInstance(boolean isMaster)
@Initializer
private void load(MorphConfigManager configManager)
{
configManager.bind(enabled, ConfigOption.ENABLE_MULTIINSTANCE);
configManager.bind(isMaster, ConfigOption.IS_MASTER);

isMaster.onValueChanged((o, n) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import xiamomc.pluginbase.XiaMoJavaPlugin;
import xyz.nifeather.morph.network.multiInstance.protocol.IClientHandler;
Expand Down Expand Up @@ -51,6 +52,7 @@ public void onOpen(WebSocket webSocket, ClientHandshake clientHandshake)
@Override
public void stop(int timeout, String closeMessage) throws InterruptedException
{
logger.info("[S] Stopping instance server...");
super.stop(timeout, closeMessage);

running = false;
Expand Down Expand Up @@ -78,9 +80,12 @@ public void onMessage(WebSocket webSocket, String msg)
}

@Override
public void onError(WebSocket webSocket, Exception e)
public void onError(@Nullable WebSocket webSocket, Exception e)
{
logger.warn("[S] An error occurred with socket '%s': %s".formatted(webSocket.getRemoteSocketAddress(), e.getMessage()));
String socketAddress = "<unknown socket @ %s>".formatted(webSocket);
if (webSocket != null) socketAddress = webSocket.getRemoteSocketAddress().toString();

logger.warn("[S] An error occurred with socket '%s': %s".formatted(socketAddress, e.getMessage()));
e.printStackTrace();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import org.bukkit.Bukkit;
import org.java_websocket.WebSocket;
import org.java_websocket.framing.CloseFrame;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -14,7 +15,6 @@
import xyz.nifeather.morph.MorphPluginObject;
import xyz.nifeather.morph.config.ConfigOption;
import xyz.nifeather.morph.config.MorphConfigManager;
import xyz.nifeather.morph.network.ReasonCodes;
import xyz.nifeather.morph.network.multiInstance.IInstanceService;
import xyz.nifeather.morph.network.multiInstance.protocol.IClientHandler;
import xyz.nifeather.morph.network.multiInstance.protocol.Operation;
Expand Down Expand Up @@ -194,7 +194,7 @@ private void sendCommand(WebSocket socket, MIS2CCommand<?> command)

private void disconnect(WebSocket socket, String reason)
{
this.sendCommand(socket, new MIS2CDisconnectCommand(ReasonCodes.DISCONNECT, reason));
this.sendCommand(socket, new MIS2CDisconnectCommand(CloseFrame.NORMAL, reason));

this.allowedSockets.remove(socket);
socket.close();
Expand Down Expand Up @@ -350,6 +350,8 @@ else if (operation == Operation.REMOVE)
@Override
public void onMessage(InstanceServer.WsRecord wsRecord, InstanceServer server)
{
if (!allowedSockets.containsKey(wsRecord.socket())) return;

this.addSchedule(() -> this.onText(wsRecord));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package xyz.nifeather.morph.network.multiInstance.slave;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.framing.CloseFrame;
import org.java_websocket.handshake.ServerHandshake;
import org.slf4j.Logger;
import xiamomc.pluginbase.XiaMoJavaPlugin;
Expand Down Expand Up @@ -53,8 +54,10 @@ public void onClose(int code, String reason, boolean isFromRemote)
{
logger.info("[C] Connection closed with code '%s' and reason '%s'".formatted(code, reason));

boolean shouldRetry = true;

var waitingSecond = 20;
if (code == 1001 || code == 1000)
if (shouldRetry)
{
logger.info("[C] Retrying connect after %s seconds...".formatted(waitingSecond));
plugin.schedule(this::reconnect, waitingSecond * 20);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import org.bukkit.Bukkit;
import org.java_websocket.framing.CloseFrame;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import xiamomc.morph.network.commands.C2S.AbstractC2SCommand;
Expand All @@ -14,7 +15,6 @@
import xyz.nifeather.morph.MorphPluginObject;
import xyz.nifeather.morph.config.ConfigOption;
import xyz.nifeather.morph.config.MorphConfigManager;
import xyz.nifeather.morph.network.ReasonCodes;
import xyz.nifeather.morph.network.multiInstance.IInstanceService;
import xyz.nifeather.morph.network.multiInstance.master.MasterInstance;
import xyz.nifeather.morph.network.multiInstance.protocol.IMasterHandler;
Expand Down Expand Up @@ -43,7 +43,7 @@ private boolean stopClient()

try
{
client.close(ReasonCodes.DISCONNECT);
client.close(CloseFrame.NORMAL);
client = null;

return true;
Expand Down

0 comments on commit 5ff2827

Please sign in to comment.