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

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

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

import io.netty.buffer.ByteBuf;

/**
 * 发送方身份上报
 */
public class PubReportProtocol extends AbstractProtocol {
	
	private static byte code = CodeProtocol.pub_report; 
	private int pubId; // 发送者id
	private long ip; // 机器ip
	private byte keyLength;
	private byte[] pubKey;
	
	public void encode(ByteBuf out) {
		super.setCode(code);
		super.encode(out);
		out.writeInt(pubId);
		out.writeLong(ip);
		out.writeByte(keyLength);
		out.writeBytes(pubKey);
	}

	public int decode(ByteBuf in){
		int readindex = super.decode(in);
		if(readindex != -1){  
			if(in.readableBytes() >= 4 + 8 + 1){
				readindex = in.readerIndex();
				pubId = in.readInt();
				ip = in.readLong();
				keyLength = in.readByte();
				if(in.readableBytes() >= keyLength){
					pubKey = new byte[keyLength];
					in.readBytes(pubKey);
					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) {
		PubReportProtocol.code = code;
	}

	
	public int getPubId() {
		return pubId;
	}

	public void setPubId(int pubId) {
		this.pubId = pubId;
	}

	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[] getPubKey() {
		return pubKey;
	}

	public void setPubKey(byte[] pubKey) {
		this.pubKey = pubKey;
	}
	
	

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy