network: process time out
This commit is contained in:
@@ -20,9 +20,14 @@ public class ClientConnexion implements PacketVisitor, PacketHandler {
|
|||||||
this.serverAddress = serverAddress;
|
this.serverAddress = serverAddress;
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
|
this.socket.onClose.connect(this::onSocketClose);
|
||||||
this.socket.addHandler(this);
|
this.socket.addHandler(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void onSocketClose() {
|
||||||
|
this.callback.handleConnexionError();
|
||||||
|
}
|
||||||
|
|
||||||
public void close() {
|
public void close() {
|
||||||
this.socket.close();
|
this.socket.close();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -134,6 +134,7 @@ public class ClientConsole implements ClientListener {
|
|||||||
public void handleDisconnect() {
|
public void handleDisconnect() {
|
||||||
System.out.println("Disconnected !");
|
System.out.println("Disconnected !");
|
||||||
stop();
|
stop();
|
||||||
|
this.onDisconnect.emit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -23,8 +23,9 @@ public class PacketPool {
|
|||||||
private final Socket socket;
|
private final Socket socket;
|
||||||
|
|
||||||
private static int MAX_SEND_TRY = 50;
|
private static int MAX_SEND_TRY = 50;
|
||||||
private static long RETRY_INTERVAL = 100;
|
private static long SEND_DELAY = 100;
|
||||||
private static float PACKET_LOSS_PROBABILITY = 0.75f;
|
private static long RETRY_INTERVAL = SEND_DELAY * 2;
|
||||||
|
private static float PACKET_LOSS_PROBABILITY = 0.5f;
|
||||||
|
|
||||||
private static record ReliablePacketAddress(ReliablePacket packet, InetSocketAddress address) {
|
private static record ReliablePacketAddress(ReliablePacket packet, InetSocketAddress address) {
|
||||||
@Override
|
@Override
|
||||||
@@ -90,8 +91,15 @@ public class PacketPool {
|
|||||||
private void sendPacketToSocket(ReliablePacket packet, InetSocketAddress address) throws IOException {
|
private void sendPacketToSocket(ReliablePacket packet, InetSocketAddress address) throws IOException {
|
||||||
var packetsSentTries = this.addressContexts.get(address).packetsSentTries;
|
var packetsSentTries = this.addressContexts.get(address).packetsSentTries;
|
||||||
|
|
||||||
if (Math.random() > PACKET_LOSS_PROBABILITY)
|
new Thread(() -> {
|
||||||
this.socket.sendPacket(packet, address);
|
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)
|
if (packet.getPacket() instanceof AcknowlegdementPacket)
|
||||||
return;
|
return;
|
||||||
@@ -135,7 +143,8 @@ public class PacketPool {
|
|||||||
Integer sendCount = packetsSentTries.get(reliablePacketAddress.packet());
|
Integer sendCount = packetsSentTries.get(reliablePacketAddress.packet());
|
||||||
if (sendCount > MAX_SEND_TRY) {
|
if (sendCount > MAX_SEND_TRY) {
|
||||||
close(reliablePacketAddress.address());
|
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
|
// simulating a fake disconnect packet
|
||||||
this.socket.handlePacket(new DisconnectPacket("Timed out"), reliablePacketAddress.address());
|
this.socket.handlePacket(new DisconnectPacket("Timed out"), reliablePacketAddress.address());
|
||||||
break;
|
break;
|
||||||
@@ -161,7 +170,9 @@ public class PacketPool {
|
|||||||
List<ReliablePacket> packetRecvBuffer = this.addressContexts.get(address).packetRecvBuffer;
|
List<ReliablePacket> packetRecvBuffer = this.addressContexts.get(address).packetRecvBuffer;
|
||||||
if (packetRecvBuffer.isEmpty())
|
if (packetRecvBuffer.isEmpty())
|
||||||
return null;
|
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) {
|
private int fillPacketQueue(InetSocketAddress address) {
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import network.protocol.Packet;
|
import network.protocol.Packet;
|
||||||
|
import utilities.Signal;
|
||||||
|
|
||||||
public class Socket {
|
public class Socket {
|
||||||
private final DatagramSocket socket;
|
private final DatagramSocket socket;
|
||||||
@@ -20,6 +21,8 @@ public class Socket {
|
|||||||
private final Thread readThread;
|
private final Thread readThread;
|
||||||
private final PacketPool packetPool;
|
private final PacketPool packetPool;
|
||||||
|
|
||||||
|
public final Signal onClose = new Signal();
|
||||||
|
|
||||||
public Socket() throws SocketException {
|
public Socket() throws SocketException {
|
||||||
this.socket = new DatagramSocket();
|
this.socket = new DatagramSocket();
|
||||||
this.handlers = new ArrayList<>();
|
this.handlers = new ArrayList<>();
|
||||||
@@ -86,6 +89,8 @@ public class Socket {
|
|||||||
public void close() {
|
public void close() {
|
||||||
this.socket.close();
|
this.socket.close();
|
||||||
this.packetPool.close();
|
this.packetPool.close();
|
||||||
|
this.readThread.interrupt();
|
||||||
|
this.onClose.emit();
|
||||||
}
|
}
|
||||||
|
|
||||||
void handlePacket(Packet packet, InetSocketAddress address) {
|
void handlePacket(Packet packet, InetSocketAddress address) {
|
||||||
|
|||||||
Reference in New Issue
Block a user