|
|
|
@ -243,9 +243,9 @@ public class ProtocolP2PDatagram {
|
|
|
|
|
this.hostR = hostR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int computeCheckSum(byte [] datagram){
|
|
|
|
|
private int computeCheckSum(byte [] datagram) throws SizeError {
|
|
|
|
|
int checksum = 0;
|
|
|
|
|
for (int i=CHECKSUM_POSITION+2; i<datagram.length; ++i){
|
|
|
|
|
for (int i=CHECKSUM_POSITION+2; i<CHECKSUM_POSITION+2+4+Payload.getPayloadSize(datagram); ++i){
|
|
|
|
|
checksum += BytesArrayTools.readInt16Bits(datagram,i);
|
|
|
|
|
checksum &= 0xffff;
|
|
|
|
|
}
|
|
|
|
@ -253,8 +253,8 @@ public class ProtocolP2PDatagram {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setCheckSum(byte [] datagram) throws InternalError {
|
|
|
|
|
int checksum = computeCheckSum(datagram);
|
|
|
|
|
try {
|
|
|
|
|
int checksum = computeCheckSum(datagram);
|
|
|
|
|
BytesArrayTools.write16Bits(datagram,CHECKSUM_POSITION,checksum);
|
|
|
|
|
} catch (SizeError e) {
|
|
|
|
|
throw new InternalError();
|
|
|
|
@ -262,10 +262,14 @@ public class ProtocolP2PDatagram {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void checkCheckSum(byte [] datagram) throws TransmissionError {
|
|
|
|
|
try {
|
|
|
|
|
int checksum = BytesArrayTools.readInt16Bits(datagram,CHECKSUM_POSITION);
|
|
|
|
|
int computedCheckSum = computeCheckSum(datagram);
|
|
|
|
|
if (computedCheckSum != checksum){
|
|
|
|
|
throw new TransmissionError();
|
|
|
|
|
}
|
|
|
|
|
} catch(SizeError e) {
|
|
|
|
|
throw new TransmissionError();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|