Skip to content

Commit

Permalink
feat: ClientHandler automatically remove
Browse files Browse the repository at this point in the history
  • Loading branch information
LapisBerry committed May 23, 2024
1 parent 9616c17 commit 969d67d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/lapisberry/net/ClientHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ private void startListeningClientPacket() {
System.out.println("Packet from client cannot be read.");
}
}
close();
}

private void sendPacketToClient(ServerPacket packet) {
Expand All @@ -61,6 +62,17 @@ private void sendPacketToClient(ServerPacket packet) {
}
}

public void close() {
try {
socket.close();
outputStream.close();
inputStream.close();
} catch (IOException e) {
System.out.println("ClientHandler cannot be closed.");
}
server.removeClientHandler(this);
}

// Getters Setters
public Socket getSocket() {
return socket;
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/lapisberry/net/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.lapisberry.game.controllers.GameController;
import com.lapisberry.game.controllers.LobbyController;
import com.lapisberry.net.packets.ClientPacket;
import com.lapisberry.net.packets.JoinResponsePacket;
import com.lapisberry.net.packets.ServerPacket;
import com.lapisberry.utils.Config;

import java.io.IOException;
Expand Down Expand Up @@ -65,4 +63,8 @@ public void close() {
System.out.println("Server cannot be closed.");
}
}

public void removeClientHandler(ClientHandler clientHandler) {
clientHandlers.remove(clientHandler);
}
}

0 comments on commit 969d67d

Please sign in to comment.