All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.javaclub.mq.common.netty.protocol.AckPubMsgProtocol Maven / Gradle / Ivy

The newest version!
package com.github.javaclub.mq.common.netty.protocol;

import io.netty.buffer.ByteBuf;

/**
 * broker向producer反馈消息pub情况
 */
public class AckPubMsgProtocol extends AbstractProtocol {
	private static byte code = CodeProtocol.ack_pub_msg;
	private byte ack; // 1: 成功, 0: 失败
	private byte msgIdLength; // msgid长度(byte)
	private byte[] msgid;
	
	public void encode(ByteBuf out) {
		super.setCode(code);
		super.encode(out);
		out.writeByte(ack);
		
		if(ack == 1){
			out.writeByte(msgIdLength);
			out.writeBytes(msgid);
		}
	}

	public int decode(ByteBuf in){
		int readindex = super.decode(in);
		if(readindex != -1){  
			if(in.readableBytes() >= 1){
				readindex = in.readerIndex();
				ack = in.readByte();
				if(ack == 1){
					if (in.readableBytes() >= 1) {
						readindex = in.readerIndex();
						byte 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 0;
			}else{
				in.readerIndex(readindex); // 恢复readindex
				return -1;
			}
		}
		return -1;
	}
	
	public byte getCode() {
		return code;
	}

	public void setCode(byte code) {
		AckPubMsgProtocol.code = code;
	}

	public byte getAck() {
		return ack;
	}

	public void setAck(byte ack) {
		this.ack = ack;
	}

	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;
	}

	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy