Fixed:Client Resilience (TCP)

This commit is contained in:
rubenpirreram
2026-05-08 17:16:09 +02:00
parent e78096d2e0
commit 9fe23c6f9e
7 changed files with 91 additions and 10 deletions
@@ -15,5 +15,6 @@ public enum EventType {
PICK_OPTIONAL_BUILD,
SKIP_NO_DRAWABLE,
SKIP_PLAYER_DISCONNECTED,
RECONNECT_PLAYER,
NO_OPTIONAL_CARD
}
@@ -0,0 +1,30 @@
package it.polimi.ingsw.gc14.Network.NetworkEvents;
import it.polimi.ingsw.gc14.Controller.GameController;
import it.polimi.ingsw.gc14.Network.EventType;
import it.polimi.ingsw.gc14.Network.NetworkEvent;
import java.io.Serializable;
//TODO
public class ReconnectPlayer extends NetworkEvent implements Serializable{
/**
* Class constructor.
* Initializes all the attributes.
* @param username the name of the player requesting the event
*/
public ReconnectPlayer(String username){
super(username, EventType.RECONNECT_PLAYER, false);
}
/**
* @param gameController the Game Controller on which to apply the event
* @return true if the player could skipTheTurn, false otherwise
*/
@Override
public boolean apply(GameController gameController){
return gameController.ReconnectPlayer(username);
}
}
@@ -81,6 +81,7 @@ public class TCPClient implements IClient {
return false;
}
new Thread(this::receiveMessage, "tcp-reader").start();
// Socket heartbeat
this.heartbeatSocket = new Socket(hostname, heartbeatPort);
this.heartbeatOut =heartbeatSocket.getOutputStream() ;
@@ -89,10 +90,11 @@ public class TCPClient implements IClient {
// manda subito username per associare i due socket lato server
new ObjectOutputStream(heartbeatSocket.getOutputStream()).writeObject(user);
heartbeatOut.flush();
new Thread(this::heartbeatLoop, "heartbeat").start();
running = true;
new Thread(this::heartbeatLoop, "heartbeat").start();
return true;
} catch (IOException e) {
@@ -7,6 +7,7 @@ import it.polimi.ingsw.gc14.Network.ClientPlayer;
import it.polimi.ingsw.gc14.Network.EventType;
import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.Network.NetworkEvents.AddPlayer;
import it.polimi.ingsw.gc14.Network.NetworkEvents.ReconnectPlayer;
import java.io.*;
import java.net.*;
@@ -124,17 +125,12 @@ public class TCPServer {
);
clientSocket.getOutputStream().write(1);
pendingHeartbeat.put(username, handler);
synchronized (controller) {
handler.notifyModel(controller.getModel());
}
handler.notifyModel(controller.getModel());
Thread thread = new Thread(handler);
thread.start();
clientHandlers.add(handler);
connectedPlayers++;
actionQueue.add(new ReconnectPlayer(username));
} else {
clientSocket.getOutputStream().write(-1);
clientSocket.close();