
com.github.javaclub.mq.common.netty.protocol.SubReportProtocol Maven / Gradle / Ivy
The newest version!
package com.github.javaclub.mq.common.netty.protocol;
import io.netty.buffer.ByteBuf;
/**
* 订阅者状态上报
*/
public class SubReportProtocol extends AbstractProtocol {
private static byte code = CodeProtocol.sub_report;
private int subId; // 订阅者id
private long ip; // 机器ip
private byte keyLength;
private byte[] subKey;
public void encode(ByteBuf out) {
super.setCode(code);
super.encode(out);
out.writeInt(subId);
out.writeLong(ip);
out.writeByte(keyLength);
out.writeBytes(subKey);
}
public int decode(ByteBuf in) {
int readindex = super.decode(in);
if (readindex != -1) {
if (in.readableBytes() >= 4 + 8 + 1) {
readindex = in.readerIndex();
subId = in.readInt();
ip = in.readLong();
keyLength = in.readByte();
if(in.readableBytes() >= keyLength){
subKey = new byte[keyLength];
in.readBytes(subKey);
return 0;
}else{
in.readerIndex(readindex); // 恢复readindex
return -1;
}
} else {
in.readerIndex(readindex); // 恢复readindex
return -1;
}
}
return -1;
}
public byte getCode() {
return code;
}
public void setCode(byte code) {
SubReportProtocol.code = code;
}
public int getSubId() {
return subId;
}
public void setSubId(int subId) {
this.subId = subId;
}
public long getIp() {
return ip;
}
public void setIp(long ip) {
this.ip = ip;
}
public byte getKeyLength() {
return keyLength;
}
public void setKeyLength(byte keyLength) {
this.keyLength = keyLength;
}
public byte[] getSubKey() {
return subKey;
}
public void setSubKey(byte[] subKey) {
this.subKey = subKey;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy