Skip to content

Commit

Permalink
fix npe and better sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Brokkonaut committed Oct 6, 2023
1 parent f12b826 commit 8a1088b
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/main/java/de/cubeside/globalserver/ClientConnection.java
Expand Up @@ -44,6 +44,7 @@ public class ClientConnection extends Thread {
private String account;
private ClientConfig client;
private HashMap<UUID, OnlinePlayer> playersOnline;
private final Object sendSync = new Object();

public ClientConnection(GlobalServer server, Socket socket) {
this.server = server;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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());
Expand All @@ -283,7 +286,7 @@ public void sendPing() {
}

private void sendPong() {
synchronized (this) {
synchronized (sendSync) {
if (os != null) {
try {
os.writeByte(ServerPacketType.PONG.ordinal());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand Down

0 comments on commit 8a1088b

Please sign in to comment.