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

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

There is a newer version: 2.1.10
Show newest version
package com.github.javaclub.mq.common.netty.protocol;

import com.github.javaclub.mq.common.util.UUID;

import io.netty.buffer.ByteBuf;

public class AbstractProtocol {
	
	protected byte code; // 协议识别码,@see CodeProtocol
	protected int packetId; // 数据包id

	public int getPacketId() {
		return packetId;
	}

	public void setPacketId(int packetId) {
		this.packetId = packetId;
	}

	public void encode(ByteBuf out) {
		out.writeByte(code);
		packetId = packetId == 0 ? UUID.getPacketId():packetId;
		out.writeInt(packetId);
	}

	public int decode(ByteBuf in) {
		if (in.readableBytes() >= 4) {
			int readindex = in.readerIndex();
			packetId = in.readInt();
			return readindex;
		}
		return -1;
	}

	public byte getCode() {
		return code;
	}

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

	@Override
	public String toString() {
		return "AbstractProtocol [code=" + code + ", packetId=" + packetId + "]";
	}

	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy