This commit is contained in:
@@ -1,14 +1,15 @@
|
||||
package network.server;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import game.Game;
|
||||
import game.Player;
|
||||
import game.Game.GameState;
|
||||
import game.Player;
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.packets.EndGamePacket;
|
||||
import network.protocol.packets.StartGamePacket;
|
||||
@@ -17,17 +18,17 @@ import sudoku.structure.MultiDoku;
|
||||
|
||||
public class Server {
|
||||
|
||||
final ServerSocket serverSocket;
|
||||
final List<ServerConnexion> connexions;
|
||||
private final ServerAcceptThread acceptThread;
|
||||
final DatagramSocket serverSocket;
|
||||
final Map<InetSocketAddress, ServerConnexion> connexions;
|
||||
private final ServerReadThread 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.serverSocket = new DatagramSocket(port);
|
||||
this.connexions = new HashMap<>();
|
||||
this.acceptThread = new ServerReadThread(this);
|
||||
this.acceptThread.start();
|
||||
this.logicThread = new ServerLogicThread(this);
|
||||
this.logicThread.start();
|
||||
@@ -35,7 +36,7 @@ public class Server {
|
||||
}
|
||||
|
||||
public void broadcastPacket(Packet packet) {
|
||||
for (ServerConnexion connexion : this.connexions) {
|
||||
for (ServerConnexion connexion : this.connexions.values()) {
|
||||
connexion.sendPacket(packet);
|
||||
}
|
||||
}
|
||||
@@ -50,8 +51,9 @@ public class Server {
|
||||
}
|
||||
|
||||
private void checkConnexions() {
|
||||
for (var it = connexions.iterator(); it.hasNext();) {
|
||||
ServerConnexion connexion = it.next();
|
||||
for (var it = connexions.entrySet().iterator(); it.hasNext();) {
|
||||
var entry = it.next();
|
||||
ServerConnexion connexion = entry.getValue();
|
||||
if (!connexion.update()) {
|
||||
connexion.close();
|
||||
connexion.nukeConnection();
|
||||
@@ -68,10 +70,10 @@ public class Server {
|
||||
public void stop() {
|
||||
this.acceptThread.cancel();
|
||||
this.logicThread.cancel();
|
||||
for (ServerConnexion connexion : this.connexions) {
|
||||
for (ServerConnexion connexion : this.connexions.values()) {
|
||||
connexion.nukeConnection();
|
||||
connexion.close();
|
||||
}
|
||||
this.serverSocket.close();
|
||||
}
|
||||
|
||||
public Player addPlayer(String pseudo) {
|
||||
@@ -88,7 +90,7 @@ public class Server {
|
||||
public void startGame(MultiDoku doku, long gameDuration) {
|
||||
Instant now = Instant.now();
|
||||
this.game.startGame(doku, now, gameDuration);
|
||||
for (ServerConnexion connexion : this.connexions) {
|
||||
for (ServerConnexion connexion : this.connexions.values()) {
|
||||
connexion.setSudoku(doku.clone());
|
||||
}
|
||||
broadcastPacket(new StartGamePacket(SudokuSerializer.serializeSudoku(doku).toString(), now, gameDuration));
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
package network.server;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
import game.Game;
|
||||
import game.Player;
|
||||
import game.Game.GameState;
|
||||
import game.Player;
|
||||
import network.Connexion;
|
||||
import network.protocol.packets.ChangeCellPacket;
|
||||
import network.protocol.packets.ConnexionInfoPacket;
|
||||
@@ -29,14 +29,14 @@ public class ServerConnexion extends Connexion {
|
||||
private Player player = null;
|
||||
private MultiDoku doku;
|
||||
|
||||
public ServerConnexion(Socket socket, Server server) throws IOException {
|
||||
super(socket);
|
||||
public ServerConnexion(InetSocketAddress remoteAddress, Server server) throws IOException {
|
||||
super(server.serverSocket, remoteAddress);
|
||||
this.server = server;
|
||||
this.keepAliveHandler = new KeepAliveHandler(this);
|
||||
}
|
||||
|
||||
public boolean update() {
|
||||
if (shouldClose || isClosed())
|
||||
if (shouldClose)
|
||||
return false;
|
||||
return this.keepAliveHandler.update();
|
||||
}
|
||||
@@ -49,15 +49,6 @@ public class ServerConnexion extends Connexion {
|
||||
}
|
||||
}
|
||||
|
||||
@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()) {
|
||||
@@ -87,7 +78,7 @@ public class ServerConnexion extends Connexion {
|
||||
|
||||
@Override
|
||||
public void visitPacket(DisconnectPacket packet) {
|
||||
close();
|
||||
//TODO: close connexion
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -164,4 +155,8 @@ public class ServerConnexion extends Connexion {
|
||||
this.server.broadcastPacket(new UpdatePlayerScorePacket(player.getId(), player.getRemainingCells()));
|
||||
}
|
||||
|
||||
public void close() {
|
||||
this.shouldClose = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
50
app/src/main/java/network/server/ServerReadThread.java
Normal file
50
app/src/main/java/network/server/ServerReadThread.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package network.server;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
import network.protocol.Packet;
|
||||
|
||||
public class ServerReadThread extends Thread {
|
||||
|
||||
private final Server server;
|
||||
|
||||
public ServerReadThread(Server server) {
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
this.server.serverSocket.close();
|
||||
interrupt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
while (!interrupted()) {
|
||||
byte[] buffer = new byte[1024];
|
||||
DatagramPacket dataPacket = new DatagramPacket(buffer, buffer.length);
|
||||
this.server.serverSocket.receive(dataPacket);
|
||||
|
||||
InetSocketAddress address = new InetSocketAddress(dataPacket.getAddress(), dataPacket.getPort());
|
||||
|
||||
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(dataPacket.getData()));
|
||||
Packet packet = (Packet) ois.readObject();
|
||||
|
||||
if (!this.server.connexions.containsKey(dataPacket.getSocketAddress())) {
|
||||
this.server.connexions.put(address, new ServerConnexion(address, server));
|
||||
}
|
||||
|
||||
this.server.connexions.get(address).visit(packet);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user