You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.8 KiB
88 lines
2.8 KiB
package seng302.Networking;
|
|
|
|
|
|
import seng302.Networking.Utils.MessageType;
|
|
|
|
import java.nio.ByteBuffer;
|
|
import java.util.Arrays;
|
|
import java.util.zip.CRC32;
|
|
|
|
/**
|
|
* Created by hba56 on 21/04/17.
|
|
*/
|
|
public class BinaryMesageEncoder {
|
|
private byte[] fullMessage;
|
|
private byte[] header;
|
|
private byte[] message;
|
|
//private byte[] crc;
|
|
|
|
private byte headerSync1 = (byte) 0x47;
|
|
private byte headerSync2 = (byte) 0x83;
|
|
private byte headerMessageType;
|
|
private long headerTimeStamp;
|
|
private int headerSourceID;
|
|
private short headerMessageLength;
|
|
|
|
public BinaryMesageEncoder(MessageType headerMessageType, long headerTimeStamp, int headerSourceID, short headerMessageLength, byte[] message) {
|
|
//set the header
|
|
this.headerMessageType = headerMessageType.getValue();
|
|
this.headerTimeStamp = headerTimeStamp;
|
|
this.headerSourceID = headerSourceID;
|
|
this.headerMessageLength = headerMessageLength;
|
|
|
|
ByteBuffer tempHeaderByteBuffer = ByteBuffer.allocate(15);
|
|
tempHeaderByteBuffer.put(this.headerSync1);
|
|
tempHeaderByteBuffer.put(this.headerSync2);
|
|
tempHeaderByteBuffer.put(this.headerMessageType);
|
|
tempHeaderByteBuffer.put(longToSixBytes(this.headerTimeStamp));
|
|
tempHeaderByteBuffer.putInt(this.headerSourceID);
|
|
tempHeaderByteBuffer.put(shortToBytes(this.headerMessageLength));
|
|
|
|
|
|
this.header = tempHeaderByteBuffer.array();
|
|
|
|
//set the message
|
|
this.message = message;
|
|
|
|
//set full message
|
|
ByteBuffer tempMessageByteBuffer = ByteBuffer.allocate(19 + this.headerMessageLength);
|
|
tempMessageByteBuffer.put(this.header);
|
|
tempMessageByteBuffer.put(this.message);
|
|
|
|
CRC32 crc = new CRC32();
|
|
crc.reset();
|
|
byte[] messageAndHeader = new byte[this.header.length + this.message.length];
|
|
System.arraycopy(this.header, 0, messageAndHeader, 0, this.header.length);
|
|
System.arraycopy(this.message, 0, messageAndHeader, this.header.length, this.message.length);
|
|
crc.update(messageAndHeader);
|
|
//System.out.println(Arrays.toString(messageAndHeader));
|
|
|
|
tempMessageByteBuffer.put(intToBytes((int) crc.getValue()));
|
|
this.fullMessage = tempMessageByteBuffer.array();
|
|
|
|
|
|
}
|
|
|
|
private byte[] longToSixBytes(long x) {
|
|
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
|
|
buffer.putLong(x);
|
|
return Arrays.copyOfRange(buffer.array(), 2, 8);
|
|
}
|
|
|
|
private byte[] shortToBytes(short x) {
|
|
ByteBuffer buffer = ByteBuffer.allocate(Short.BYTES);
|
|
buffer.putShort(x);
|
|
return buffer.array();
|
|
}
|
|
|
|
private byte[] intToBytes(int x) {
|
|
ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
|
|
buffer.putInt(x);
|
|
return buffer.array();
|
|
}
|
|
|
|
public byte[] getFullMessage() {
|
|
return fullMessage;
|
|
}
|
|
}
|