
com.github.javaclub.mq.common.netty.protocol.AckSubMsgProtocol Maven / Gradle / Ivy
The newest version!
package com.github.javaclub.mq.common.netty.protocol;
import io.netty.buffer.ByteBuf;
/**
* customer向broker发送消息ack
*/
public class AckSubMsgProtocol extends AbstractProtocol{
private static byte code = CodeProtocol.ack_sub_msg;
private byte ack; // 1: 成功, 0 : 失败
private int subId; // 订阅者
private byte msgIdLength; // msgid长度(byte)
private byte[] msgid;
public void encode(ByteBuf out) {
super.setCode(code);
super.encode(out);
out.writeByte(ack);
out.writeInt(subId);
out.writeByte(msgIdLength);
out.writeBytes(msgid);
}
public int decode(ByteBuf in){
int readindex = super.decode(in);
if(readindex != -1){
if(in.readableBytes() >= 1 + 4 + 1){
readindex = in.readerIndex();
ack = in.readByte();
subId = in.readInt();
msgIdLength = in.readByte();
if(in.readableBytes() >= msgIdLength){
msgid = new byte[msgIdLength];
in.readBytes(msgid);
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) {
AckSubMsgProtocol.code = code;
}
public byte getMsgIdLength() {
return msgIdLength;
}
public void setMsgIdLength(byte msgIdLength) {
this.msgIdLength = msgIdLength;
}
public byte[] getMsgid() {
return msgid;
}
public void setMsgid(byte[] msgid) {
this.msgid = msgid;
}
public int getSubId() {
return subId;
}
public void setSubId(int subId) {
this.subId = subId;
}
public byte getAck() {
return ack;
}
public void setAck(byte ack) {
this.ack = ack;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy