Commit d50e9720 by zhangxk

事件、流量增加CRC校验

1 parent d54a7a66
...@@ -9,6 +9,8 @@ import io.netty.buffer.ByteBufUtil; ...@@ -9,6 +9,8 @@ import io.netty.buffer.ByteBufUtil;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.util.zip.CRC32;
/** /**
* . * .
* *
...@@ -100,8 +102,24 @@ public class BehaviorModel extends BaseModel { ...@@ -100,8 +102,24 @@ public class BehaviorModel extends BaseModel {
buffer.writeBytes(reservedField2); buffer.writeBytes(reservedField2);
buffer.writeBytes(picture); buffer.writeBytes(picture);
this.data = new byte[buffer.readableBytes()]; byte[] bytes = new byte[buffer.readableBytes()];
buffer.readBytes(this.data); buffer.readBytes(bytes);
buffer.release(); buffer.release();
int count = bytes.length;
// crc32
CRC32 crc32 = new CRC32();
crc32.update(bytes);
int value = (int) crc32.getValue();
for (int i = 0; i < count; i++) {
bytes[i] = (byte) (bytes[i] ^ 0x94);
}
this.data = new byte[count + 4];
System.arraycopy(bytes, 0, data, 0, count);
this.data[count++] = (byte) value;
this.data[count++] = (byte) (value >> 8);
this.data[count++] = (byte) (value >> 16);
this.data[count] = (byte) (value >> 24);
} }
} }
...@@ -6,6 +6,8 @@ import io.netty.buffer.ByteBufAllocator; ...@@ -6,6 +6,8 @@ import io.netty.buffer.ByteBufAllocator;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.util.zip.CRC32;
/** /**
* . * .
* *
...@@ -62,8 +64,24 @@ public class FlowModel extends BaseModel { ...@@ -62,8 +64,24 @@ public class FlowModel extends BaseModel {
buffer.writeIntLE(i); buffer.writeIntLE(i);
} }
buffer.writeBytes(reservedField); buffer.writeBytes(reservedField);
this.data = new byte[buffer.readableBytes()]; byte[] bytes = new byte[buffer.readableBytes()];
buffer.readBytes(this.data); buffer.readBytes(bytes);
buffer.release(); buffer.release();
int count = bytes.length;
// crc32
CRC32 crc32 = new CRC32();
crc32.update(bytes);
int value = (int) crc32.getValue();
for (int i = 0; i < count; i++) {
bytes[i] = (byte) (bytes[i] ^ 0x94);
}
this.data = new byte[count + 4];
System.arraycopy(bytes, 0, data, 0, count);
this.data[count++] = (byte) value;
this.data[count++] = (byte) (value >> 8);
this.data[count++] = (byte) (value >> 16);
this.data[count] = (byte) (value >> 24);
} }
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!