packet processing

main
Brett 2023-04-13 23:56:28 -04:00
parent cf0e6bd5c8
commit 88a52a6f74
1 changed files with 19 additions and 8 deletions

View File

@ -5,16 +5,14 @@ import ca.cosc3p91.a4.game.GameEngine;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.rmi.ServerException; import java.rmi.ServerException;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.Queue;
import java.util.Random;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
public class Server implements Runnable { public class Server implements Runnable {
public static final int SERVER_PORT = 42069; public static final int SERVER_PORT = 42069;
public static final int PACKET_SIZE = 4096; public static final int PACKET_SIZE = 4096;
public static final long MAX_PACKET_ACK_TIME_SECONDS = 30;
private final HashMap<Long, ConnectedClient> clients = new HashMap<>(); private final HashMap<Long, ConnectedClient> clients = new HashMap<>();
private long clientAssignmentID = 0; private long clientAssignmentID = 0;
@ -78,6 +76,7 @@ public class Server implements Runnable {
private final InetAddress address; private final InetAddress address;
private final int port; private final int port;
private final ArrayList<ServerRequest> requests = new ArrayList<>(); private final ArrayList<ServerRequest> requests = new ArrayList<>();
private final Queue<ServerRequest> pendingRequests = new PriorityQueue<>();
// could use read/write lock for some of this, as certain operations, mostly timeout check, won't modify data. // could use read/write lock for some of this, as certain operations, mostly timeout check, won't modify data.
private final ReentrantLock requestLock = new ReentrantLock(); private final ReentrantLock requestLock = new ReentrantLock();
private final DatagramSocket socket; private final DatagramSocket socket;
@ -96,18 +95,30 @@ public class Server implements Runnable {
public void handleRequest(ServerRequest request){ public void handleRequest(ServerRequest request){
requestLock.lock(); requestLock.lock();
requests.add(request); pendingRequests.add(request);
requestLock.unlock(); requestLock.unlock();
} }
private void processRequest(ServerRequest request){
}
public void run(){ public void run(){
while (running){ while (running){
requestLock.lock(); requestLock.lock();
requests.removeIf(ServerRequest::isAck); while (pendingRequests.size() > 0) {
for (ServerRequest request : requests){ ServerRequest request = pendingRequests.remove();
if (request.getTimeSinceReceived().) processRequest(request);
requests.add(request);
} }
requestLock.unlock(); requestLock.unlock();
requests.removeIf(ServerRequest::isAck);
for (ServerRequest request : requests){
// TODO:
if (request.getTimeSinceReceived().get() > MAX_PACKET_ACK_TIME_SECONDS)
System.out.println("A packet hasn't received a ack, it might have been lost!");
}
} }
} }