cn.teleinfo.idpointer.sdk.transport.MessagePackets Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of id-pointer-sdk Show documentation
Show all versions of id-pointer-sdk Show documentation
基于Java语言开发的工业互联网标识解析体系客户端软件开发工具包,应用通过集成 id-pointer-sdk,快速对接标识解析、标识注册、标识维护等功能服务。
The newest version!
package cn.teleinfo.idpointer.sdk.transport;
import cn.teleinfo.idpointer.sdk.core.Common;
import cn.teleinfo.idpointer.sdk.core.MessageEnvelope;
import io.netty.buffer.ByteBuf;
public class MessagePackets {
private int messageLength;
private int packetsNum;
private int lastPacketSize;
private byte[] messageBytes;
private boolean receiveCompleted = false;
private boolean[] packetsReceived;
private long createTimeMillis;
public MessagePackets(int messageLength) {
this(messageLength, Common.MAX_UDP_DATA_SIZE);
}
public MessagePackets(int messageLength, int dataSize) {
this.messageLength = messageLength;
this.packetsNum = messageLength / dataSize;
this.lastPacketSize = messageLength % dataSize;
if (this.lastPacketSize > 0) {
this.packetsNum++;
} else {
this.lastPacketSize = dataSize;
}
this.packetsReceived = new boolean[this.packetsNum];
this.messageBytes = new byte[messageLength];
this.createTimeMillis = System.currentTimeMillis();
}
public boolean isReceiveCompleted() {
if (!receiveCompleted) {
boolean haveAllPackets = true;
for (int i = 0; i < packetsReceived.length; i++) {
if (!packetsReceived[i]) {
haveAllPackets = false;
break;
}
}
receiveCompleted = haveAllPackets;
}
return receiveCompleted;
}
/**
* 接收 packet
*/
public void receivePacket(MessageEnvelope rcvEnvelope, byte[] dataArray) {
packetsReceived[rcvEnvelope.messageId] = true;
System.arraycopy(dataArray, 0, messageBytes, rcvEnvelope.messageId * Common.MAX_UDP_DATA_SIZE, dataArray.length);
}
public byte[] getMessageBytes() {
return messageBytes;
}
}