Merge branch 'network'
All checks were successful
Linux arm64 / Build (push) Successful in 23m59s

This commit is contained in:
2025-01-26 21:16:57 +01:00
34 changed files with 1145 additions and 59 deletions

View File

@@ -0,0 +1,40 @@
package network;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import network.protocol.Packet;
import network.protocol.PacketVisitor;
public abstract class Connexion implements PacketVisitor {
final Socket socket;
private final ObjectOutputStream objectOutputStream;
private final ConnexionThread connexionThread;
public Connexion(Socket socket) throws IOException {
this.socket = socket;
this.objectOutputStream = new ObjectOutputStream(this.socket.getOutputStream());
this.connexionThread = new ConnexionThread(this);
this.connexionThread.start();
}
public boolean isClosed() {
return this.socket.isClosed();
}
public synchronized void sendPacket(Packet packet) {
try {
objectOutputStream.writeObject(packet);
objectOutputStream.flush();
} catch (IOException e) {
System.err.println("Error while sending packet ! " + e.getLocalizedMessage());
close();
}
}
public void close() {
this.connexionThread.cancel();
}
}

View File

@@ -0,0 +1,44 @@
package network;
import java.io.IOException;
import java.io.ObjectInputStream;
import network.protocol.Packet;
public class ConnexionThread extends Thread {
private final Connexion connexion;
private final ObjectInputStream objectInputStream;
public ConnexionThread(Connexion connexion) throws IOException {
this.connexion = connexion;
this.objectInputStream = new ObjectInputStream(this.connexion.socket.getInputStream());
}
@Override
public void run() {
while (!interrupted()) {
try {
// System.out.println(objectInputStream.available());
Object o = objectInputStream.readObject();
if (o instanceof Packet packet) {
connexion.visitPacket(packet);
}
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
this.connexion.close();
break;
}
}
}
public void cancel() {
try {
objectInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
interrupt();
}
}

View File

@@ -0,0 +1,57 @@
package network.client;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Random;
import common.Signal;
import game.Game;
import game.Player;
import network.protocol.packets.LoginPacket;
public class Client {
private final ClientConnexion clientConnection;
private final Game game;
public final Signal onConnect = new Signal();
public final Signal onDisconnect = new Signal();
public final Signal onClosed = new Signal();
public final Signal onGameStarted = new Signal();
String disconnectReason = null;
public Client(String address, short port) throws UnknownHostException, IOException {
this.clientConnection = new ClientConnexion(address, port, this);
this.game = new Game();
// temp
Random r = new Random();
login("Player" + r.nextInt());
}
public void login(String pseudo) {
System.out.println("Logging in with pseudo " + pseudo + " ...");
this.clientConnection.sendPacket(new LoginPacket(pseudo));
}
public void stop() {
this.clientConnection.close();
}
public void addPlayer(Player player) {
this.game.addPlayer(player);
}
public Game getGame() {
return game;
}
public String getDisconnectReason() {
return disconnectReason;
}
public void forceDisconnect() {
this.onClosed.emit();
stop();
}
}

View File

@@ -0,0 +1,76 @@
package network.client;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import game.Player;
import network.Connexion;
import network.protocol.packets.ConnexionInfoPacket;
import network.protocol.packets.DisconnectPacket;
import network.protocol.packets.KeepAlivePacket;
import network.protocol.packets.LoginPacket;
import network.protocol.packets.PlayerJoinPacket;
import network.protocol.packets.PlayerLeavePacket;
import network.protocol.packets.StartGamePacket;
import sudoku.io.SudokuSerializer;
public class ClientConnexion extends Connexion {
private final Client client;
private Player player = null;
public ClientConnexion(String address, short port, Client client) throws UnknownHostException, IOException {
super(new Socket(address, port));
this.client = client;
}
@Override
public void close() {
if (!this.isClosed()) {
super.close();
client.onDisconnect.emit();
}
}
@Override
public void visitPacket(ConnexionInfoPacket packet) {
this.player = this.client.getGame().getPlayerById(packet.getConnectionId());
client.onConnect.emit();
}
@Override
public void visitPacket(KeepAlivePacket packet) {
// we just send the packet back to the server
sendPacket(packet);
}
@Override
public void visitPacket(DisconnectPacket packet) {
this.client.disconnectReason = packet.getReason();
close();
}
@Override
public void visitPacket(LoginPacket packet) {
throw new UnsupportedOperationException("Unimplemented method 'visitPacketLogin'");
}
@Override
public void visitPacket(PlayerJoinPacket packet) {
this.client.addPlayer(packet.getPlayer());
System.out.println("[Client] " + packet.getPlayer().getPseudo() + " joined the game !");
}
@Override
public void visitPacket(PlayerLeavePacket packet) {
this.client.getGame().removePlayer(packet.getPlayer());
}
@Override
public void visitPacket(StartGamePacket packet) {
this.client.getGame().startGame(SudokuSerializer.deserializeSudoku(packet.getSerializedSudoku()));
this.client.onGameStarted.emit();
}
}

View File

@@ -0,0 +1,11 @@
package network.protocol;
import java.io.Serializable;
public abstract class Packet implements Serializable {
// public abstract int getId();
public abstract void accept(PacketVisitor packetVisitor);
}

View File

@@ -0,0 +1,28 @@
package network.protocol;
import java.util.ArrayList;
import java.util.List;
public class PacketDispatcher {
private final List<PacketVisitor> handlers;
public PacketDispatcher() {
this.handlers = new ArrayList<>();
}
public void dispatch(Packet packet) {
for (PacketVisitor handler : handlers) {
handler.visitPacket(packet);
}
}
public void registerHandler(PacketVisitor handler) {
handlers.add(handler);
}
public void unregisterHandler(PacketVisitor handler) {
handlers.remove(handler);
}
}

View File

@@ -0,0 +1,6 @@
package network.protocol;
public class PacketFactory {
}

View File

@@ -0,0 +1,25 @@
package network.protocol;
import network.protocol.packets.ConnexionInfoPacket;
import network.protocol.packets.DisconnectPacket;
import network.protocol.packets.KeepAlivePacket;
import network.protocol.packets.LoginPacket;
import network.protocol.packets.PlayerJoinPacket;
import network.protocol.packets.PlayerLeavePacket;
import network.protocol.packets.StartGamePacket;
public interface PacketVisitor {
default void visitPacket(Packet packet) {
packet.accept(this);
}
void visitPacket(ConnexionInfoPacket packet);
void visitPacket(DisconnectPacket packet);
void visitPacket(KeepAlivePacket packet);
void visitPacket(LoginPacket packet);
void visitPacket(PlayerJoinPacket packet);
void visitPacket(PlayerLeavePacket packet);
void visitPacket(StartGamePacket packet);
}

View File

@@ -0,0 +1,7 @@
package network.protocol;
public enum Packets {
ConnectionInfo, KeepAlive, Disconnect, Login, PlayerJoin, PlayerLeave, StartGame
}

View File

@@ -0,0 +1,25 @@
package network.protocol.packets;
import network.protocol.Packet;
import network.protocol.PacketVisitor;
import network.protocol.Packets;
public class ConnexionInfoPacket extends Packet {
static private final long serialVersionUID = Packets.ConnectionInfo.ordinal();
private final int connectionId;
public ConnexionInfoPacket(int connectionId) {
this.connectionId = connectionId;
}
public int getConnectionId() {
return connectionId;
}
@Override
public void accept(PacketVisitor packetVisitor) {
packetVisitor.visitPacket(this);
}
}

View File

@@ -0,0 +1,26 @@
package network.protocol.packets;
import network.protocol.Packet;
import network.protocol.PacketVisitor;
import network.protocol.Packets;
public class DisconnectPacket extends Packet {
static private final long serialVersionUID = Packets.Disconnect.ordinal();
private final String reason;
public DisconnectPacket(String reason) {
this.reason = reason;
}
public String getReason() {
return reason;
}
@Override
public void accept(PacketVisitor packetVisitor) {
packetVisitor.visitPacket(this);
}
}

View File

@@ -0,0 +1,26 @@
package network.protocol.packets;
import network.protocol.Packet;
import network.protocol.PacketVisitor;
import network.protocol.Packets;
public class KeepAlivePacket extends Packet {
static private final long serialVersionUID = Packets.KeepAlive.ordinal();
private final long keepAliveId;
public KeepAlivePacket(long keepAliveId) {
this.keepAliveId = keepAliveId;
}
public long getKeepAliveId() {
return keepAliveId;
}
@Override
public void accept(PacketVisitor packetVisitor) {
packetVisitor.visitPacket(this);
}
}

View File

@@ -0,0 +1,26 @@
package network.protocol.packets;
import network.protocol.Packet;
import network.protocol.PacketVisitor;
import network.protocol.Packets;
public class LoginPacket extends Packet {
static private final long serialVersionUID = Packets.Login.ordinal();
private final String pseudo;
public LoginPacket(String pseudo) {
this.pseudo = pseudo;
}
public String getPseudo() {
return pseudo;
}
@Override
public void accept(PacketVisitor packetVisitor) {
packetVisitor.visitPacket(this);
}
}

View File

@@ -0,0 +1,29 @@
package network.protocol.packets;
import game.Player;
import network.protocol.Packet;
import network.protocol.PacketVisitor;
import network.protocol.Packets;
public class PlayerJoinPacket extends Packet{
static private final long serialVersionUID = Packets.PlayerJoin.ordinal();
private final Player player;
public PlayerJoinPacket(Player player) {
this.player = player;
}
public Player getPlayer() {
return player;
}
@Override
public void accept(PacketVisitor packetVisitor) {
packetVisitor.visitPacket(this);
}
}

View File

@@ -0,0 +1,28 @@
package network.protocol.packets;
import network.protocol.Packet;
import network.protocol.PacketVisitor;
import network.protocol.Packets;
public class PlayerLeavePacket extends Packet{
static private final long serialVersionUID = Packets.PlayerLeave.ordinal();
private final int playerId;
public PlayerLeavePacket(int playerId) {
this.playerId = playerId;
}
public int getPlayer() {
return playerId;
}
@Override
public void accept(PacketVisitor packetVisitor) {
packetVisitor.visitPacket(this);
}
}

View File

@@ -0,0 +1,26 @@
package network.protocol.packets;
import network.protocol.Packet;
import network.protocol.PacketVisitor;
import network.protocol.Packets;
public class StartGamePacket extends Packet {
static private final long serialVersionUID = Packets.StartGame.ordinal();
private final String serializedSudoku;
public StartGamePacket(String serializedSudoku) {
this.serializedSudoku = serializedSudoku;
}
public String getSerializedSudoku() {
return serializedSudoku;
}
@Override
public void accept(PacketVisitor packetVisitor) {
packetVisitor.visitPacket(this);
}
}

View File

@@ -0,0 +1,49 @@
package network.server;
import java.util.Random;
import network.protocol.packets.KeepAlivePacket;
public class KeepAliveHandler {
private final ServerConnexion serverConnexion;
private static final int KEEP_ALIVE_COOLDOWN = 5 * 1000;
private long lastKeepAlive = 0;
private long lastSend = 0;
private volatile boolean keepAliveRecieved = false;
public KeepAliveHandler(ServerConnexion serverConnexion) {
this.serverConnexion = serverConnexion;
sendKeepAlive();
}
public boolean update() {
var currentTime = System.currentTimeMillis();
if (currentTime - lastSend > KEEP_ALIVE_COOLDOWN) {
if (keepAliveRecieved) {
sendKeepAlive();
} else {
System.out.println("Zombie");
serverConnexion.close();
return false;
}
}
return true;
}
public void recievedKeepAlive(long keepAliveId) {
if (lastKeepAlive == keepAliveId)
this.keepAliveRecieved = true;
}
private void sendKeepAlive() {
Random r = new Random();
lastKeepAlive = r.nextLong();
lastSend = System.currentTimeMillis();
keepAliveRecieved = false;
this.serverConnexion.sendPacket(new KeepAlivePacket(lastKeepAlive));
}
}

View File

@@ -0,0 +1,76 @@
package network.server;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.List;
import game.Game;
import game.Player;
import network.protocol.Packet;
import network.protocol.packets.StartGamePacket;
import sudoku.io.SudokuSerializer;
import sudoku.structure.MultiDoku;
public class Server {
final ServerSocket serverSocket;
final List<ServerConnexion> connexions;
private final ServerAcceptThread acceptThread;
private final ServerLogicThread logicThread;
private final Game game;
private int nextPlayerId = 0;
public Server(short port) throws IOException {
this.serverSocket = new ServerSocket(port);
this.connexions = new ArrayList<>();
this.acceptThread = new ServerAcceptThread(this);
this.acceptThread.start();
this.logicThread = new ServerLogicThread(this);
this.logicThread.start();
this.game = new Game();
}
public void broadcastPacket(Packet packet) {
for (ServerConnexion connexion : this.connexions) {
connexion.sendPacket(packet);
}
}
public void update() {
for (var it = connexions.iterator(); it.hasNext();) {
ServerConnexion connexion = it.next();
if (!connexion.update()) {
connexion.close();
connexion.nukeConnection();
it.remove();
}
}
}
public void stop() {
this.acceptThread.cancel();
this.logicThread.cancel();
for (ServerConnexion connexion : this.connexions) {
connexion.nukeConnection();
connexion.close();
}
}
public Player addPlayer(String pseudo) {
Player p = new Player(nextPlayerId, pseudo);
this.game.addPlayer(p);
nextPlayerId++;
return p;
}
public Game getGame() {
return game;
}
public void startGame(MultiDoku doku) {
this.game.startGame(doku);
broadcastPacket(new StartGamePacket(SudokuSerializer.serializeSudoku(doku)));
}
}

View File

@@ -0,0 +1,36 @@
package network.server;
import java.io.IOException;
import java.net.Socket;
public class ServerAcceptThread extends Thread {
private final Server server;
public ServerAcceptThread(Server server) {
this.server = server;
}
public void cancel() {
try {
this.server.serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
interrupt();
}
@Override
public void run() {
try {
while(!interrupted()) {
Socket newConnection = this.server.serverSocket.accept();
ServerConnexion serverConnection = new ServerConnexion(newConnection, this.server);
this.server.connexions.add(serverConnection);
}
} catch(IOException e) {
// e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,105 @@
package network.server;
import java.io.IOException;
import java.net.Socket;
import game.Player;
import game.Game.GameState;
import network.Connexion;
import network.protocol.packets.ConnexionInfoPacket;
import network.protocol.packets.DisconnectPacket;
import network.protocol.packets.KeepAlivePacket;
import network.protocol.packets.LoginPacket;
import network.protocol.packets.PlayerJoinPacket;
import network.protocol.packets.PlayerLeavePacket;
import network.protocol.packets.StartGamePacket;
import sudoku.io.SudokuSerializer;
public class ServerConnexion extends Connexion {
private final Server server;
private final KeepAliveHandler keepAliveHandler;
private boolean shouldClose = false;
private Player player = null;
public ServerConnexion(Socket socket, Server server) throws IOException {
super(socket);
this.server = server;
this.keepAliveHandler = new KeepAliveHandler(this);
}
public boolean update() {
if (shouldClose | isClosed())
return false;
return this.keepAliveHandler.update();
}
public void nukeConnection() {
if (player != null) {
sendPacket(new DisconnectPacket("Le serveur a été fermé !"));
this.server.broadcastPacket(new PlayerLeavePacket(player.getId()));
this.server.getGame().removePlayer(player.getId());
}
}
@Override
public synchronized void close() {
if(shouldClose)
return;
super.close();
shouldClose = true;
System.out.println("[Server] Closing connexion !");
}
private void finishLogin() {
// send players that have already joined (excluding this one)
for (Player p : this.server.getGame().getPlayers().values()) {
if (p.getId() != player.getId())
sendPacket(new PlayerJoinPacket(p));
}
this.server.broadcastPacket(new PlayerJoinPacket(player));
sendPacket(new ConnexionInfoPacket(player.getId()));
if (this.server.getGame().getGameState() == GameState.GameGoing) {
sendPacket(new StartGamePacket(SudokuSerializer.serializeSudoku(this.server.getGame().getDoku())));
}
}
@Override
public void visitPacket(KeepAlivePacket packet) {
this.keepAliveHandler.recievedKeepAlive(packet.getKeepAliveId());
}
@Override
public void visitPacket(DisconnectPacket packet) {
close();
}
@Override
public void visitPacket(LoginPacket packet) {
if (this.player != null)
return;
this.player = this.server.addPlayer(packet.getPseudo());
finishLogin();
}
@Override
public void visitPacket(ConnexionInfoPacket packet) {
throw new UnsupportedOperationException("Unimplemented method 'visitPacketConnexionInfo'");
}
@Override
public void visitPacket(PlayerJoinPacket packet) {
throw new UnsupportedOperationException("Unimplemented method 'visitPacketPlayerJoin'");
}
@Override
public void visitPacket(PlayerLeavePacket packet) {
throw new UnsupportedOperationException("Unimplemented method 'visitPacketPlayerLeave'");
}
@Override
public void visitPacket(StartGamePacket packet) {
throw new UnsupportedOperationException("Unimplemented method 'visitPacketStartGame'");
}
}

View File

@@ -0,0 +1,28 @@
package network.server;
public class ServerLogicThread extends Thread {
private final Server server;
public ServerLogicThread(Server server) {
this.server = server;
}
public void cancel() {
interrupt();
}
@Override
public void run() {
while (!interrupted()) {
server.update();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// e.printStackTrace();
break;
}
}
}
}