延迟优化

This commit is contained in:
Administrator
2022-11-26 08:22:16 +08:00
parent 0b5ff2465b
commit 479204919b
4 changed files with 110 additions and 99 deletions
@@ -3,13 +3,13 @@ package org.kne.cloud.network.klalb;
import java.util.Arrays; import java.util.Arrays;
import java.util.Objects; import java.util.Objects;
public class KLALBBlock implements Comparable<KLALBBlock>{ public class KLALBBlock {
volatile byte[]data; volatile byte[]data;
volatile int size; volatile int size;
volatile long number; volatile long number;
volatile long time=-1; //volatile long time=-1;
volatile Thread thread; volatile Thread thread;
public KLALBBlock(byte[] b, int size,long number) { public KLALBBlock(byte[] b, int size,long number) {
data=b; data=b;
@@ -42,16 +42,6 @@ public class KLALBBlock implements Comparable<KLALBBlock>{
KLALBBlock other = (KLALBBlock) obj; KLALBBlock other = (KLALBBlock) obj;
return number == other.number; return number == other.number;
} }
@Override
public int compareTo(KLALBBlock o) {
if(time>o.time) {
return 1;
}else if(time<o.time){
return -1;
}else {
return 0;
}
}
} }
+16 -28
View File
@@ -28,7 +28,6 @@ public class KLALBCore {
private Set<KLALBBlock> inputcache = Collections.synchronizedSet(new HashSet<>()); private Set<KLALBBlock> inputcache = Collections.synchronizedSet(new HashSet<>());
private List<KLALBBlock> outputcache = Collections.synchronizedList(new ArrayList<>()); private List<KLALBBlock> outputcache = Collections.synchronizedList(new ArrayList<>());
private BlockingQueue<KLALBBlock> kack = new LinkedBlockingQueue<>();
private volatile boolean inlocal=true; private volatile boolean inlocal=true;
@@ -91,37 +90,32 @@ public class KLALBCore {
public void sendDataBlock(TCPConnection out) throws IOException, InterruptedException { public void sendDataBlock(TCPConnection out) throws IOException, InterruptedException {
if (close) if (close)
throw new InterruptedException(); throw new InterruptedException();
while (kack.isEmpty() && outputcache.isEmpty()) { while ( outputcache.isEmpty()) {
if (close) if (close)
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
Thread.sleep(1); Thread.sleep(1);
} }
KLALBBlock kx = kack.poll();
if (kx != null) {
kx.time+=System.nanoTime();
send0(out, kx);
} else {
KLALBBlock ks = null; KLALBBlock ks = null;
synchronized (outputcache) { synchronized (outputcache) {
for (int i = 0; i < outputcache.size(); i++) { for (int i = 0; i < outputcache.size(); i++) {
KLALBBlock kd = outputcache.get(i); KLALBBlock kd = outputcache.get(i);
if (kd.time == -1) { if (kd.thread == null) {
kd.time = System.nanoTime(); //kd.time = System.nanoTime();
kd.thread = Thread.currentThread(); kd.thread = Thread.currentThread();
ks = kd; ks = kd;
} else { } else {
if (kd.thread.isAlive()) { if (kd.thread.isAlive()) {
long timex = (System.nanoTime() - kd.time) / 1000000; /*long timex = (System.nanoTime() - kd.time) / 1000000;
if (timex > 10000) { if (timex > 10000) {
System.out.println("³¬Ê±ÖØ´«£º"+kd); System.out.println("³¬Ê±ÖØ´«£º"+kd);
kd.time = System.nanoTime(); kd.time = System.nanoTime();
kd.thread = Thread.currentThread(); kd.thread = Thread.currentThread();
ks = kd; ks = kd;
} }*/
} else { } else {
System.out.println("µôÏßÖØ´«£º"+kd); System.out.println("µôÏßÖØ´«£º"+kd);
kd.time = System.nanoTime(); //kd.time = System.nanoTime();
kd.thread = Thread.currentThread(); kd.thread = Thread.currentThread();
ks = kd; ks = kd;
} }
@@ -135,7 +129,6 @@ public class KLALBCore {
send0(out, ks); send0(out, ks);
// System.out.println(outputcache.size()); // System.out.println(outputcache.size());
} }
}
} }
@@ -144,13 +137,16 @@ public class KLALBCore {
throw new InterruptedException(); throw new InterruptedException();
KLALBBlock x = receive0(in); KLALBBlock x = receive0(in);
if (x.number > 0) { if (x.number > 0) {
try {
KLALBBlock klb=new KLALBBlock(null, 0, -x.number); KLALBBlock klb=new KLALBBlock(null, 0, -x.number);
klb.time=x.time-System.nanoTime(); ThreadTool.makeVThreadIfSupport("ACK", ()->{
kack.put(klb); try {
} catch (InterruptedException e) { send0(in, klb);
} catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
}).start();
if (x.number >= inputcount) { if (x.number >= inputcount) {
while(inputcache.size()>(2*cacheblocks)&&inlocal) { while(inputcache.size()>(2*cacheblocks)&&inlocal) {
@@ -175,13 +171,12 @@ public class KLALBCore {
synchronized (out) { synchronized (out) {
out.writeLong(kd.number); out.writeLong(kd.number);
if (kd.number > 0) { if (kd.number > 0) {
out.writeLong(System.nanoTime());
out.writeInt(kd.size); out.writeInt(kd.size);
out.write(kd.data, 0, kd.size); out.write(kd.data, 0, kd.size);
}else {
out.writeLong(kd.time);
} }
out.flush();
tcp.flush();
} }
System.out.println("SEND:" + kd); System.out.println("SEND:" + kd);
} }
@@ -191,17 +186,10 @@ public class KLALBCore {
DataInputStream in=tcp.getDin(); DataInputStream in=tcp.getDin();
synchronized (in) { synchronized (in) {
kb.number = in.readLong(); kb.number = in.readLong();
kb.time = in.readLong();
if (kb.number > 0) { if (kb.number > 0) {
kb.size = in.readInt(); kb.size = in.readInt();
kb.data = new byte[kb.size]; kb.data = new byte[kb.size];
in.readFully(kb.data); in.readFully(kb.data);
}else{
long dela=(System.nanoTime()-kb.time)/2000000;
//System.out.println(tcp.getTunnel()+" "+dela);
} }
} }
System.out.println("RECEIVE:" + kb); System.out.println("RECEIVE:" + kb);
@@ -27,7 +27,7 @@ public class KLALBServer {
nx=whm.get(uid); nx=whm.get(uid);
}else { }else {
nx=new KLALBServerProtocol(); nx=new KLALBServerProtocol();
Socket soc=new Socket("192.168.1.233",8444); Socket soc=new Socket("192.168.1.233",3389);
nx.setOut(soc.getOutputStream()); nx.setOut(soc.getOutputStream());
nx.setIn(soc.getInputStream()); nx.setIn(soc.getInputStream());
nx.startLocal(); nx.startLocal();
@@ -13,20 +13,26 @@ public class TCPConnection {
private Tunnel tunnel; private Tunnel tunnel;
private Socket connect; private Socket connect;
private DataInputStream din; private DataInputStream din;
public Tunnel getTunnel() { public Tunnel getTunnel() {
return tunnel; return tunnel;
} }
private DataOutputStream dout; private DataOutputStream dout;
private long delay; private long delay;
public Socket getConnect() { public Socket getConnect() {
return connect; return connect;
} }
public DataInputStream getDin() { public DataInputStream getDin() {
return din; return din;
} }
public DataOutputStream getDout() { public DataOutputStream getDout() {
return dout; return dout;
} }
public void close() { public void close() {
// TODO 自动生成的方法存根 // TODO 自动生成的方法存根
try { try {
@@ -52,21 +58,48 @@ public void close() {
} }
connect = null; connect = null;
} }
public long getDelay() { public long getDelay() {
return delay; return delay;
} }
public void setDelay(long delay) { public void setDelay(long delay) {
this.delay = delay; this.delay = delay;
} }
public TCPConnection(Tunnel t) throws UnknownHostException, IOException { public TCPConnection(Tunnel t) throws UnknownHostException, IOException {
this(t, t.connectClientSocket()); this(t, t.connectClientSocket());
} }
public TCPConnection(Tunnel t, Socket soc) throws IOException { public TCPConnection(Tunnel t, Socket soc) throws IOException {
connect = soc; connect = soc;
tunnel = t; tunnel = t;
// connect.setSoTimeout(10000); // connect.setSoTimeout(10000);
din = new DataInputStream(new BufferedInputStream(connect.getInputStream(), 65536)); din = new DataInputStream(new BufferedInputStream(connect.getInputStream(), 65536));
dout = new DataOutputStream(new BufferedOutputStream(connect.getOutputStream(), 65536)); dout = new DataOutputStream(new BufferedOutputStream(connect.getOutputStream(), 65536));
ThreadTool.makeVThreadIfSupport("FLUSH", () -> {
try {
while (true) {
Thread.sleep(100);
if(flush) {
synchronized (dout) {
dout.flush();
} }
flush=false;
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
private volatile boolean flush = false;
public void flush() throws IOException {
flush = true;
}
} }