Host also can be it's own client.

The host can connect to itself to become a client, packets are also sending from the host to client, update method should be ready to fully implemented. Added chatter packets to packet types to be used mostly for testing but can be further implemented for proper use in the future.

#story[1055]
This commit is contained in:
Kusal Ekanayake
2017-07-20 13:53:53 +12:00
parent 1e80d76acd
commit b1575e57df
10 changed files with 82 additions and 14 deletions
@@ -5,6 +5,8 @@ import seng302.models.Player;
import seng302.models.stream.PacketBufferDelegate;
import seng302.models.stream.StreamParser;
import seng302.models.stream.packets.StreamPacket;
import seng302.server.messages.ChatterMessage;
import seng302.server.messages.Heartbeat;
import seng302.server.messages.Message;
import java.io.*;
@@ -54,6 +56,9 @@ public class ServerToClientThread extends Thread {
//Perform a write if it is time to as delegated by the MainServerThread
if (updateClient) {
// TODO: 13/07/17 wmu16 - Write out game state - some function that would write all appropriate messages to this output stream
ChatterMessage chatterMessage = new ChatterMessage(4, 14, "Hello, it's me");
sendMessage(chatterMessage);
// try {
// GameState.outputState(os);
// } catch (IOException e) {
@@ -62,9 +67,11 @@ public class ServerToClientThread extends Thread {
updateClient = false;
}
crcBuffer = new ByteArrayOutputStream();
sync1 = readByte();
sync2 = readByte();
//checking if it is the start of the packet
if(sync1 == 0x47 && sync2 == 0x83) {
int type = readByte();
@@ -86,6 +93,7 @@ public class ServerToClientThread extends Thread {
}
}
} catch (Exception e) {
e.printStackTrace();
closeSocket();
return;
}
@@ -162,4 +170,12 @@ public class ServerToClientThread extends Thread {
readByte();
}
}
public void sendMessage(Message message){
try {
os.write(message.getBuffer());
} catch (IOException e) {
e.printStackTrace();
}
}
}