WIP. Probably need to cherry pick stuff out of here.
Added ClientConnection and server-side handshake. Added MessageSerialiser and Deserialiser. #story[1095]
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package shared.exceptions;
|
||||
|
||||
/**
|
||||
* An exception thrown when a specific boat cannot be found.
|
||||
*/
|
||||
public class BoatNotFoundException extends Exception {
|
||||
|
||||
public BoatNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public BoatNotFoundException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package shared.exceptions;
|
||||
|
||||
/**
|
||||
* An exception thrown when we the client-server handshake fails.
|
||||
*/
|
||||
public class HandshakeException extends Exception {
|
||||
|
||||
/**
|
||||
* Constructs the exception with a given message.
|
||||
* @param message Message to store.
|
||||
*/
|
||||
public HandshakeException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs the exception with a given message and cause.
|
||||
* @param message Message to store.
|
||||
* @param cause Cause to store.
|
||||
*/
|
||||
public HandshakeException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package shared.model;
|
||||
|
||||
|
||||
import network.Exceptions.InvalidMessageException;
|
||||
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||
import network.Messages.AC35Data;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* This interface is a {@link Runnable} interface, with the ability to sleep until a given time period has elapsed.
|
||||
*/
|
||||
public interface RunnableWithFramePeriod extends Runnable {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Waits for enough time for the period of this frame to be greater than minimumFramePeriod.
|
||||
* @param previousFrameTime The timestamp of the previous frame.
|
||||
* @param currentFrameTime The timestamp of the current frame.
|
||||
* @param minimumFramePeriod The minimum period the frame must be.
|
||||
*/
|
||||
default void waitForFramePeriod(long previousFrameTime, long currentFrameTime, long minimumFramePeriod) {
|
||||
|
||||
|
||||
//This is the time elapsed, in milliseconds, since the last server "frame".
|
||||
long framePeriod = currentFrameTime - previousFrameTime;
|
||||
|
||||
//We only attempt to send packets every X milliseconds.
|
||||
if (framePeriod >= minimumFramePeriod) {
|
||||
return;
|
||||
|
||||
} else {
|
||||
//Wait until the frame period will be large enough.
|
||||
long timeToWait = minimumFramePeriod - framePeriod;
|
||||
|
||||
try {
|
||||
Thread.sleep(timeToWait);
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
//If we get interrupted, exit the function.
|
||||
Logger.getGlobal().log(Level.SEVERE, "RunnableWithFramePeriod.waitForFramePeriod().sleep(framePeriod) was interrupted on thread: " + Thread.currentThread(), e);
|
||||
//Re-set the interrupt flag.
|
||||
Thread.currentThread().interrupt();
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user