more stable connexion
Some checks failed
Linux arm64 / Build (push) Failing after 5m1s

This commit is contained in:
2025-01-26 14:54:46 +01:00
parent e51cc23459
commit 47dc35ebfa
7 changed files with 64 additions and 12 deletions

View File

@@ -12,30 +12,68 @@ public class ConnexionStatusView extends BaseView {
private Client client; private Client client;
private Server server; private Server server;
public ConnexionStatusView(StateMachine stateMachine, String address, short port) throws UnknownHostException, IOException { private String displayText = "Connecting ...";
this(stateMachine, null, new Client(address, port));
public ConnexionStatusView(StateMachine stateMachine, String address, short port)
throws UnknownHostException, IOException {
super(stateMachine);
Thread t = new Thread(() -> {
try {
this.client = new Client(address, port);
bindListeners();
} catch (IOException e) {
e.printStackTrace();
onDisconnect();
}
});
t.start();
} }
public ConnexionStatusView(StateMachine stateMachine, short port) throws UnknownHostException, IOException { public ConnexionStatusView(StateMachine stateMachine, short port) throws UnknownHostException, IOException {
this(stateMachine, new Server(port), new Client("localhost", port)); super(stateMachine);
Thread t = new Thread(() -> {
try {
this.server = new Server(port);
this.client = new Client("localhost", port);
bindListeners();
} catch (IOException e) {
e.printStackTrace();
onDisconnect();
}
});
t.start();
}
private void bindListeners() {
this.client.onConnect.connect(this::onConnect);
this.client.onClosed.connect(this::onLeave);
this.client.onDisconnect.connect(this::onDisconnect);
} }
private ConnexionStatusView(StateMachine stateMachine, Server server, Client client) { private ConnexionStatusView(StateMachine stateMachine, Server server, Client client) {
super(stateMachine); super(stateMachine);
this.client = client; this.client = client;
this.client.onConnect.connect(this::onConnect);
this.client.onClosed.connect(this::onLeave);
this.server = server; this.server = server;
} }
public void onConnect() { public void onConnect() {
// System.out.println("Connecté");
this.stateMachine.pushState(new MultiPlayerView(stateMachine, client)); this.stateMachine.pushState(new MultiPlayerView(stateMachine, client));
} }
public void onDisconnect() {
if (client != null) {
String reason = client.getDisconnectReason();
if (reason == null)
displayText = "Le serveur a fermé la connexion !";
else
displayText = "Vous avez été déconnecté ! Raison : " + client.getDisconnectReason();
} else {
displayText = "La connexion a échoué !";
}
}
public void onLeave() { public void onLeave() {
// System.out.println("Quitté !");
this.client.onDisconnect.clear();
this.client = null; this.client = null;
// on passe le menu de la connexion // on passe le menu de la connexion
this.closeMenu(); this.closeMenu();
@@ -43,7 +81,14 @@ public class ConnexionStatusView extends BaseView {
@Override @Override
public void render() { public void render() {
ImGui.text("Connecting ..."); ImGui.text(displayText);
renderReturnButton();
}
@Override
public void closeMenu() {
super.closeMenu();
cleanResources();
} }
@Override @Override

View File

@@ -14,6 +14,7 @@ public class MultiMenu extends BaseView {
public MultiMenu(StateMachine stateMachine) { public MultiMenu(StateMachine stateMachine) {
super(stateMachine); super(stateMachine);
address.resize(20);
} }
private void renderCreate() { private void renderCreate() {

View File

@@ -20,9 +20,7 @@ public class MultiPlayerView extends BaseView {
} }
public void onDisconnect() { public void onDisconnect() {
// System.out.println("ohohohohohohoho");
this.stateMachine.popState(); this.stateMachine.popState();
this.client.onDisconnect.clear();
} }
@Override @Override

View File

@@ -16,6 +16,8 @@ public class Client {
public final Signal onDisconnect = new Signal(); public final Signal onDisconnect = new Signal();
public final Signal onClosed = new Signal(); public final Signal onClosed = new Signal();
String disconnectReason = null;
public Client(String address, short port) throws UnknownHostException, IOException { public Client(String address, short port) throws UnknownHostException, IOException {
this.clientConnection = new ClientConnexion(address, port, this); this.clientConnection = new ClientConnexion(address, port, this);
this.game = new Game(); this.game = new Game();
@@ -39,6 +41,10 @@ public class Client {
return game; return game;
} }
public String getDisconnectReason() {
return disconnectReason;
}
public void forceDisconnect() { public void forceDisconnect() {
this.onClosed.emit(); this.onClosed.emit();
stop(); stop();

View File

@@ -45,6 +45,7 @@ public class ClientConnexion extends Connexion {
@Override @Override
public void visitPacket(DisconnectPacket packet) { public void visitPacket(DisconnectPacket packet) {
this.client.disconnectReason = packet.getReason();
close(); close();
} }

View File

@@ -49,6 +49,7 @@ public class Server {
this.acceptThread.cancel(); this.acceptThread.cancel();
this.logicThread.cancel(); this.logicThread.cancel();
for (ServerConnexion connexion : this.connexions) { for (ServerConnexion connexion : this.connexions) {
connexion.nukeConnection();
connexion.close(); connexion.close();
} }
} }

View File

@@ -33,7 +33,7 @@ public class ServerConnexion extends Connexion {
public void nukeConnection() { public void nukeConnection() {
if (player != null) { if (player != null) {
sendPacket(new DisconnectPacket("Server stopped")); sendPacket(new DisconnectPacket("Le serveur a été fermé !"));
this.server.broadcastPacket(new PlayerLeavePacket(player.getId())); this.server.broadcastPacket(new PlayerLeavePacket(player.getId()));
this.server.getGame().removePlayer(player.getId()); this.server.getGame().removePlayer(player.getId());
} }