forked from KNEMC/KLALB
KLALB2.3
This commit is contained in:
@@ -1,18 +1,31 @@
|
||||
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.AtomicLong;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public abstract class KLALBPacket implements Sumable{
|
||||
public abstract class KLALBPacket implements Comparable<KLALBPacket>{
|
||||
public static final int PING=0;
|
||||
public static final int PONG=1;
|
||||
public static final int SYNT=2;
|
||||
public static final int SACKT=3;
|
||||
public static final int BWINF=2;
|
||||
//public static final int SYNT=2;
|
||||
//public static final int SACKT=3;
|
||||
public static final int RST=4;
|
||||
public static final int DATAT=5;
|
||||
public static final int ACKT=6;
|
||||
@@ -23,26 +36,61 @@ public abstract class KLALBPacket implements Sumable{
|
||||
public static final int TEST=11;
|
||||
public static final int VADDRACK=12;
|
||||
public static final int VADDRREQ=13;
|
||||
private int type;
|
||||
|
||||
|
||||
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);
|
||||
|
||||
//public static final ByteArrayPool dataarraypool=new ByteArrayPool(5000, 8192);
|
||||
|
||||
protected volatile ByteBuffer header;
|
||||
|
||||
|
||||
|
||||
protected KLALBPacket(ByteBuffer header) {
|
||||
super();
|
||||
this.header = header;
|
||||
}
|
||||
|
||||
public KLALBPacket(int type) {
|
||||
super();
|
||||
this.type = type;
|
||||
header=headerbufferpool.borrow();
|
||||
header.put((byte) type);
|
||||
}
|
||||
public KLALBPacket(int type, long priority) {
|
||||
this(type);
|
||||
this.priority=priority;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "KLALBPacket [type=" + type + "]";
|
||||
return "KLALBPacket [type=" + getType() + "]";
|
||||
}
|
||||
public int getType() {
|
||||
return type;
|
||||
return header.get(0)&0xff;
|
||||
}
|
||||
|
||||
private long sndtime,rcvtime;
|
||||
protected void writeToStream(DataOutput dto) throws IOException {
|
||||
|
||||
|
||||
|
||||
protected void writeToChannel(WritableByteChannel dto) throws IOException {
|
||||
sndtime=System.nanoTime();
|
||||
dto.write(header.slice(0, getHeaderSize()));
|
||||
}
|
||||
protected void readFromStream(DataInput din) throws IOException {
|
||||
|
||||
protected void readFromChannel(ReadableByteChannel din) throws IOException {
|
||||
rcvtime=System.nanoTime();
|
||||
header.limit(getHeaderSize());
|
||||
while(header.hasRemaining()){
|
||||
if(din.read(header)==-1) {
|
||||
throw new EOFException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected int getHeaderSize() {
|
||||
return 1;
|
||||
}
|
||||
public long getSndtime() {
|
||||
return sndtime;
|
||||
@@ -51,19 +99,150 @@ public abstract class KLALBPacket implements Sumable{
|
||||
return rcvtime;
|
||||
}
|
||||
public long getLength() {
|
||||
return 1;
|
||||
}
|
||||
@Override
|
||||
public long getValue() {
|
||||
return getLength();
|
||||
return getHeaderSize();
|
||||
}
|
||||
|
||||
public long getPriority() {
|
||||
return priority;
|
||||
}
|
||||
public void setPriority(long priority) {
|
||||
this.priority = priority;
|
||||
}
|
||||
public long getSendseq() {
|
||||
return sendseq;
|
||||
}
|
||||
|
||||
private long priority;
|
||||
private long sendseq;
|
||||
private static final AtomicLong seqgen=new AtomicLong();
|
||||
|
||||
private Vector<KLALBRemoteLine> sendRecord=new Vector<>();
|
||||
@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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected void genseq() {
|
||||
this.sendseq=seqgen.getAndIncrement();
|
||||
}
|
||||
|
||||
private List<KLALBRemoteLine> sendRecord=new ArrayList<>(2);
|
||||
private volatile boolean disposed;
|
||||
private volatile ReentrantLock disposeLock=new ReentrantLock();
|
||||
public boolean isDisposed() {
|
||||
return disposed;
|
||||
}
|
||||
|
||||
public Vector<KLALBRemoteLine> getSendRecord() {
|
||||
public List<KLALBRemoteLine> getSendRecord() {
|
||||
return sendRecord;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public ReentrantLock getDisposeLock() {
|
||||
return disposeLock;
|
||||
}
|
||||
|
||||
protected ByteBuffer getHeader() {
|
||||
return header;
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
disposeLock.lock();
|
||||
try {
|
||||
disposed=true;
|
||||
}finally {
|
||||
disposeLock.unlock();
|
||||
}
|
||||
ByteBuffer headerx=header;
|
||||
header=null;
|
||||
KLALBPacket.headerbufferpool.back(headerx);
|
||||
}
|
||||
|
||||
public static KLALBPacket readKLALBPacketFromStream(DataInputStream in) throws IOException {
|
||||
return readKLALBPacketFromChannel(Channels.newChannel(in));
|
||||
}
|
||||
|
||||
public static KLALBPacket readKLALBPacketFromChannel(ReadableByteChannel in) throws IOException {
|
||||
ByteBuffer bb=headerbufferpool.borrow();
|
||||
bb.limit(1);
|
||||
while(bb.hasRemaining()){
|
||||
if(in.read(bb)==-1) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
int type=bb.get(0);
|
||||
bb.limit(bb.capacity());
|
||||
|
||||
KLALBPacket klp;
|
||||
switch(type) {
|
||||
case PING:klp=new PINGPacket(bb);
|
||||
klp.readFromChannel(in);
|
||||
return klp;
|
||||
case PONG:
|
||||
klp=new PONGPacket(bb);
|
||||
klp.readFromChannel(in);
|
||||
return klp;
|
||||
case BWINF:
|
||||
klp=new BWINFPacket(bb);
|
||||
klp.readFromChannel(in);
|
||||
return klp;
|
||||
case RST:
|
||||
klp=new RSTPacket(bb);
|
||||
klp.readFromChannel(in);
|
||||
return klp;
|
||||
case DATAT:
|
||||
klp=new DATATPacket(bb);
|
||||
klp.readFromChannel(in);
|
||||
return klp;
|
||||
case ACKT:
|
||||
klp=new ACKTPacket(bb);
|
||||
klp.readFromChannel(in);
|
||||
return klp;
|
||||
case VADDR:
|
||||
klp=new VADDRPacket(bb);
|
||||
klp.readFromChannel(in);
|
||||
return klp;
|
||||
case ADDLINES:
|
||||
klp=new ADDLINESPacket(bb);
|
||||
klp.readFromChannel(in);
|
||||
return klp;
|
||||
case NACKT:
|
||||
klp=new NACKTPacket(bb);
|
||||
klp.readFromChannel(in);
|
||||
return klp;
|
||||
case TEST:
|
||||
klp=new TESTPacket(bb);
|
||||
klp.readFromChannel(in);
|
||||
return klp;
|
||||
case VADDRACK:
|
||||
klp=new VADDRACKPacket(bb);
|
||||
klp.readFromChannel(in);
|
||||
return klp;
|
||||
case VADDRREQ:
|
||||
klp=new VADDRREQPacket(bb);
|
||||
klp.readFromChannel(in);
|
||||
return klp;
|
||||
}
|
||||
throw new StreamCorruptedException("unknown package type:"+type);
|
||||
}
|
||||
|
||||
public static void writeKLALBPacketToStream(DataOutputStream out,KLALBPacket klb) throws IOException {
|
||||
writeKLALBPacketToChannel(Channels.newChannel(out),klb);
|
||||
}
|
||||
public static void writeKLALBPacketToChannel(WritableByteChannel writableByteChannel,KLALBPacket klb) throws IOException {
|
||||
klb.writeToChannel(writableByteChannel);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user