network structure
Some checks failed
Linux arm64 / Build (push) Failing after 5m1s

This commit is contained in:
2025-01-23 18:59:11 +01:00
parent b05351bc20
commit 5e99cd92df
21 changed files with 429 additions and 36 deletions

View File

@@ -0,0 +1,28 @@
package network.server;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.List;
public class Server {
final ServerSocket serverSocket;
final List<ServerConnexion> connexions;
private final ServerThread thread;
public Server(short port) throws IOException {
this.serverSocket = new ServerSocket(port);
this.connexions = new ArrayList<>();
this.thread = new ServerThread(this);
this.thread.start();
}
public void stop() {
this.thread.cancel();
for (ServerConnexion connexion : this.connexions) {
connexion.close();
}
}
}

View File

@@ -0,0 +1,35 @@
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;
public class ServerConnexion extends Connexion{
public ServerConnexion(Socket socket) throws IOException {
super(socket);
System.out.println("Bonjour le client !");
sendKeepAlive();
}
public void sendKeepAlive() throws IOException {
Random r = new Random();
sendPacket(new KeepAlivePacket(r.nextLong()));
}
@Override
public void visit(ConnexionInfoPacket packet) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'visit'");
}
@Override
public void visit(KeepAlivePacket packet) {
System.out.println("Je l'ai reçu !");
}
}

View File

@@ -0,0 +1,36 @@
package network.server;
import java.io.IOException;
import java.net.Socket;
public class ServerThread extends Thread {
private final Server server;
public ServerThread(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.connexions.add(serverConnection);
}
} catch(IOException e) {
// e.printStackTrace();
}
}
}