提高程序的稳定性
This commit is contained in:
@@ -4,6 +4,7 @@ import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
@@ -21,6 +22,7 @@ import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
import java.util.concurrent.PriorityBlockingQueue;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.zip.DataFormatException;
|
||||
import java.util.zip.Deflater;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
@@ -39,23 +41,29 @@ public class RemoteTCPConnection extends TCPConnection {
|
||||
t.getRtcs().add(this);
|
||||
}
|
||||
}
|
||||
|
||||
AtomicLong om=new AtomicLong(0);
|
||||
AtomicLong im=new AtomicLong(0);
|
||||
long om1=0;
|
||||
long im1=0;
|
||||
private volatile long ups,downs;
|
||||
private volatile double upsm,downsm;
|
||||
@Override
|
||||
protected void initIO() throws IOException {
|
||||
|
||||
|
||||
}
|
||||
|
||||
protected void initIOs() throws IOException {
|
||||
connect.setTrafficClass(0x10);
|
||||
connect.setTcpNoDelay(true);
|
||||
OutputStream outm=null;
|
||||
InputStream inm=null;
|
||||
if(tunnel!=null) {
|
||||
outm = new OutputMetre( connect.getOutputStream(),tunnel.getOM());
|
||||
inm = new InputMetre(connect.getInputStream(),tunnel.getIM());
|
||||
outm = new OutputMetre( connect.getOutputStream(),tunnel.getOM(),om);
|
||||
inm = new InputMetre(connect.getInputStream(),tunnel.getIM(),im);
|
||||
}else {
|
||||
outm=connect.getOutputStream();
|
||||
inm=connect.getInputStream();
|
||||
outm = new OutputMetre( connect.getOutputStream(),om);
|
||||
inm = new InputMetre(connect.getInputStream(),im);
|
||||
}
|
||||
|
||||
new DataOutputStream(outm).writeShort(59649);//59649
|
||||
@@ -70,6 +78,62 @@ public class RemoteTCPConnection extends TCPConnection {
|
||||
din = new DataInputStream(inm);
|
||||
|
||||
}
|
||||
public long getUps() {
|
||||
return ups;
|
||||
}
|
||||
|
||||
public long getDowns() {
|
||||
return downs;
|
||||
}
|
||||
|
||||
public AtomicLong getOm() {
|
||||
return om;
|
||||
}
|
||||
|
||||
public AtomicLong getIm() {
|
||||
return im;
|
||||
}
|
||||
private volatile long ptime=System.nanoTime();
|
||||
public void updateTraffics() {
|
||||
long ctime=System.nanoTime();
|
||||
long det=ctime-ptime;
|
||||
ptime=ctime;
|
||||
ups= (om.get()-om1)*1000000000/det;
|
||||
downs= (im.get()-im1)*1000000000/det;
|
||||
/*
|
||||
if(ndbg==null)
|
||||
initcdbg();
|
||||
ndbg.println((om.get()-om1)+ (im.get()-im1)+","+delay);
|
||||
*/
|
||||
om1=om.get();
|
||||
im1=im.get();
|
||||
if(ups>upsm) {
|
||||
upsm=ups;
|
||||
}else {
|
||||
upsm=(upsm*1000+ups)/1001;
|
||||
}
|
||||
if(downs>downsm) {
|
||||
downsm=downs;
|
||||
}else {
|
||||
downsm=(downsm*1000+downs)/1001;
|
||||
}
|
||||
//predictLatency();
|
||||
//System.out.println(tunnel+"//// "+upsm+" "+downsm+" "+getPredictedLatency());
|
||||
}
|
||||
private double latency;
|
||||
public double getPredictedLatency() {
|
||||
return latency;
|
||||
}
|
||||
public void predictLatency() {
|
||||
AtomicLong data=new AtomicLong(0);
|
||||
sendDeque.forEach((e)->{
|
||||
if(e.data!=null) {
|
||||
data.addAndGet(e.data.length);
|
||||
}
|
||||
});
|
||||
latency= delay/2000000.0+data.get()*1000.0/upsm;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "RemoteTCPConnection [tunnel=" + tunnel + "]";
|
||||
@@ -105,16 +169,6 @@ public class RemoteTCPConnection extends TCPConnection {
|
||||
|
||||
}
|
||||
long odelay=Long.MAX_VALUE;
|
||||
public void setDelayAvg(long delay) {
|
||||
if(odelay==Long.MAX_VALUE) {
|
||||
odelay=delay;
|
||||
setDelay(odelay);
|
||||
}else {
|
||||
odelay=(delay+odelay*100)/101;
|
||||
setDelay(odelay);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (tunnel != null) {
|
||||
@@ -141,16 +195,16 @@ public class RemoteTCPConnection extends TCPConnection {
|
||||
if (data.data == null) {
|
||||
dout.writeShort(-1);
|
||||
} else {
|
||||
Deflater def = new Deflater(Deflater.BEST_COMPRESSION, true);
|
||||
/*Deflater def = new Deflater(Deflater.BEST_COMPRESSION, true);
|
||||
def.setInput(data.data);
|
||||
def.finish();
|
||||
byte[] b = new byte[(int)(data.data.length * 1.1D) + 64];
|
||||
int nsize = def.deflate(b);
|
||||
def.end();
|
||||
this.dout.writeShort(nsize);
|
||||
this.dout.write(b, 0, nsize);
|
||||
/*dout.writeShort(data.data.length);
|
||||
dout.write(data.data);*/
|
||||
this.dout.write(b, 0, nsize);*/
|
||||
dout.writeShort(data.data.length);
|
||||
dout.write(data.data);
|
||||
//System.out.println(Arrays.toString(data.data));
|
||||
}
|
||||
} else if (data.number == 0) {
|
||||
@@ -179,7 +233,7 @@ public class RemoteTCPConnection extends TCPConnection {
|
||||
if(isflush)
|
||||
dout.flush();
|
||||
|
||||
//System.out.println("SEND:" + data);
|
||||
System.out.println("SEND:" + data);
|
||||
}
|
||||
|
||||
public KLALBBlock receiveBlock() throws IOException, DataFormatException {
|
||||
@@ -193,7 +247,7 @@ public class RemoteTCPConnection extends TCPConnection {
|
||||
} else {
|
||||
byte[] d = new byte[size];
|
||||
din.readFully(d);
|
||||
Inflater in = new Inflater(true);
|
||||
/*Inflater in = new Inflater(true);
|
||||
in.setInput(d);
|
||||
byte[] b = new byte[8192];
|
||||
int nsize = in.inflate(b);
|
||||
@@ -202,9 +256,9 @@ public class RemoteTCPConnection extends TCPConnection {
|
||||
klb.data = b;
|
||||
} else {
|
||||
klb.data = Arrays.copyOf(b, nsize);
|
||||
}
|
||||
} */
|
||||
//System.out.println(Arrays.toString(d));
|
||||
//klb.data = d;
|
||||
klb.data = d;
|
||||
}
|
||||
} else if (klb.number == 0) {
|
||||
klb.command = din.read();
|
||||
@@ -229,7 +283,7 @@ public class RemoteTCPConnection extends TCPConnection {
|
||||
klb.cacheused = din.readInt();
|
||||
}
|
||||
|
||||
//System.out.println("RECEIVE:" + klb);
|
||||
System.out.println("RECEIVE:" + klb);
|
||||
return klb;
|
||||
}
|
||||
|
||||
@@ -250,7 +304,7 @@ public class RemoteTCPConnection extends TCPConnection {
|
||||
public Queue<KLALBBlock> getNDSendDeque() {
|
||||
return NDsendDeque;
|
||||
}
|
||||
|
||||
//private Queue<KLALBBlock> sendDeque = new ConcurrentLinkedQueue<KLALBBlock>();
|
||||
private Queue<KLALBBlock> sendDeque = new PriorityBlockingQueue<KLALBBlock>(2, new Comparator<KLALBBlock>() {
|
||||
|
||||
@Override
|
||||
@@ -260,14 +314,8 @@ public class RemoteTCPConnection extends TCPConnection {
|
||||
}else if(o1.resend<o2.resend) {
|
||||
return 1;
|
||||
}else {
|
||||
if(o1.number>o2.number) {
|
||||
return 1;
|
||||
}else if(o1.number<o2.number) {
|
||||
return -1;
|
||||
}else {
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -275,4 +323,16 @@ public class RemoteTCPConnection extends TCPConnection {
|
||||
return sendDeque;
|
||||
}
|
||||
|
||||
public void handshake(int x) throws IOException {
|
||||
for(int i=0;i<x;i++) {
|
||||
getDout().write(0);
|
||||
getDout().flush();
|
||||
int v=getDin().read();
|
||||
if(v==-1) {
|
||||
throw new EOFException();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user