This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user