fix to prevent server chat from displaying the handshakes spam
This commit is contained in:
@@ -18,12 +18,14 @@ public class Server implements PacketHandler {
|
|||||||
|
|
||||||
private final DatagramSocket serverSocket;
|
private final DatagramSocket serverSocket;
|
||||||
private final Map<InetSocketAddress, ServerConnexion> connexions;
|
private final Map<InetSocketAddress, ServerConnexion> connexions;
|
||||||
|
private final Map<InetSocketAddress, Instant> connexionTimes;
|
||||||
private final SocketReader reader;
|
private final SocketReader reader;
|
||||||
private final Map<String, ArrayList<ServerConnexion>> rooms;
|
private final Map<String, ArrayList<ServerConnexion>> rooms;
|
||||||
|
|
||||||
public Server(int port) throws SocketException {
|
public Server(int port) throws SocketException {
|
||||||
this.serverSocket = new DatagramSocket(port);
|
this.serverSocket = new DatagramSocket(port);
|
||||||
this.connexions = new HashMap<>();
|
this.connexions = new HashMap<>();
|
||||||
|
this.connexionTimes = new HashMap<>();
|
||||||
this.reader = new SocketReader(serverSocket, this);
|
this.reader = new SocketReader(serverSocket, this);
|
||||||
this.rooms = new HashMap<>();
|
this.rooms = new HashMap<>();
|
||||||
}
|
}
|
||||||
@@ -126,4 +128,22 @@ public class Server implements PacketHandler {
|
|||||||
this.connexions.get(address).visit(packet);
|
this.connexions.get(address).visit(packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Avoid the server to spam the chat
|
||||||
|
* @param address the address of the connexion
|
||||||
|
* @return true if the connexion is the first received or older than 5 seconds
|
||||||
|
*/
|
||||||
|
public boolean handleConnexionTime(InetSocketAddress address) {
|
||||||
|
if (!connexionTimes.containsKey(address)) {
|
||||||
|
connexionTimes.put(address, Instant.now());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Instant lastConnexion = connexionTimes.get(address);
|
||||||
|
if (Instant.now().isAfter(lastConnexion.plusSeconds(5))) {
|
||||||
|
connexionTimes.put(address, Instant.now());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import java.io.IOException;
|
|||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import network.SocketWriter;
|
import network.SocketWriter;
|
||||||
import network.protocol.Packet;
|
import network.protocol.Packet;
|
||||||
@@ -96,7 +98,9 @@ public class ServerConnexion implements PacketVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitPacket(HandshakePacket packet) {
|
public void visitPacket(HandshakePacket packet) {
|
||||||
System.out.println("[Server] Handshake received from " + clientAddress);
|
if(this.server.handleConnexionTime(this.clientAddress)) {
|
||||||
|
System.out.println("[Server] Handshake received from " + clientAddress);
|
||||||
|
}
|
||||||
sendPacket(packet);
|
sendPacket(packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user