readd fucking time difference because git

main
Brett 2023-04-13 23:50:28 -04:00
parent d5bbf1a930
commit cf0e6bd5c8
2 changed files with 38 additions and 22 deletions

View File

@ -7,7 +7,9 @@ import java.net.*;
import java.rmi.ServerException; import java.rmi.ServerException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Queue;
import java.util.Random; import java.util.Random;
import java.util.concurrent.locks.ReentrantLock;
public class Server implements Runnable { public class Server implements Runnable {
@ -35,6 +37,7 @@ public class Server implements Runnable {
byte[] receiveData = new byte[PACKET_SIZE]; byte[] receiveData = new byte[PACKET_SIZE];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
try { try {
// BLOCKING!
socket.receive(receivePacket); socket.receive(receivePacket);
DataInputStream stream = new DataInputStream(new ByteArrayInputStream(receivePacket.getData())); DataInputStream stream = new DataInputStream(new ByteArrayInputStream(receivePacket.getData()));
@ -42,17 +45,21 @@ public class Server implements Runnable {
byte packetID = stream.readByte(); byte packetID = stream.readByte();
long clientID = stream.readLong(); long clientID = stream.readLong();
ConnectedClient client = clients.get(clientID);
if (packetID == PacketTable.CONNECT){ if (packetID == PacketTable.CONNECT){
clients.put(++clientAssignmentID, new ConnectedClient(socket, clientID, receivePacket.getAddress(), receivePacket.getPort())); clients.put(++clientAssignmentID, new ConnectedClient(socket, clientID, receivePacket.getAddress(), receivePacket.getPort()));
} else if (packetID == PacketTable.DISCONNECT){ } else if (packetID == PacketTable.DISCONNECT){
if (client == null)
throw new ServerException("Client disconnected with invalid client id! (" + clientID + ")");
client.halt();
clients.put(clientID, null); clients.put(clientID, null);
} else { } else {
ConnectedClient client = clients.get(clientID);
if (client == null) if (client == null)
throw new ServerException("Client Connected with invalid client id! (" + clientID + ")"); throw new ServerException("Client message with invalid client id! (" + clientID + ")");
client.handleRequest(new ConnectedClient.ServerRequest(packetID, stream)); client.handleRequest(new ConnectedClient.ServerRequest(packetID, stream));
} }
} catch (IOException e) { } catch (IOException | InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@ -65,42 +72,48 @@ public class Server implements Runnable {
public static void main(String[] args) throws IOException, InterruptedException { public static void main(String[] args) throws IOException, InterruptedException {
new Server(); new Server();
byte[] receiveData = new byte[1284];
byte[] sendData = new byte[1284];
stream_in = new ByteArrayInputStream(receiveData);
stream_out = new ByteArrayOutputStream(1284);
new Thread(mainEngine = new GameEngine()).start();
while(true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
stream_in.reset();
Thread.sleep(500);
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
sendData = stream_out.toByteArray();
stream_out.reset();
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
}
} }
private static class ConnectedClient { private static class ConnectedClient 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<>();
// 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 DatagramSocket socket; private final DatagramSocket socket;
private final long clientID; private final long clientID;
private volatile boolean running = true;
private final Thread processingThread;
public ConnectedClient(DatagramSocket socket, long clientID, InetAddress address, int port){ public ConnectedClient(DatagramSocket socket, long clientID, InetAddress address, int port){
this.socket = socket; this.socket = socket;
this.address = address; this.address = address;
this.port = port; this.port = port;
this.clientID = clientID; this.clientID = clientID;
processingThread = new Thread(this);
processingThread.start();
} }
public void handleRequest(ServerRequest request){ public void handleRequest(ServerRequest request){
requestLock.lock();
requests.add(request);
requestLock.unlock();
}
public void run(){
while (running){
requestLock.lock();
requests.removeIf(ServerRequest::isAck);
for (ServerRequest request : requests){
if (request.getTimeSinceReceived().)
}
requestLock.unlock();
}
}
public void halt() throws InterruptedException {
running = false;
processingThread.join();
} }
private static class ServerRequest { private static class ServerRequest {

View File

@ -47,4 +47,7 @@ public class Time implements Serializable {
return new Time(System.currentTimeMillis() / 1000); return new Time(System.currentTimeMillis() / 1000);
} }
public Time difference(Time right) {
return new Time(this.timeSeconds - right.timeSeconds);
}
} }