forked from KNEMC/KLALB
优化代码,性能暴涨
This commit is contained in:
@@ -1,26 +1,25 @@
|
||||
package org.kne.cloud.network.klalb;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutput;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.EOFException;
|
||||
import java.io.Externalizable;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInput;
|
||||
import java.io.ObjectOutput;
|
||||
import java.io.StreamCorruptedException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.channels.WritableByteChannel;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public abstract class KLALBPacket implements Comparable<KLALBPacket>{
|
||||
import org.kne.cloud.network.NetworkPacket;
|
||||
import org.kne.cloud.network.ipv6.IPv6Packet;
|
||||
|
||||
public abstract class KLALBPacket extends IPv6Packet.IPv6Payload {
|
||||
public static final int KLALB_PROTOCOL_NUMBER=254;
|
||||
|
||||
|
||||
public static final int PING=0;
|
||||
public static final int PONG=1;
|
||||
public static final int BWINF=2;
|
||||
@@ -36,139 +35,112 @@ public abstract class KLALBPacket implements Comparable<KLALBPacket>{
|
||||
public static final int TEST=11;
|
||||
public static final int VADDRACK=12;
|
||||
public static final int VADDRREQ=13;
|
||||
|
||||
public static final int IPV6OVERKLALB=14;
|
||||
public static final int ADDR=15;
|
||||
public static final int ADDRREQ=16;
|
||||
|
||||
private static final int HEADER_CAPACITY = 32;
|
||||
public static final ByteBufferPool headerbufferpool=new ByteBufferPool(1000, HEADER_CAPACITY);
|
||||
public static final ByteBufferPool databufferpool=new ByteBufferPool(1000, 65535);
|
||||
//private static final int HEADER_CAPACITY = 32;
|
||||
|
||||
private int headerLength=1;
|
||||
|
||||
//public static final ByteArrayPool dataarraypool=new ByteArrayPool(5000, 8192);
|
||||
|
||||
protected volatile ByteBuffer header;
|
||||
protected volatile ByteBuffer klalbHeader;
|
||||
|
||||
|
||||
|
||||
protected KLALBPacket(ByteBuffer header) {
|
||||
super();
|
||||
this.header = header;
|
||||
protected KLALBPacket(ByteBuffer klalbHeader,int headerLength) {
|
||||
super(KLALB_PROTOCOL_NUMBER,false);
|
||||
this.klalbHeader = klalbHeader;
|
||||
this.headerLength=headerLength;
|
||||
}
|
||||
|
||||
public KLALBPacket(int type) {
|
||||
super();
|
||||
header=headerbufferpool.borrow();
|
||||
header.put((byte) type);
|
||||
public KLALBPacket(int type,int headerLength) {
|
||||
super(KLALB_PROTOCOL_NUMBER,false);
|
||||
klalbHeader=ByteBuffer.allocate(headerLength);
|
||||
klalbHeader.put((byte) type);
|
||||
this.headerLength=headerLength;
|
||||
}
|
||||
public KLALBPacket(int type, long priority) {
|
||||
this(type);
|
||||
this.priority=priority;
|
||||
public KLALBPacket(int type,int headerLength, long priority) {
|
||||
this(type,headerLength);
|
||||
setPriority(priority);
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "KLALBPacket [type=" + getType() + "]";
|
||||
}
|
||||
public int getType() {
|
||||
return header.get(0)&0xff;
|
||||
return klalbHeader.get(0)&0xff;
|
||||
}
|
||||
|
||||
private long sndtime,rcvtime;
|
||||
|
||||
private long joinqueuetime;
|
||||
|
||||
|
||||
protected void writeToChannel(WritableByteChannel dto) throws IOException {
|
||||
sndtime=System.nanoTime();
|
||||
dto.write(header.slice(0, getHeaderSize()));
|
||||
public void markJoinqueuetime() {
|
||||
joinqueuetime=System.nanoTime();
|
||||
}
|
||||
|
||||
protected void readFromChannel(ReadableByteChannel din) throws IOException {
|
||||
rcvtime=System.nanoTime();
|
||||
header.limit(getHeaderSize());
|
||||
while(header.hasRemaining()){
|
||||
if(din.read(header)==-1) {
|
||||
public long getJoinqueuetime() {
|
||||
return joinqueuetime;
|
||||
}
|
||||
|
||||
protected void writeToChannel(WritableByteChannel dto) throws IOException {
|
||||
sndtime=System.nanoTime();
|
||||
dto.write(klalbHeader.slice(0, headerLength));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean needEndPosition() {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void readFromChannel(ReadableByteChannel din,long length) throws IOException {
|
||||
klalbHeader.limit(headerLength);
|
||||
while(klalbHeader.hasRemaining()){
|
||||
if(din.read(klalbHeader)==-1) {
|
||||
throw new EOFException();
|
||||
}
|
||||
}
|
||||
rcvtime=System.nanoTime();
|
||||
}
|
||||
|
||||
protected int getHeaderSize() {
|
||||
return 1;
|
||||
}
|
||||
public long getSndtime() {
|
||||
return sndtime;
|
||||
}
|
||||
public long getRcvtime() {
|
||||
return rcvtime;
|
||||
}
|
||||
public long getLength() {
|
||||
return getHeaderSize();
|
||||
public long getLength() {//缓冲区limit,实际长度
|
||||
return headerLength;
|
||||
}
|
||||
|
||||
public long getPriority() {
|
||||
return priority;
|
||||
}
|
||||
public void setPriority(long priority) {
|
||||
this.priority = priority;
|
||||
}
|
||||
public long getSendseq() {
|
||||
return sendseq;
|
||||
}
|
||||
|
||||
|
||||
//private List<KLALBRemoteLine> sendRecord=new ArrayList<>(2);
|
||||
|
||||
private AtomicInteger sendCounter=new AtomicInteger(0);
|
||||
|
||||
private long priority;
|
||||
private long sendseq;
|
||||
private static final AtomicLong seqgen=new AtomicLong();
|
||||
|
||||
@Override
|
||||
public int compareTo(KLALBPacket o) {
|
||||
if(priority>o.priority) {
|
||||
return 1;
|
||||
}else if(priority<o.priority){
|
||||
return -1;
|
||||
}else {
|
||||
if(sendseq>o.sendseq) {
|
||||
return 1;
|
||||
}else if(sendseq<o.sendseq){
|
||||
return -1;
|
||||
}else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
public int getSendCounter() {
|
||||
return sendCounter.get();
|
||||
}
|
||||
|
||||
|
||||
protected void genseq() {
|
||||
this.sendseq=seqgen.getAndIncrement();
|
||||
public void incSendCounter() {
|
||||
sendCounter.incrementAndGet();
|
||||
}
|
||||
|
||||
private List<KLALBRemoteLine> sendRecord=new ArrayList<>(2);
|
||||
private volatile boolean disposed;
|
||||
private volatile ReentrantLock disposeLock=new ReentrantLock();
|
||||
public boolean isDisposed() {
|
||||
return disposed;
|
||||
}
|
||||
|
||||
public List<KLALBRemoteLine> getSendRecord() {
|
||||
/*public List<KLALBRemoteLine> getSendRecord() {
|
||||
return sendRecord;
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
|
||||
public ReentrantLock getDisposeLock() {
|
||||
return disposeLock;
|
||||
}
|
||||
|
||||
protected ByteBuffer getHeader() {
|
||||
return header;
|
||||
protected ByteBuffer getKLALBHeader() {
|
||||
return klalbHeader;
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
disposeLock.lock();
|
||||
try {
|
||||
disposed=true;
|
||||
}finally {
|
||||
disposeLock.unlock();
|
||||
}
|
||||
ByteBuffer headerx=header;
|
||||
header=null;
|
||||
KLALBPacket.headerbufferpool.back(headerx);
|
||||
super.dispose();
|
||||
/* ByteBuffer datax=klalbHeader;
|
||||
klalbHeader=null;
|
||||
if(datax!=null)
|
||||
KLALBPacket.databufferpool_40.back(datax);*/
|
||||
}
|
||||
|
||||
public static KLALBPacket readKLALBPacketFromStream(DataInputStream in) throws IOException {
|
||||
@@ -176,7 +148,8 @@ public abstract class KLALBPacket implements Comparable<KLALBPacket>{
|
||||
}
|
||||
|
||||
public static KLALBPacket readKLALBPacketFromChannel(ReadableByteChannel in) throws IOException {
|
||||
ByteBuffer bb=headerbufferpool.borrow();
|
||||
while(true) {
|
||||
ByteBuffer bb=databufferpool_40.borrow();
|
||||
bb.limit(1);
|
||||
while(bb.hasRemaining()){
|
||||
if(in.read(bb)==-1) {
|
||||
@@ -235,8 +208,22 @@ public abstract class KLALBPacket implements Comparable<KLALBPacket>{
|
||||
klp=new VADDRREQPacket(bb);
|
||||
klp.readFromChannel(in);
|
||||
return klp;
|
||||
case IPV6OVERKLALB:
|
||||
klp=new IPv6OverKLALBPacket(bb);
|
||||
klp.readFromChannel(in);
|
||||
return klp;
|
||||
case ADDR:
|
||||
klp=new ADDRPacket(bb);
|
||||
klp.readFromChannel(in);
|
||||
return klp;
|
||||
case ADDRREQ:
|
||||
klp=new ADDRREQPacket(bb);
|
||||
klp.readFromChannel(in);
|
||||
return klp;
|
||||
}
|
||||
throw new StreamCorruptedException("unknown package type:"+type);
|
||||
//throw new StreamCorruptedException("unknown package type:"+type);
|
||||
System.err.println("ignore unknown KLALBPacket type:"+type);
|
||||
}
|
||||
}
|
||||
|
||||
public static void writeKLALBPacketToStream(DataOutputStream out,KLALBPacket klb) throws IOException {
|
||||
@@ -245,4 +232,20 @@ public abstract class KLALBPacket implements Comparable<KLALBPacket>{
|
||||
public static void writeKLALBPacketToChannel(WritableByteChannel writableByteChannel,KLALBPacket klb) throws IOException {
|
||||
klb.writeToChannel(writableByteChannel);
|
||||
}
|
||||
|
||||
public static KLALBPacket createByBuffer(ByteBuffer data) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void doDisposeAfterSend() {
|
||||
if(isDisposeAfterSend())
|
||||
dispose();
|
||||
}
|
||||
private boolean ce=false;
|
||||
public void setCE(boolean ce) {
|
||||
this.ce=ce;
|
||||
}
|
||||
public boolean isCE() {
|
||||
return ce;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user