From 8a1088bfcd37c67bd3867a2ee6f9e19cbe21efe8 Mon Sep 17 00:00:00 2001 From: Brokkonaut Date: Fri, 6 Oct 2023 08:17:26 +0200 Subject: [PATCH] fix npe and better sync --- .../globalserver/ClientConnection.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/cubeside/globalserver/ClientConnection.java b/src/main/java/de/cubeside/globalserver/ClientConnection.java index b28054b..4f4553d 100644 --- a/src/main/java/de/cubeside/globalserver/ClientConnection.java +++ b/src/main/java/de/cubeside/globalserver/ClientConnection.java @@ -44,6 +44,7 @@ public class ClientConnection extends Thread { private String account; private ClientConfig client; private HashMap playersOnline; + private final Object sendSync = new Object(); public ClientConnection(GlobalServer server, Socket socket) { this.server = server; @@ -233,7 +234,9 @@ public void sendLoginResultAndActivateEncryption(boolean success, ClientConfig c try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); digest.update(randomNumberServer); - digest.update(config.getPassword().getBytes(StandardCharsets.UTF_8)); + if (config != null) { + digest.update(config.getPassword().getBytes(StandardCharsets.UTF_8)); + } digest.update(randomNumberClient); secret = digest.digest(); } catch (NoSuchAlgorithmException e) { @@ -270,7 +273,7 @@ public void sendLoginResultAndActivateEncryption(boolean success, ClientConfig c } public void sendPing() { - synchronized (this) { + synchronized (sendSync) { if (os != null) { try { os.writeByte(ServerPacketType.PING.ordinal()); @@ -283,7 +286,7 @@ public void sendPing() { } private void sendPong() { - synchronized (this) { + synchronized (sendSync) { if (os != null) { try { os.writeByte(ServerPacketType.PONG.ordinal()); @@ -296,7 +299,7 @@ private void sendPong() { } public void sendServerOnline(String server) { - synchronized (this) { + synchronized (sendSync) { if (os != null) { try { os.writeByte(ServerPacketType.SERVER_ONLINE.ordinal()); @@ -311,7 +314,7 @@ public void sendServerOnline(String server) { } public void sendServerOffline(String server) { - synchronized (this) { + synchronized (sendSync) { if (os != null) { try { os.writeByte(ServerPacketType.SERVER_OFFLINE.ordinal()); @@ -325,7 +328,7 @@ public void sendServerOffline(String server) { } public void sendPlayerOnline(String server, UUID uuid, String name, long joinTime) { - synchronized (this) { + synchronized (sendSync) { if (os != null) { try { os.writeByte(ServerPacketType.PLAYER_ONLINE.ordinal()); @@ -344,7 +347,7 @@ public void sendPlayerOnline(String server, UUID uuid, String name, long joinTim } public void sendPlayerOffline(String server, UUID uuid) { - synchronized (this) { + synchronized (sendSync) { if (os != null) { try { os.writeByte(ServerPacketType.PLAYER_OFFLINE.ordinal()); @@ -366,7 +369,7 @@ public void sendData(ClientConnection fromServer, String channel, UUID targetUui if (channel == null) { throw new NullPointerException("channel"); } - synchronized (this) { + synchronized (sendSync) { if (os != null) { try { os.writeByte(ServerPacketType.DATA.ordinal());