Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 05df8a56a7 | |||
| af0ac0ff77 | |||
| 375e0eddbd | |||
| 7b90a81365 | |||
| 11a246adfd |
@@ -39,6 +39,8 @@ public class SudokuSelector {
|
|||||||
|
|
||||||
private final SmoothProgressBar genProgressBar;
|
private final SmoothProgressBar genProgressBar;
|
||||||
|
|
||||||
|
private volatile boolean genDone = false;
|
||||||
|
|
||||||
public SudokuSelector(boolean canGenEmptyGrid, String confirmMessage) {
|
public SudokuSelector(boolean canGenEmptyGrid, String confirmMessage) {
|
||||||
this.canGenEmptyGrid = canGenEmptyGrid;
|
this.canGenEmptyGrid = canGenEmptyGrid;
|
||||||
this.confirmMessage = confirmMessage;
|
this.confirmMessage = confirmMessage;
|
||||||
@@ -74,6 +76,8 @@ public class SudokuSelector {
|
|||||||
int filled = this.doku.getFilledCells().size();
|
int filled = this.doku.getFilledCells().size();
|
||||||
int total = this.doku.getCells().size();
|
int total = this.doku.getCells().size();
|
||||||
this.genProgressBar.render(filled / (float) total);
|
this.genProgressBar.render(filled / (float) total);
|
||||||
|
if (genDone)
|
||||||
|
ImGui.closeCurrentPopup();
|
||||||
ImGui.endPopup();
|
ImGui.endPopup();
|
||||||
} else {
|
} else {
|
||||||
stopGenThread();
|
stopGenThread();
|
||||||
@@ -82,12 +86,14 @@ public class SudokuSelector {
|
|||||||
|
|
||||||
private void selectSudoku(MultiDoku doku, boolean empty) {
|
private void selectSudoku(MultiDoku doku, boolean empty) {
|
||||||
this.doku = doku;
|
this.doku = doku;
|
||||||
|
this.genDone = false;
|
||||||
ImGui.openPopup("genProgress");
|
ImGui.openPopup("genProgress");
|
||||||
this.genThread = new Thread(() -> {
|
this.genThread = new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
if (!empty) {
|
if (!empty) {
|
||||||
SudokuFactory.fillDoku(doku, Difficulty.values()[difficulty.get()]);
|
SudokuFactory.fillDoku(doku, Difficulty.values()[difficulty.get()]);
|
||||||
}
|
}
|
||||||
|
this.genDone = true;
|
||||||
this.onSelect.emit(this.doku);
|
this.onSelect.emit(this.doku);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|||||||
@@ -1,40 +1,54 @@
|
|||||||
package network;
|
package network;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
import java.io.ObjectOutputStream;
|
import java.io.ObjectOutputStream;
|
||||||
import java.net.Socket;
|
import java.net.DatagramPacket;
|
||||||
|
import java.net.DatagramSocket;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
import network.protocol.Packet;
|
import network.protocol.Packet;
|
||||||
import network.protocol.PacketVisitor;
|
import network.protocol.PacketVisitor;
|
||||||
|
|
||||||
public abstract class Connexion implements PacketVisitor {
|
public abstract class Connexion implements PacketVisitor {
|
||||||
|
|
||||||
final Socket socket;
|
protected final DatagramSocket socket;
|
||||||
private final ObjectOutputStream objectOutputStream;
|
protected final InetSocketAddress address;
|
||||||
private final ConnexionThread connexionThread;
|
|
||||||
|
|
||||||
public Connexion(Socket socket) throws IOException {
|
public Connexion(DatagramSocket socket, InetSocketAddress address) {
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
this.objectOutputStream = new ObjectOutputStream(this.socket.getOutputStream());
|
this.address = address;
|
||||||
this.connexionThread = new ConnexionThread(this);
|
|
||||||
this.connexionThread.start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isClosed() {
|
public static record ReadInfo(Packet packet, InetSocketAddress address) {}
|
||||||
return this.socket.isClosed();
|
|
||||||
|
public static ReadInfo readPacket(final DatagramSocket socket)
|
||||||
|
throws IOException, ClassNotFoundException {
|
||||||
|
byte[] buffer = new byte[65535];
|
||||||
|
DatagramPacket dataPacket = new DatagramPacket(buffer, buffer.length);
|
||||||
|
socket.receive(dataPacket);
|
||||||
|
|
||||||
|
InetSocketAddress address = new InetSocketAddress(dataPacket.getAddress(), dataPacket.getPort());
|
||||||
|
|
||||||
|
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(dataPacket.getData()));
|
||||||
|
Packet packet = (Packet) ois.readObject();
|
||||||
|
return new ReadInfo(packet, address);
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void sendPacket(Packet packet) {
|
public void sendPacket(Packet packet) {
|
||||||
try {
|
try {
|
||||||
objectOutputStream.writeObject(packet);
|
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||||
objectOutputStream.flush();
|
ObjectOutputStream oos = new ObjectOutputStream(stream);
|
||||||
|
oos.writeObject(packet);
|
||||||
|
oos.flush();
|
||||||
|
byte[] data = stream.toByteArray();
|
||||||
|
DatagramPacket dataPacket = new DatagramPacket(data, data.length, this.address.getAddress(),
|
||||||
|
this.address.getPort());
|
||||||
|
this.socket.send(dataPacket);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Error while sending packet ! " + e.getLocalizedMessage());
|
e.printStackTrace();
|
||||||
close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() {
|
|
||||||
this.connexionThread.cancel();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,44 +0,0 @@
|
|||||||
package network;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.ObjectInputStream;
|
|
||||||
|
|
||||||
import network.protocol.Packet;
|
|
||||||
|
|
||||||
public class ConnexionThread extends Thread {
|
|
||||||
|
|
||||||
private final Connexion connexion;
|
|
||||||
private final ObjectInputStream objectInputStream;
|
|
||||||
|
|
||||||
public ConnexionThread(Connexion connexion) throws IOException {
|
|
||||||
this.connexion = connexion;
|
|
||||||
this.objectInputStream = new ObjectInputStream(this.connexion.socket.getInputStream());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
while (!interrupted()) {
|
|
||||||
try {
|
|
||||||
// System.out.println(objectInputStream.available());
|
|
||||||
Object o = objectInputStream.readObject();
|
|
||||||
if (o instanceof Packet packet) {
|
|
||||||
connexion.visit(packet);
|
|
||||||
}
|
|
||||||
} catch (ClassNotFoundException | IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
this.connexion.close();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void cancel() {
|
|
||||||
try {
|
|
||||||
objectInputStream.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
interrupt();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,11 +1,13 @@
|
|||||||
package network.client;
|
package network.client;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.Socket;
|
import java.net.DatagramSocket;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
import game.Player;
|
import game.Player;
|
||||||
import network.Connexion;
|
import network.Connexion;
|
||||||
|
import network.protocol.Packet;
|
||||||
import network.protocol.packets.ChangeCellPacket;
|
import network.protocol.packets.ChangeCellPacket;
|
||||||
import network.protocol.packets.ConnexionInfoPacket;
|
import network.protocol.packets.ConnexionInfoPacket;
|
||||||
import network.protocol.packets.DisconnectPacket;
|
import network.protocol.packets.DisconnectPacket;
|
||||||
@@ -21,18 +23,35 @@ import sudoku.io.SudokuSerializer;
|
|||||||
public class ClientConnexion extends Connexion {
|
public class ClientConnexion extends Connexion {
|
||||||
|
|
||||||
private final Client client;
|
private final Client client;
|
||||||
|
private final Thread readThread;
|
||||||
|
|
||||||
public ClientConnexion(String address, short port, Client client) throws UnknownHostException, IOException {
|
public ClientConnexion(String address, short port, Client client) throws UnknownHostException, IOException {
|
||||||
super(new Socket(address, port));
|
super(new DatagramSocket(), new InetSocketAddress(address, port));
|
||||||
this.client = client;
|
this.client = client;
|
||||||
|
this.readThread = new Thread(this::readPackets);
|
||||||
|
this.readThread.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void readPackets() {
|
||||||
|
while (!Thread.interrupted()) {
|
||||||
|
try {
|
||||||
|
Packet packet = Connexion.readPacket(this.socket).packet();
|
||||||
|
|
||||||
|
visit(packet);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void close() {
|
public void close() {
|
||||||
if (!this.isClosed()) {
|
if (!this.socket.isClosed()) {
|
||||||
super.close();
|
|
||||||
sendPacket(new DisconnectPacket(""));
|
sendPacket(new DisconnectPacket(""));
|
||||||
client.onDisconnect.emit();
|
client.onDisconnect.emit();
|
||||||
|
this.readThread.interrupt();
|
||||||
|
this.socket.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
package network.server;
|
package network.server;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.ServerSocket;
|
import java.net.DatagramSocket;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.ArrayList;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.Map;
|
||||||
|
|
||||||
import game.Game;
|
import game.Game;
|
||||||
import game.Player;
|
|
||||||
import game.Game.GameState;
|
import game.Game.GameState;
|
||||||
|
import game.Player;
|
||||||
import network.protocol.Packet;
|
import network.protocol.Packet;
|
||||||
import network.protocol.packets.EndGamePacket;
|
import network.protocol.packets.EndGamePacket;
|
||||||
import network.protocol.packets.StartGamePacket;
|
import network.protocol.packets.StartGamePacket;
|
||||||
@@ -17,17 +18,17 @@ import sudoku.structure.MultiDoku;
|
|||||||
|
|
||||||
public class Server {
|
public class Server {
|
||||||
|
|
||||||
final ServerSocket serverSocket;
|
final DatagramSocket serverSocket;
|
||||||
final List<ServerConnexion> connexions;
|
final Map<InetSocketAddress, ServerConnexion> connexions;
|
||||||
private final ServerAcceptThread acceptThread;
|
private final ServerReadThread acceptThread;
|
||||||
private final ServerLogicThread logicThread;
|
private final ServerLogicThread logicThread;
|
||||||
private final Game game;
|
private final Game game;
|
||||||
private int nextPlayerId = 0;
|
private int nextPlayerId = 0;
|
||||||
|
|
||||||
public Server(short port) throws IOException {
|
public Server(short port) throws IOException {
|
||||||
this.serverSocket = new ServerSocket(port);
|
this.serverSocket = new DatagramSocket(port);
|
||||||
this.connexions = new ArrayList<>();
|
this.connexions = new HashMap<>();
|
||||||
this.acceptThread = new ServerAcceptThread(this);
|
this.acceptThread = new ServerReadThread(this);
|
||||||
this.acceptThread.start();
|
this.acceptThread.start();
|
||||||
this.logicThread = new ServerLogicThread(this);
|
this.logicThread = new ServerLogicThread(this);
|
||||||
this.logicThread.start();
|
this.logicThread.start();
|
||||||
@@ -35,7 +36,7 @@ public class Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void broadcastPacket(Packet packet) {
|
public void broadcastPacket(Packet packet) {
|
||||||
for (ServerConnexion connexion : this.connexions) {
|
for (ServerConnexion connexion : this.connexions.values()) {
|
||||||
connexion.sendPacket(packet);
|
connexion.sendPacket(packet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -50,8 +51,9 @@ public class Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void checkConnexions() {
|
private void checkConnexions() {
|
||||||
for (var it = connexions.iterator(); it.hasNext();) {
|
for (var it = connexions.entrySet().iterator(); it.hasNext();) {
|
||||||
ServerConnexion connexion = it.next();
|
var entry = it.next();
|
||||||
|
ServerConnexion connexion = entry.getValue();
|
||||||
if (!connexion.update()) {
|
if (!connexion.update()) {
|
||||||
connexion.close();
|
connexion.close();
|
||||||
connexion.nukeConnection();
|
connexion.nukeConnection();
|
||||||
@@ -68,10 +70,10 @@ public class Server {
|
|||||||
public void stop() {
|
public void stop() {
|
||||||
this.acceptThread.cancel();
|
this.acceptThread.cancel();
|
||||||
this.logicThread.cancel();
|
this.logicThread.cancel();
|
||||||
for (ServerConnexion connexion : this.connexions) {
|
for (ServerConnexion connexion : this.connexions.values()) {
|
||||||
connexion.nukeConnection();
|
connexion.nukeConnection();
|
||||||
connexion.close();
|
|
||||||
}
|
}
|
||||||
|
this.serverSocket.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Player addPlayer(String pseudo) {
|
public Player addPlayer(String pseudo) {
|
||||||
@@ -88,7 +90,7 @@ public class Server {
|
|||||||
public void startGame(MultiDoku doku, long gameDuration) {
|
public void startGame(MultiDoku doku, long gameDuration) {
|
||||||
Instant now = Instant.now();
|
Instant now = Instant.now();
|
||||||
this.game.startGame(doku, now, gameDuration);
|
this.game.startGame(doku, now, gameDuration);
|
||||||
for (ServerConnexion connexion : this.connexions) {
|
for (ServerConnexion connexion : this.connexions.values()) {
|
||||||
connexion.setSudoku(doku.clone());
|
connexion.setSudoku(doku.clone());
|
||||||
}
|
}
|
||||||
broadcastPacket(new StartGamePacket(SudokuSerializer.serializeSudoku(doku).toString(), now, gameDuration));
|
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;
|
package network.server;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.Socket;
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
import game.Game;
|
import game.Game;
|
||||||
import game.Player;
|
|
||||||
import game.Game.GameState;
|
import game.Game.GameState;
|
||||||
|
import game.Player;
|
||||||
import network.Connexion;
|
import network.Connexion;
|
||||||
import network.protocol.packets.ChangeCellPacket;
|
import network.protocol.packets.ChangeCellPacket;
|
||||||
import network.protocol.packets.ConnexionInfoPacket;
|
import network.protocol.packets.ConnexionInfoPacket;
|
||||||
@@ -29,14 +29,14 @@ public class ServerConnexion extends Connexion {
|
|||||||
private Player player = null;
|
private Player player = null;
|
||||||
private MultiDoku doku;
|
private MultiDoku doku;
|
||||||
|
|
||||||
public ServerConnexion(Socket socket, Server server) throws IOException {
|
public ServerConnexion(InetSocketAddress remoteAddress, Server server) throws IOException {
|
||||||
super(socket);
|
super(server.serverSocket, remoteAddress);
|
||||||
this.server = server;
|
this.server = server;
|
||||||
this.keepAliveHandler = new KeepAliveHandler(this);
|
this.keepAliveHandler = new KeepAliveHandler(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean update() {
|
public boolean update() {
|
||||||
if (shouldClose || isClosed())
|
if (shouldClose)
|
||||||
return false;
|
return false;
|
||||||
return this.keepAliveHandler.update();
|
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() {
|
private void finishLogin() {
|
||||||
// send players that have already joined (excluding this one)
|
// send players that have already joined (excluding this one)
|
||||||
for (Player p : this.server.getGame().getPlayers().values()) {
|
for (Player p : this.server.getGame().getPlayers().values()) {
|
||||||
@@ -87,7 +78,7 @@ public class ServerConnexion extends Connexion {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitPacket(DisconnectPacket packet) {
|
public void visitPacket(DisconnectPacket packet) {
|
||||||
close();
|
//TODO: close connexion
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -164,4 +155,8 @@ public class ServerConnexion extends Connexion {
|
|||||||
this.server.broadcastPacket(new UpdatePlayerScorePacket(player.getId(), player.getRemainingCells()));
|
this.server.broadcastPacket(new UpdatePlayerScorePacket(player.getId(), player.getRemainingCells()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void close() {
|
||||||
|
this.shouldClose = true;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
43
app/src/main/java/network/server/ServerReadThread.java
Normal file
43
app/src/main/java/network/server/ServerReadThread.java
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package network.server;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
|
import network.Connexion;
|
||||||
|
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()) {
|
||||||
|
Connexion.ReadInfo read = Connexion.readPacket(this.server.serverSocket);
|
||||||
|
Packet packet = read.packet();
|
||||||
|
InetSocketAddress address = read.address();
|
||||||
|
|
||||||
|
if (!this.server.connexions.containsKey(address)) {
|
||||||
|
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