network: process time out
This commit is contained in:
@@ -20,9 +20,14 @@ public class ClientConnexion implements PacketVisitor, PacketHandler {
|
||||
this.serverAddress = serverAddress;
|
||||
this.socket = socket;
|
||||
this.callback = callback;
|
||||
this.socket.onClose.connect(this::onSocketClose);
|
||||
this.socket.addHandler(this);
|
||||
}
|
||||
|
||||
private void onSocketClose() {
|
||||
this.callback.handleConnexionError();
|
||||
}
|
||||
|
||||
public void close() {
|
||||
this.socket.close();
|
||||
}
|
||||
|
||||
@@ -134,6 +134,7 @@ public class ClientConsole implements ClientListener {
|
||||
public void handleDisconnect() {
|
||||
System.out.println("Disconnected !");
|
||||
stop();
|
||||
this.onDisconnect.emit();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -19,12 +19,13 @@ public class PacketPool {
|
||||
|
||||
private final Stack<ReliablePacketAddress> packetQueue;
|
||||
private final Map<InetSocketAddress, AdressContext> addressContexts;
|
||||
|
||||
|
||||
private final Socket socket;
|
||||
|
||||
private static int MAX_SEND_TRY = 50;
|
||||
private static long RETRY_INTERVAL = 100;
|
||||
private static float PACKET_LOSS_PROBABILITY = 0.75f;
|
||||
private static long SEND_DELAY = 100;
|
||||
private static long RETRY_INTERVAL = SEND_DELAY * 2;
|
||||
private static float PACKET_LOSS_PROBABILITY = 0.5f;
|
||||
|
||||
private static record ReliablePacketAddress(ReliablePacket packet, InetSocketAddress address) {
|
||||
@Override
|
||||
@@ -89,9 +90,16 @@ public class PacketPool {
|
||||
|
||||
private void sendPacketToSocket(ReliablePacket packet, InetSocketAddress address) throws IOException {
|
||||
var packetsSentTries = this.addressContexts.get(address).packetsSentTries;
|
||||
|
||||
if (Math.random() > PACKET_LOSS_PROBABILITY)
|
||||
this.socket.sendPacket(packet, address);
|
||||
|
||||
new Thread(() -> {
|
||||
try {
|
||||
Thread.sleep(SEND_DELAY);
|
||||
if (Math.random() > PACKET_LOSS_PROBABILITY)
|
||||
this.socket.sendPacket(packet, address);
|
||||
} catch (InterruptedException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}).start();
|
||||
|
||||
if (packet.getPacket() instanceof AcknowlegdementPacket)
|
||||
return;
|
||||
@@ -131,11 +139,12 @@ public class PacketPool {
|
||||
// the packet has been received
|
||||
if (!packetsSentTries.containsKey(reliablePacketAddress.packet()))
|
||||
break;
|
||||
|
||||
|
||||
Integer sendCount = packetsSentTries.get(reliablePacketAddress.packet());
|
||||
if (sendCount > MAX_SEND_TRY) {
|
||||
close(reliablePacketAddress.address());
|
||||
debugPrint("Packet" + reliablePacketAddress.packet() + " not send after " + MAX_SEND_TRY + " tries");
|
||||
debugPrint(
|
||||
"Packet" + reliablePacketAddress.packet() + " not send after " + MAX_SEND_TRY + " tries");
|
||||
// simulating a fake disconnect packet
|
||||
this.socket.handlePacket(new DisconnectPacket("Timed out"), reliablePacketAddress.address());
|
||||
break;
|
||||
@@ -161,7 +170,9 @@ public class PacketPool {
|
||||
List<ReliablePacket> packetRecvBuffer = this.addressContexts.get(address).packetRecvBuffer;
|
||||
if (packetRecvBuffer.isEmpty())
|
||||
return null;
|
||||
return Collections.min(packetRecvBuffer, (rel1, rel2) -> {return Integer.compare(rel1.getSeq(), rel2.getSeq());});
|
||||
return Collections.min(packetRecvBuffer, (rel1, rel2) -> {
|
||||
return Integer.compare(rel1.getSeq(), rel2.getSeq());
|
||||
});
|
||||
}
|
||||
|
||||
private int fillPacketQueue(InetSocketAddress address) {
|
||||
|
||||
@@ -13,6 +13,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import network.protocol.Packet;
|
||||
import utilities.Signal;
|
||||
|
||||
public class Socket {
|
||||
private final DatagramSocket socket;
|
||||
@@ -20,6 +21,8 @@ public class Socket {
|
||||
private final Thread readThread;
|
||||
private final PacketPool packetPool;
|
||||
|
||||
public final Signal onClose = new Signal();
|
||||
|
||||
public Socket() throws SocketException {
|
||||
this.socket = new DatagramSocket();
|
||||
this.handlers = new ArrayList<>();
|
||||
@@ -86,6 +89,8 @@ public class Socket {
|
||||
public void close() {
|
||||
this.socket.close();
|
||||
this.packetPool.close();
|
||||
this.readThread.interrupt();
|
||||
this.onClose.emit();
|
||||
}
|
||||
|
||||
void handlePacket(Packet packet, InetSocketAddress address) {
|
||||
|
||||
Reference in New Issue
Block a user