This commit is contained in:
49
app/src/main/java/network/server/KeepAliveHandler.java
Normal file
49
app/src/main/java/network/server/KeepAliveHandler.java
Normal 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));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
28
app/src/main/java/network/server/ServerLogicThread.java
Normal file
28
app/src/main/java/network/server/ServerLogicThread.java
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user