better network structure
Some checks failed
Linux arm64 / Build (push) Has been cancelled

This commit is contained in:
2025-01-23 22:24:23 +01:00
parent 5e99cd92df
commit bfe98a2cf0
22 changed files with 248 additions and 93 deletions

View File

@@ -6,17 +6,12 @@ import imgui.ImGui;
import imgui.ImVec2;
import imgui.type.ImInt;
import imgui.type.ImString;
import network.client.Client;
import network.server.Server;
public class MultiMenu extends BaseView {
private final ImInt port = new ImInt(25565);
private final ImString address = new ImString("localhost");
private Server server;
private Client client;
public MultiMenu(StateMachine stateMachine) {
super(stateMachine);
}

View File

@@ -27,7 +27,7 @@ public class StateMachine {
}
public void popState() {
menus.getLast().onKill();
menus.get(menus.size() - 1).onKill();
menus.pop();
}
@@ -43,7 +43,7 @@ public class StateMachine {
ImGui.setNextWindowSize(displaySize);
ImGui.begin("##Main Window", null, ImGuiWindowFlags.NoDecoration | ImGuiWindowFlags.NoMove
| ImGuiWindowFlags.NoSavedSettings | ImGuiWindowFlags.NoBackground);
menus.getLast().render();
menus.get(menus.size() - 1).render();
ImGui.end();
checkEscape();
}

View File

@@ -4,6 +4,9 @@ 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;
@@ -17,8 +20,13 @@ public abstract class Connexion implements PacketVisitor {
this.connexionThread.start();
}
public void sendPacket(Packet packet) throws IOException {
objectOutputStream.writeObject(packet);
public void sendPacket(Packet packet) {
try {
objectOutputStream.writeObject(packet);
} catch (IOException e) {
System.err.println("Error while sending packet ! " + e.getLocalizedMessage());
close();
}
}
public void close() {

View File

@@ -3,6 +3,8 @@ package network;
import java.io.IOException;
import java.io.ObjectInputStream;
import network.protocol.Packet;
public class ConnexionThread extends Thread{
private final Connexion connexion;
@@ -19,7 +21,7 @@ public class ConnexionThread extends Thread{
try {
Object o = objectInputStream.readObject();
if (o instanceof Packet packet) {
connexion.visit(packet);
connexion.visitPacket(packet);
}
} catch (ClassNotFoundException | IOException e) {
break;

View File

@@ -1,8 +0,0 @@
package network;
import java.nio.ByteBuffer;
public class PacketFactory {
}

View File

@@ -1,15 +0,0 @@
package network;
import network.packets.ConnexionInfoPacket;
import network.packets.KeepAlivePacket;
public interface PacketVisitor {
default void visit(Packet packet) {
packet.accept(this);
}
void visit(ConnexionInfoPacket packet);
void visit(KeepAlivePacket packet);
}

View File

@@ -1,7 +0,0 @@
package network;
public enum Packets {
ConnectionInfo, KeepAlive
}

View File

@@ -7,11 +7,16 @@ public class Client {
private final ClientConnexion clientConnection;
public Client(String address, short port) throws UnknownHostException, IOException {
this.clientConnection = new ClientConnexion(address, port);
this.clientConnection = new ClientConnexion(address, port, this);
}
public void stop() {
this.clientConnection.close();
}
public void onDisconnect() {
// do some stuff
System.out.println("OSEKOUR");
}
}

View File

@@ -5,29 +5,39 @@ import java.net.Socket;
import java.net.UnknownHostException;
import network.Connexion;
import network.packets.ConnexionInfoPacket;
import network.packets.KeepAlivePacket;
import network.protocol.packets.ConnexionInfoPacket;
import network.protocol.packets.DisconnectPacket;
import network.protocol.packets.KeepAlivePacket;
public class ClientConnexion extends Connexion {
public ClientConnexion(String address, short port) throws UnknownHostException, IOException {
private final Client client;
public ClientConnexion(String address, short port, Client client) throws UnknownHostException, IOException {
super(new Socket(address, port));
this.client = client;
}
@Override
public void visit(ConnexionInfoPacket packet) {
public void close() {
super.close();
client.onDisconnect();
}
@Override
public void visit(KeepAlivePacket packet) {
System.out.println("Coucou !!!!!!!!!!!!!!!!!!!");
try {
sendPacket(packet);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public void visitPacket(ConnexionInfoPacket packet) {
}
@Override
public void visitPacket(KeepAlivePacket packet) {
// we just send the packet back to the server
sendPacket(packet);
}
@Override
public void visitPacket(DisconnectPacket packet) {
close();
}
}

View File

@@ -1,4 +1,4 @@
package network;
package network.protocol;
import java.io.Serializable;

View File

@@ -1,11 +1,8 @@
package network.client;
package network.protocol;
import java.util.ArrayList;
import java.util.List;
import network.Packet;
import network.PacketVisitor;
public class PacketDispatcher {
private final List<PacketVisitor> handlers;
@@ -16,7 +13,7 @@ public class PacketDispatcher {
public void dispatch(Packet packet) {
for (PacketVisitor handler : handlers) {
handler.visit(packet);
handler.visitPacket(packet);
}
}

View File

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

View File

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

View File

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

View File

@@ -1,8 +1,8 @@
package network.packets;
package network.protocol.packets;
import network.Packet;
import network.PacketVisitor;
import network.Packets;
import network.protocol.Packet;
import network.protocol.PacketVisitor;
import network.protocol.Packets;
public class ConnexionInfoPacket extends Packet {
@@ -20,6 +20,6 @@ public class ConnexionInfoPacket extends Packet {
@Override
public void accept(PacketVisitor packetVisitor) {
packetVisitor.visit(this);
packetVisitor.visitPacket(this);
}
}

View File

@@ -0,0 +1,23 @@
package network.protocol.packets;
import network.protocol.Packet;
import network.protocol.PacketVisitor;
public class DisconnectPacket extends Packet {
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

@@ -1,8 +1,8 @@
package network.packets;
package network.protocol.packets;
import network.Packet;
import network.PacketVisitor;
import network.Packets;
import network.protocol.Packet;
import network.protocol.PacketVisitor;
import network.protocol.Packets;
public class KeepAlivePacket extends Packet {
@@ -20,7 +20,7 @@ public class KeepAlivePacket extends Packet {
@Override
public void accept(PacketVisitor packetVisitor) {
packetVisitor.visit(this);
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

@@ -5,21 +5,43 @@ import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.List;
import network.protocol.Packet;
public class Server {
final ServerSocket serverSocket;
final List<ServerConnexion> connexions;
private final ServerThread thread;
private final ServerAcceptThread acceptThread;
private final ServerLogicThread logicThread;
public Server(short port) throws IOException {
this.serverSocket = new ServerSocket(port);
this.connexions = new ArrayList<>();
this.thread = new ServerThread(this);
this.thread.start();
this.acceptThread = new ServerAcceptThread(this);
this.acceptThread.start();
this.logicThread = new ServerLogicThread(this);
this.logicThread.start();
}
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();
it.remove();
}
}
}
public void stop() {
this.thread.cancel();
this.acceptThread.cancel();
this.logicThread.cancel();
for (ServerConnexion connexion : this.connexions) {
connexion.close();
}

View File

@@ -3,11 +3,11 @@ package network.server;
import java.io.IOException;
import java.net.Socket;
public class ServerThread extends Thread {
public class ServerAcceptThread extends Thread {
private final Server server;
public ServerThread(Server server) {
public ServerAcceptThread(Server server) {
this.server = server;
}
@@ -25,7 +25,7 @@ public class ServerThread extends Thread {
try {
while(!interrupted()) {
Socket newConnection = this.server.serverSocket.accept();
ServerConnexion serverConnection = new ServerConnexion(newConnection);
ServerConnexion serverConnection = new ServerConnexion(newConnection, this.server);
this.server.connexions.add(serverConnection);
}
} catch(IOException e) {

View File

@@ -2,34 +2,50 @@ package network.server;
import java.io.IOException;
import java.net.Socket;
import java.util.Random;
import network.Connexion;
import network.packets.ConnexionInfoPacket;
import network.packets.KeepAlivePacket;
import network.protocol.packets.ConnexionInfoPacket;
import network.protocol.packets.DisconnectPacket;
import network.protocol.packets.KeepAlivePacket;
public class ServerConnexion extends Connexion{
public class ServerConnexion extends Connexion {
public ServerConnexion(Socket socket) throws IOException {
private final Server server;
private final KeepAliveHandler keepAliveHandler;
private boolean shouldClose = false;
public ServerConnexion(Socket socket, Server server) throws IOException {
super(socket);
System.out.println("Bonjour le client !");
sendKeepAlive();
this.server = server;
this.keepAliveHandler = new KeepAliveHandler(this);
}
public void sendKeepAlive() throws IOException {
Random r = new Random();
sendPacket(new KeepAlivePacket(r.nextLong()));
public boolean update() {
if (shouldClose)
return false;
return this.keepAliveHandler.update();
}
@Override
public void visit(ConnexionInfoPacket packet) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'visit'");
public void close() {
sendPacket(new DisconnectPacket("Server stopped"));
super.close();
shouldClose = true;
}
@Override
public void visit(KeepAlivePacket packet) {
System.out.println("Je l'ai reçu !");
public void visitPacket(ConnexionInfoPacket packet) {
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
}
@Override
public void visitPacket(KeepAlivePacket packet) {
this.keepAliveHandler.recievedKeepAlive(packet.getKeepAliveId());
}
@Override
public void visitPacket(DisconnectPacket packet) {
close();
}
}

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;
}
}
}
}