move networking into it's own package, add messages

main
Brett 2023-04-15 13:19:40 -04:00
parent d2200e6300
commit 3b6269958b
7 changed files with 85 additions and 22 deletions

View File

@ -1,6 +1,6 @@
package ca.cosc3p91.a4;
import ca.cosc3p91.a4.util.Client;
import ca.cosc3p91.a4.util.network.Client;
import java.io.*;
public class Main {

View File

@ -15,8 +15,8 @@ public class GameDisplay {
private BufferedReader reader;
private String input;
public GameDisplay(InputStream readFrom) {
reader = new BufferedReader(new InputStreamReader(readFrom));
public GameDisplay() {
reader = new BufferedReader(new InputStreamReader(System.in));
}
public String nextInput() throws IOException {

View File

@ -1,16 +0,0 @@
package ca.cosc3p91.a4.util;
public class PacketTable {
// packetID -> byte defined in this file
// clientID -> long
// messageID -> long
// packetID, clientID (0 if connecting to server)
public static final byte CONNECT = 0x1;
// packetID, clientID
public static final byte DISCONNECT = 0x2;
// packetID, clientID, messageID
public static final byte ACK = 0x3;
}

View File

@ -1,4 +1,4 @@
package ca.cosc3p91.a4.util;
package ca.cosc3p91.a4.util.network;
import ca.cosc3p91.a4.userinterface.GameDisplay;
@ -8,7 +8,7 @@ import java.net.DatagramSocket;
import java.net.InetAddress;
public class Client {
GameDisplay view = new GameDisplay(System.in);
GameDisplay view = new GameDisplay();
public Client(int port) throws IOException {
DatagramSocket clientSocket = new DatagramSocket();

View File

@ -0,0 +1,57 @@
package ca.cosc3p91.a4.util.network;
import java.io.DataInputStream;
import java.io.DataOutputStream;
public class Message {
private final byte packetID;
private final long clientID, messageID;
public Message(byte packetID, long clientID, long messageID){
this.packetID = packetID;
this.clientID = clientID;
this.messageID = messageID;
}
public byte getPacketID() {
return packetID;
}
public long getClientID() {
return clientID;
}
public long getMessageID() {
return messageID;
}
public static class ReceivedMessage extends Message {
private final DataInputStream reader;
public ReceivedMessage(byte packetID, long clientID, long messageID, DataInputStream reader) {
super(packetID, clientID, messageID);
this.reader = reader;
}
public DataInputStream getReader(){
return reader;
}
}
public static class SentMessage extends Message {
private final DataOutputStream writer;
public SentMessage(byte packetID, long clientID, long messageID, DataOutputStream writer) {
super(packetID, clientID, messageID);
this.writer = writer;
}
public DataOutputStream getWriter(){
return writer;
}
}
}

View File

@ -0,0 +1,20 @@
package ca.cosc3p91.a4.util.network;
public class PacketTable {
// packetID -> byte defined in this file
// clientID -> long
// messageID -> long
// messageHeader (packetID, clientID, messageID)
// messageHeader, (clientID = 0 if connecting to server)
public static final byte CONNECT = 0x1;
// messageHeader
public static final byte DISCONNECT = 0x2;
// messageHeader
public static final byte ACK = 0x3;
// messageHeader, UTF8 String with length information (use DOS.writeUTF/DIS.readUTF)
public static final byte MESSAGE = 0x4;
}

View File

@ -1,6 +1,7 @@
package ca.cosc3p91.a4.util;
package ca.cosc3p91.a4.util.network;
import ca.cosc3p91.a4.game.GameEngine;
import ca.cosc3p91.a4.util.Time;
import java.io.*;
import java.net.*;
@ -139,6 +140,7 @@ public class Server implements Runnable {
private final byte id;
private final Time receiveTime;
private final DataInputStream receive;
// ack should be on messages send to the client, which the client acks!
private boolean ack = false;
public ServerRequest(byte id, DataInputStream receive){