This commit is contained in:
@@ -20,9 +20,14 @@ public abstract class Connexion implements PacketVisitor {
|
||||
this.connexionThread.start();
|
||||
}
|
||||
|
||||
public void sendPacket(Packet packet) {
|
||||
public boolean isClosed() {
|
||||
return this.socket.isClosed();
|
||||
}
|
||||
|
||||
public synchronized void sendPacket(Packet packet) {
|
||||
try {
|
||||
objectOutputStream.writeObject(packet);
|
||||
objectOutputStream.flush();
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error while sending packet ! " + e.getLocalizedMessage());
|
||||
close();
|
||||
|
||||
@@ -5,7 +5,7 @@ import java.io.ObjectInputStream;
|
||||
|
||||
import network.protocol.Packet;
|
||||
|
||||
public class ConnexionThread extends Thread{
|
||||
public class ConnexionThread extends Thread {
|
||||
|
||||
private final Connexion connexion;
|
||||
private final ObjectInputStream objectInputStream;
|
||||
@@ -17,13 +17,16 @@ public class ConnexionThread extends Thread{
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while(!interrupted()) {
|
||||
while (!interrupted()) {
|
||||
try {
|
||||
// System.out.println(objectInputStream.available());
|
||||
Object o = objectInputStream.readObject();
|
||||
if (o instanceof Packet packet) {
|
||||
connexion.visitPacket(packet);
|
||||
}
|
||||
} catch (ClassNotFoundException | IOException e) {
|
||||
e.printStackTrace();
|
||||
this.connexion.close();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,20 +3,45 @@ package network.client;
|
||||
import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import common.Signal;
|
||||
import game.Game;
|
||||
import game.Player;
|
||||
import network.protocol.packets.LoginPacket;
|
||||
|
||||
public class Client {
|
||||
private final ClientConnexion clientConnection;
|
||||
private final Game game;
|
||||
|
||||
public final Signal onConnect = new Signal();
|
||||
public final Signal onDisconnect = new Signal();
|
||||
public final Signal onClosed = new Signal();
|
||||
|
||||
public Client(String address, short port) throws UnknownHostException, IOException {
|
||||
this.clientConnection = new ClientConnexion(address, port, this);
|
||||
this.game = new Game();
|
||||
login("Player2" + Math.random());
|
||||
}
|
||||
|
||||
public void login(String pseudo) {
|
||||
System.out.println("Logging in with pseudo " + pseudo + " ...");
|
||||
this.clientConnection.sendPacket(new LoginPacket(pseudo));
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
this.clientConnection.close();
|
||||
}
|
||||
|
||||
public void onDisconnect() {
|
||||
// do some stuff
|
||||
System.out.println("OSEKOUR");
|
||||
public void addPlayer(Player player) {
|
||||
this.game.addPlayer(player);
|
||||
}
|
||||
|
||||
|
||||
public Game getGame() {
|
||||
return game;
|
||||
}
|
||||
|
||||
public void forceDisconnect() {
|
||||
this.onClosed.emit();
|
||||
stop();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,14 +4,19 @@ import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import game.Player;
|
||||
import network.Connexion;
|
||||
import network.protocol.packets.ConnexionInfoPacket;
|
||||
import network.protocol.packets.DisconnectPacket;
|
||||
import network.protocol.packets.KeepAlivePacket;
|
||||
import network.protocol.packets.LoginPacket;
|
||||
import network.protocol.packets.PlayerJoinPacket;
|
||||
import network.protocol.packets.PlayerLeavePacket;
|
||||
|
||||
public class ClientConnexion extends Connexion {
|
||||
|
||||
private final Client client;
|
||||
private Player player = null;
|
||||
|
||||
public ClientConnexion(String address, short port, Client client) throws UnknownHostException, IOException {
|
||||
super(new Socket(address, port));
|
||||
@@ -20,13 +25,16 @@ public class ClientConnexion extends Connexion {
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
super.close();
|
||||
client.onDisconnect();
|
||||
if (!this.isClosed()) {
|
||||
super.close();
|
||||
client.onDisconnect.emit();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(ConnexionInfoPacket packet) {
|
||||
|
||||
this.player = this.client.getGame().getPlayerById(packet.getConnectionId());
|
||||
client.onConnect.emit();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -40,4 +48,20 @@ public class ClientConnexion extends Connexion {
|
||||
close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(LoginPacket packet) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacketLogin'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(PlayerJoinPacket packet) {
|
||||
this.client.addPlayer(packet.getPlayer());
|
||||
System.out.println("[Client] " + packet.getPlayer().getPseudo() + " joined the game !");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(PlayerLeavePacket packet) {
|
||||
this.client.getGame().removePlayer(packet.getPlayer());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,9 @@ package network.protocol;
|
||||
import network.protocol.packets.ConnexionInfoPacket;
|
||||
import network.protocol.packets.DisconnectPacket;
|
||||
import network.protocol.packets.KeepAlivePacket;
|
||||
import network.protocol.packets.LoginPacket;
|
||||
import network.protocol.packets.PlayerJoinPacket;
|
||||
import network.protocol.packets.PlayerLeavePacket;
|
||||
|
||||
public interface PacketVisitor {
|
||||
|
||||
@@ -13,5 +16,8 @@ public interface PacketVisitor {
|
||||
void visitPacket(ConnexionInfoPacket packet);
|
||||
void visitPacket(DisconnectPacket packet);
|
||||
void visitPacket(KeepAlivePacket packet);
|
||||
void visitPacket(LoginPacket packet);
|
||||
void visitPacket(PlayerJoinPacket packet);
|
||||
void visitPacket(PlayerLeavePacket packet);
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,6 @@ package network.protocol;
|
||||
|
||||
public enum Packets {
|
||||
|
||||
ConnectionInfo, KeepAlive, Disconnect
|
||||
ConnectionInfo, KeepAlive, Disconnect, Login, PlayerJoin, PlayerLeave
|
||||
|
||||
}
|
||||
|
||||
@@ -2,9 +2,12 @@ package network.protocol.packets;
|
||||
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
import network.protocol.Packets;
|
||||
|
||||
public class DisconnectPacket extends Packet {
|
||||
|
||||
static private final long serialVersionUID = Packets.Disconnect.ordinal();
|
||||
|
||||
private final String reason;
|
||||
|
||||
public DisconnectPacket(String reason) {
|
||||
|
||||
26
app/src/main/java/network/protocol/packets/LoginPacket.java
Normal file
26
app/src/main/java/network/protocol/packets/LoginPacket.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package network.protocol.packets;
|
||||
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
import network.protocol.Packets;
|
||||
|
||||
public class LoginPacket extends Packet {
|
||||
|
||||
static private final long serialVersionUID = Packets.Login.ordinal();
|
||||
|
||||
private final String pseudo;
|
||||
|
||||
public LoginPacket(String pseudo) {
|
||||
this.pseudo = pseudo;
|
||||
}
|
||||
|
||||
public String getPseudo() {
|
||||
return pseudo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(PacketVisitor packetVisitor) {
|
||||
packetVisitor.visitPacket(this);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package network.protocol.packets;
|
||||
|
||||
import game.Player;
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
import network.protocol.Packets;
|
||||
|
||||
public class PlayerJoinPacket extends Packet{
|
||||
|
||||
static private final long serialVersionUID = Packets.PlayerJoin.ordinal();
|
||||
|
||||
private final Player player;
|
||||
|
||||
public PlayerJoinPacket(Player player) {
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
return player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(PacketVisitor packetVisitor) {
|
||||
packetVisitor.visitPacket(this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package network.protocol.packets;
|
||||
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
import network.protocol.Packets;
|
||||
|
||||
public class PlayerLeavePacket extends Packet{
|
||||
|
||||
static private final long serialVersionUID = Packets.PlayerLeave.ordinal();
|
||||
|
||||
private final int playerId;
|
||||
|
||||
public PlayerLeavePacket(int playerId) {
|
||||
this.playerId = playerId;
|
||||
}
|
||||
|
||||
public int getPlayer() {
|
||||
return playerId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(PacketVisitor packetVisitor) {
|
||||
packetVisitor.visitPacket(this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import java.net.ServerSocket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import game.Game;
|
||||
import game.Player;
|
||||
import network.protocol.Packet;
|
||||
|
||||
public class Server {
|
||||
@@ -13,6 +15,8 @@ public class Server {
|
||||
final List<ServerConnexion> connexions;
|
||||
private final ServerAcceptThread acceptThread;
|
||||
private final ServerLogicThread logicThread;
|
||||
private final Game game;
|
||||
private int nextPlayerId = 0;
|
||||
|
||||
public Server(short port) throws IOException {
|
||||
this.serverSocket = new ServerSocket(port);
|
||||
@@ -21,6 +25,7 @@ public class Server {
|
||||
this.acceptThread.start();
|
||||
this.logicThread = new ServerLogicThread(this);
|
||||
this.logicThread.start();
|
||||
this.game = new Game();
|
||||
}
|
||||
|
||||
public void broadcastPacket(Packet packet) {
|
||||
@@ -32,8 +37,9 @@ public class Server {
|
||||
public void update() {
|
||||
for (var it = connexions.iterator(); it.hasNext();) {
|
||||
ServerConnexion connexion = it.next();
|
||||
if(!connexion.update()) {
|
||||
if (!connexion.update()) {
|
||||
connexion.close();
|
||||
connexion.nukeConnection();
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
@@ -47,4 +53,15 @@ public class Server {
|
||||
}
|
||||
}
|
||||
|
||||
public Player addPlayer(String pseudo) {
|
||||
Player p = new Player(nextPlayerId, pseudo);
|
||||
this.game.addPlayer(p);
|
||||
nextPlayerId++;
|
||||
return p;
|
||||
}
|
||||
|
||||
public Game getGame() {
|
||||
return game;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,16 +3,21 @@ package network.server;
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
|
||||
import game.Player;
|
||||
import network.Connexion;
|
||||
import network.protocol.packets.ConnexionInfoPacket;
|
||||
import network.protocol.packets.DisconnectPacket;
|
||||
import network.protocol.packets.KeepAlivePacket;
|
||||
import network.protocol.packets.LoginPacket;
|
||||
import network.protocol.packets.PlayerJoinPacket;
|
||||
import network.protocol.packets.PlayerLeavePacket;
|
||||
|
||||
public class ServerConnexion extends Connexion {
|
||||
|
||||
private final Server server;
|
||||
private final KeepAliveHandler keepAliveHandler;
|
||||
private boolean shouldClose = false;
|
||||
private Player player = null;
|
||||
|
||||
public ServerConnexion(Socket socket, Server server) throws IOException {
|
||||
super(socket);
|
||||
@@ -21,21 +26,36 @@ public class ServerConnexion extends Connexion {
|
||||
}
|
||||
|
||||
public boolean update() {
|
||||
if (shouldClose)
|
||||
if (shouldClose | isClosed())
|
||||
return false;
|
||||
return this.keepAliveHandler.update();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
sendPacket(new DisconnectPacket("Server stopped"));
|
||||
super.close();
|
||||
shouldClose = true;
|
||||
public void nukeConnection() {
|
||||
if (player != null) {
|
||||
sendPacket(new DisconnectPacket("Server stopped"));
|
||||
this.server.broadcastPacket(new PlayerLeavePacket(player.getId()));
|
||||
this.server.getGame().removePlayer(player.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(ConnexionInfoPacket packet) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
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()) {
|
||||
if (p.getId() != player.getId())
|
||||
sendPacket(new PlayerJoinPacket(p));
|
||||
}
|
||||
this.server.broadcastPacket(new PlayerJoinPacket(player));
|
||||
sendPacket(new ConnexionInfoPacket(player.getId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -48,4 +68,27 @@ public class ServerConnexion extends Connexion {
|
||||
close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(LoginPacket packet) {
|
||||
if (this.player != null)
|
||||
return;
|
||||
this.player = this.server.addPlayer(packet.getPseudo());
|
||||
finishLogin();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(ConnexionInfoPacket packet) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacketConnexionInfo'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(PlayerJoinPacket packet) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacketPlayerJoin'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(PlayerLeavePacket packet) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacketPlayerLeave'");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user