mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 06:18:44 +00:00
Fixed buffer overflow in message header
- Fixed buffer overflow by adding a reset method that clears the buffer and sets the position to zero before re-writing the header Tags: #story[1047]
This commit is contained in:
@@ -238,61 +238,49 @@ public class GameServerThread implements Runnable, Observer, ClientConnectionDel
|
|||||||
serverLog("Couldn't send an XML Message: " + e.getMessage(), 0);
|
serverLog("Couldn't send an XML Message: " + e.getMessage(), 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},25000);
|
},1000);
|
||||||
//Delays the new course xml data for 25 seconds so the boats are able to pass the starting line
|
//Delays the new course xml data for 25 seconds so the boats are able to pass the starting line
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
ServerListenThread serverListenThread;
|
ServerListenThread serverListenThread;
|
||||||
|
Boolean serverIsSendingMessages = false;
|
||||||
|
|
||||||
try{
|
try{
|
||||||
server = ServerSocketChannel.open();
|
server = ServerSocketChannel.open();
|
||||||
server.socket().bind(new InetSocketAddress("localhost", PORT_NUMBER));
|
server.socket().bind(new InetSocketAddress("localhost", PORT_NUMBER));
|
||||||
// serverListenThread = new ServerListenThread(server, this);
|
serverListenThread = new ServerListenThread(server, this);
|
||||||
// serverListenThread.start();
|
serverListenThread.start();
|
||||||
}
|
}
|
||||||
catch (IOException e){
|
catch (IOException e){
|
||||||
serverLog("Failed to bind socket: " + e.getMessage(), 0);
|
serverLog("Failed to bind socket: " + e.getMessage(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
acceptConnection();
|
while (hosting) {
|
||||||
// acceptConnection();
|
try {
|
||||||
// acceptConnection();
|
Thread.sleep(100);
|
||||||
System.out.println("well, hit here now");
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
for (Player player : GameState.getPlayers()) {
|
if (GameState.getCurrentStage() == GameStages.RACING && !serverIsSendingMessages) {
|
||||||
System.out.println(player);
|
serverLog("Race Started", 0);
|
||||||
}
|
|
||||||
|
|
||||||
|
startSendingHeartbeats();
|
||||||
|
sendXml();
|
||||||
|
startSendingRaceStartStatusMessages();
|
||||||
|
//startSendingRaceStatusMessages();
|
||||||
|
sendPostStartCourseXml();
|
||||||
|
serverIsSendingMessages = true;
|
||||||
|
}
|
||||||
|
|
||||||
// while (hosting) {
|
else if (GameState.getCurrentStage() == GameStages.FINISHED) {
|
||||||
// try {
|
serverLog("Race Finished", 0);
|
||||||
// Thread.sleep(1000);
|
}
|
||||||
// } catch (InterruptedException e) {
|
|
||||||
// e.printStackTrace();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// if (GameState.getCurrentStage() == GameStages.RACING) {
|
|
||||||
// System.out.println("Racing");
|
|
||||||
// //startSendingHeartbeats();
|
|
||||||
// sendXml();
|
|
||||||
// //startSendingRaceStartStatusMessages();
|
|
||||||
// //startSendingRaceStatusMessages();
|
|
||||||
// //sendPostStartCourseXml();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// else if (GameState.getCurrentStage() == GameStages.FINISHED) {
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// startTime = System.currentTimeMillis() + TIME_TILL_RACE_START;
|
|
||||||
// }
|
|
||||||
|
|
||||||
startSendingHeartbeats();
|
startTime = System.currentTimeMillis() + TIME_TILL_RACE_START;
|
||||||
// sendXml();
|
|
||||||
startSendingRaceStartStatusMessages();
|
}
|
||||||
//startSendingRaceStatusMessages();
|
|
||||||
sendPostStartCourseXml();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**
|
// /**
|
||||||
@@ -319,6 +307,19 @@ public class GameServerThread implements Runnable, Observer, ClientConnectionDel
|
|||||||
// }, 0, BOAT_LOCATION_PERIOD);
|
// }, 0, BOAT_LOCATION_PERIOD);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A client has tried to connect to the server
|
||||||
|
* @param player The player that connected
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void clientConnected(Player player) {
|
||||||
|
if (GameState.getPlayers().size() < MAX_NUM_PLAYERS && GameState.getCurrentStage() == GameStages.LOBBYING) {
|
||||||
|
System.out.println("");
|
||||||
|
serverLog("Player Connected", 0);
|
||||||
|
GameState.addPlayer(player);
|
||||||
|
}
|
||||||
|
sendXml();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Listens for a connection and upon finding one, creates a Player object and adds it to the universal GameState
|
* Listens for a connection and upon finding one, creates a Player object and adds it to the universal GameState
|
||||||
@@ -349,13 +350,7 @@ public class GameServerThread implements Runnable, Observer, ClientConnectionDel
|
|||||||
seqNum++; // TODO: 11/07/17 Do we increment seqNum for every message or for the one message to everyone
|
seqNum++; // TODO: 11/07/17 Do we increment seqNum for every message or for the one message to everyone
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void clientConnected(Player player) {
|
|
||||||
if (GameState.getPlayers().size() < MAX_NUM_PLAYERS && GameState.getCurrentStage() == GameStages.LOBBYING) {
|
|
||||||
System.out.println("Hi");
|
|
||||||
GameState.addPlayer(player);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a boat location message when they are updated by the simulator
|
* Send a boat location message when they are updated by the simulator
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ public class Player {
|
|||||||
|
|
||||||
public Player(SocketChannel socketChannel) {
|
public Player(SocketChannel socketChannel) {
|
||||||
this.socketChannel = socketChannel;
|
this.socketChannel = socketChannel;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public SocketChannel getSocketChannel() {
|
public SocketChannel getSocketChannel() {
|
||||||
|
|||||||
@@ -43,10 +43,21 @@ public class Header {
|
|||||||
buff.position(buffPos);
|
buff.position(buffPos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reset the buffer
|
||||||
|
*/
|
||||||
|
public void reset(){
|
||||||
|
buffPos = 0;
|
||||||
|
buff.clear();
|
||||||
|
buff.position(buffPos);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return a ByteBuffer containing the message header
|
* @return a ByteBuffer containing the message header
|
||||||
*/
|
*/
|
||||||
public ByteBuffer getByteBuffer(){
|
public ByteBuffer getByteBuffer(){
|
||||||
|
reset();
|
||||||
|
|
||||||
putInBuffer(ByteBuffer.allocate(1).put((byte)syncByte1).array(), syncByte1);
|
putInBuffer(ByteBuffer.allocate(1).put((byte)syncByte1).array(), syncByte1);
|
||||||
|
|
||||||
putInBuffer(ByteBuffer.allocate(1).put((byte)syncByte2).array(), syncByte2);
|
putInBuffer(ByteBuffer.allocate(1).put((byte)syncByte2).array(), syncByte2);
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ public abstract class Message {
|
|||||||
buffer = ByteBuffer.allocate(Header.getSize() + getSize() + CRC_SIZE);
|
buffer = ByteBuffer.allocate(Header.getSize() + getSize() + CRC_SIZE);
|
||||||
buffer.order(ByteOrder.LITTLE_ENDIAN);
|
buffer.order(ByteOrder.LITTLE_ENDIAN);
|
||||||
bufferPosition = 0;
|
bufferPosition = 0;
|
||||||
|
buffer.position(bufferPosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user