network: process time out
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user