Skip to content

Commit

Permalink
Fix bungee unknown subchannel and add log along with RuntimeException
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Lauz committed Aug 6, 2023
1 parent 9e6ad2d commit 132f8bf
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ public void run() {
try {
Socket socket = serverSocket.accept();
new ConnectionToClient(socket);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (IOException err) {
MessagesManager.this.plugin.getLogger().severe(err.getMessage());
throw new RuntimeException(err);
}
}
}
Expand Down Expand Up @@ -107,9 +108,6 @@ private void processMessage(Message message) {
String subchannel = packet.getSubchannel();

this.handleMessageBySubchannel(subchannel, packet, client);



} catch (DeconstructPacketErrorException | IOException | UnknownSubchannelException err) {
this.plugin.getLogger().severe(Arrays.toString(err.getStackTrace()));
}
Expand All @@ -124,7 +122,7 @@ private void handleMessageBySubchannel(String subchannel, Packet packet, Connect
case EVENT -> this.handleEventSubchannel(packet);
case QUERY -> this.handleQuerySubchannel(packet);
case HANDSHAKE -> this.handleHandshakeSubchannel(packet, client);
default -> throw new UnknownSubchannelException(subchannel);
default -> throw new UnknownSubchannelException(subchannel, "Will ignore this message");
}
}

Expand Down Expand Up @@ -180,13 +178,14 @@ public void sendToOne(String server, Packet packet) {
ConnectionToClient connection = clients.remove(server);
try {
connection.close();
} catch (IOException | InterruptedException ex) {
throw new RuntimeException(ex);
} catch (IOException | InterruptedException err) {
this.plugin.getLogger().severe(Arrays.toString(err.getStackTrace()));
throw new RuntimeException(err);
}
}
} catch (InvalidPacketException | ConstructPacketErrorException e) { // TODO better exception handling
e.printStackTrace();
throw new RuntimeException(e);
} catch (InvalidPacketException | ConstructPacketErrorException err) { // TODO better exception handling
this.plugin.getLogger().severe(Arrays.toString(err.getStackTrace()));
throw new RuntimeException(err);
}
}

Expand Down Expand Up @@ -244,10 +243,9 @@ public void sendQueryHasPlayedBefore(String serverName, ProxiedPlayer player) {
sendToAll(greetingPacket);
}

} catch (InterruptedException e) { // TODO better exception handling
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
} catch (ExecutionException | InterruptedException err) { // TODO better exception handling
this.plugin.getLogger().severe(Arrays.toString(err.getStackTrace()));
throw new RuntimeException(err);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public String getValue() {
return value;
}

public static Subchannel typeof(String value) {
public static Subchannel typeof(String value) throws UnknownSubchannelException {
for (Subchannel type : Subchannel.values()) {
if (type.getValue().equalsIgnoreCase(value))
return type;
}

throw new IllegalArgumentException("No Subchannel enum with value \"" + value + "\" found.");
throw new UnknownSubchannelException(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ public class UnknownSubchannelException extends Exception{
public UnknownSubchannelException(String subchannel) {
super("Unknown subchannel: " + subchannel);
}

public UnknownSubchannelException(String subchannel, String message) {
super("Unknown subchannel: " + subchannel + " (" + message + ")");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,13 @@ private void processMessage(byte[] message) {

this.handleMessageBySubchannel(subchannel, packet);
} catch (DeconstructPacketErrorException | InvalidPacketException |
ConstructPacketErrorException | IOException err) {
ConstructPacketErrorException | IOException | UnknownSubchannelException err) {
getLogger().severe(Arrays.toString(err.getStackTrace()));
}
}

private void handleMessageBySubchannel(String subchannel, Packet packet) throws ConstructPacketErrorException, IOException, InvalidPacketException {
private void handleMessageBySubchannel(String subchannel, Packet packet)
throws ConstructPacketErrorException, IOException, InvalidPacketException, UnknownSubchannelException {
Subchannel subchannelType = Subchannel.typeof(subchannel);

switch (subchannelType) {
Expand Down Expand Up @@ -291,6 +292,7 @@ public void run() {
}
} catch (IOException error) {
isConnected.set(false);
getLogger().warning(error.getMessage());
getLogger().warning("Connection to server lost, will attempt to reconnect...");
}
}
Expand Down

0 comments on commit 132f8bf

Please sign in to comment.