修复线程不能正确关闭的问题

This commit is contained in:
Administrator
2022-12-18 09:43:43 +08:00
parent e93a5815b4
commit 0797da8eb3
7 changed files with 48 additions and 14 deletions
@@ -10,6 +10,7 @@ import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.Vector; import java.util.Vector;
import java.util.WeakHashMap; import java.util.WeakHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import org.kne.cloud.network.mport.ServiceElement; import org.kne.cloud.network.mport.ServiceElement;
import org.kne.cloud.network.mport.ThreadTool; import org.kne.cloud.network.mport.ThreadTool;
@@ -24,6 +25,7 @@ public class IOThreadManager {
public void handleLocal(LocalTCPConnection tc, boolean syn) { public void handleLocal(LocalTCPConnection tc, boolean syn) {
klc.getLocaltcps().put(tc.getCuid(), tc); klc.getLocaltcps().put(tc.getCuid(), tc);
AtomicBoolean AB=new AtomicBoolean(true);
try { try {
if (syn) { if (syn) {
KLALBBlock sbk = new KLALBBlock(); KLALBBlock sbk = new KLALBBlock();
@@ -39,7 +41,7 @@ public class IOThreadManager {
Thread lt = ThreadTool.makeVThreadIfSupport("数据包计时重发线程", () -> { Thread lt = ThreadTool.makeVThreadIfSupport("数据包计时重发线程", () -> {
try { try {
while (true) { while (AB.get()) {
klc.outputTimer(tc); klc.outputTimer(tc);
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
@@ -49,16 +51,16 @@ public class IOThreadManager {
Thread ls = ThreadTool.makeVThreadIfSupport("本地发送线程", () -> { Thread ls = ThreadTool.makeVThreadIfSupport("本地发送线程", () -> {
try { try {
while (true) { while (AB.get()) {
KLALBBlock rd=klc.getDataBlock(tc); KLALBBlock rd=klc.getDataBlock(tc);
if(rd.data==null) { if(rd.data==null) {
break; break;
} }
tc.unpackBlock(rd); tc.unpackBlock(rd);
} }
} catch (InterruptedException e) {
} catch (IOException e) { } catch (IOException|InterruptedException e) {
AB.set(false);
lt.interrupt(); lt.interrupt();
tc.getOutputcache().clear(); tc.getOutputcache().clear();
KLALBBlock sbk = new KLALBBlock(); KLALBBlock sbk = new KLALBBlock();
@@ -77,14 +79,15 @@ public class IOThreadManager {
}); });
Thread lr = ThreadTool.makeVThreadIfSupport("本地接收线程", () -> { Thread lr = ThreadTool.makeVThreadIfSupport("本地接收线程", () -> {
try { try {
while (true) { while (AB.get()) {
KLALBBlock ks = tc.packBlock(); KLALBBlock ks = tc.packBlock();
klc.putDataBlock(tc, ks); klc.putDataBlock(tc, ks);
if (ks.data == null) { if (ks.data == null) {
break; break;
} }
} }
} catch (IOException e) { } catch (IOException|InterruptedException e) {
AB.set(false);
ls.interrupt(); ls.interrupt();
lt.interrupt(); lt.interrupt();
tc.getOutputcache().clear(); tc.getOutputcache().clear();
@@ -94,7 +97,6 @@ public class IOThreadManager {
sbk.command = 3; sbk.command = 3;
klc.submitDataBlockNoDelay(sbk); klc.submitDataBlockNoDelay(sbk);
e.printStackTrace(); e.printStackTrace();
} catch (InterruptedException e) {
} finally { } finally {
try { try {
tc.getDin().close(); tc.getDin().close();
@@ -104,7 +106,12 @@ public class IOThreadManager {
} }
}); });
tc.setRSTHook((t)->{
AB.set(false);
ls.interrupt();
lt.interrupt();
tc.getOutputcache().clear();
});
ls.start(); ls.start();
lr.start(); lr.start();
lt.start(); lt.start();
@@ -8,7 +8,7 @@ import java.util.concurrent.atomic.AtomicLong;
import org.kne.cloud.network.mport.IPPort; import org.kne.cloud.network.mport.IPPort;
public class KLALBBlock implements Comparable<KLALBBlock>{ public class KLALBBlock implements Comparable<KLALBBlock>{
private static AtomicLong sng=new AtomicLong(0); private static AtomicLong sng=new AtomicLong(1);
public long sn;//每个数据包的唯一编号 public long sn;//每个数据包的唯一编号
public UUID cuid;//用于识别数据包的stream ID号 public UUID cuid;//用于识别数据包的stream ID号
@@ -40,9 +40,9 @@ public class KLALBClient {
try { try {
tcc = new LocalTCPConnection(s); tcc = new LocalTCPConnection(s);
tcc.setServiceElement(sel); tcc.setServiceElement(sel);
System.out.println("TCP"+tcc.getCuid()+"已连接"); //System.out.println("TCP"+tcc.getCuid()+"已连接");
iom.handleLocal(tcc,true); iom.handleLocal(tcc,true);
System.out.println("TCP"+tcc.getCuid()+"已关闭"); //System.out.println("TCP"+tcc.getCuid()+"已关闭");
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
}finally { }finally {
@@ -55,9 +55,11 @@ public class KLALBClient {
for (int i = 0; i < tls.size(); i++) { for (int i = 0; i < tls.size(); i++) {
Tunnel tll=tls.get(i); Tunnel tll=tls.get(i);
ThreadTool.makeVThreadIfSupport("隧道监视线程", ()->{ ThreadTool.makeVThreadIfSupport("隧道监视线程", ()->{
int re=0;
RemoteTCPConnection tc=null; RemoteTCPConnection tc=null;
while(iom.isOpen()) { while(iom.isOpen()) {
try { try {
re++;
tc=new RemoteTCPConnection(tll); tc=new RemoteTCPConnection(tll);
tc.getDout().writeShort(59649); tc.getDout().writeShort(59649);
tc.getDout().write(1); tc.getDout().write(1);
@@ -71,6 +73,7 @@ public class KLALBClient {
if(v==-1) { if(v==-1) {
throw new EOFException(); throw new EOFException();
} }
re=0;
System.out.println(tll+":连接成功"); System.out.println(tll+":连接成功");
iom.handleRemote(tc); iom.handleRemote(tc);
System.out.println(tll+":连接断开"); System.out.println(tll+":连接断开");
@@ -85,7 +88,8 @@ public class KLALBClient {
tc.close(); tc.close();
} }
try { try {
Thread.sleep(5000); //System.out.println(re);
Thread.sleep(500*(1<<Math.min(re,8)));
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -98,6 +98,7 @@ public class KLALBCore {
for(;;) { for(;;) {
if(tc.checkPingTime()) { if(tc.checkPingTime()) {
KLALBBlock pdb=new KLALBBlock(); KLALBBlock pdb=new KLALBBlock();
pdb.sn=0;
pdb.cuid=ZERO_UUID; pdb.cuid=ZERO_UUID;
pdb.number=0; pdb.number=0;
pdb.command=0; pdb.command=0;
@@ -118,7 +119,7 @@ public class KLALBCore {
KLALBBlock brc=null; KLALBBlock brc=null;
for(;;) { for(;;) {
brc=tc.receiveBlock(); brc=tc.receiveBlock();
if(brc.number!=0) { if(brc.sn==0||brc.number!=0) {
break; break;
} }
SN s=new SN(brc.sn); SN s=new SN(brc.sn);
@@ -145,6 +146,7 @@ public class KLALBCore {
if(brc.number==0) { if(brc.number==0) {
if(brc.command==0) { if(brc.command==0) {
KLALBBlock pdb=new KLALBBlock(); KLALBBlock pdb=new KLALBBlock();
pdb.sn=0;
pdb.cuid=ZERO_UUID; pdb.cuid=ZERO_UUID;
pdb.number=0; pdb.number=0;
pdb.command=1; pdb.command=1;
@@ -172,6 +174,7 @@ public class KLALBCore {
LocalTCPConnection ltc=localtcps.get(brc.cuid); LocalTCPConnection ltc=localtcps.get(brc.cuid);
if(ltc!=null) { if(ltc!=null) {
ltc.close(); ltc.close();
ltc.getRSTHook().accept(ltc);
} }
} }
}else if(brc.number>0){ }else if(brc.number>0){
@@ -244,9 +247,13 @@ public class KLALBCore {
submitDataBlock(ks); submitDataBlock(ks);
ks.sendtime=System.nanoTime(); ks.sendtime=System.nanoTime();
tc.getOutputcache().add(ks); tc.getOutputcache().add(ks);
if(tc.getPeerCacheUsed()>cacheblocks) {
while(tc.getPeerCacheUsed()>cacheblocks) { while(tc.getPeerCacheUsed()>cacheblocks) {
Thread.sleep(1); Thread.sleep(1);
} }
}else if(tc.getPeerCacheUsed()>cacheblocks/2){
Thread.sleep(tc.getPeerCacheUsed()-cacheblocks/2);
}
} }
private volatile long uackt=System.nanoTime(); private volatile long uackt=System.nanoTime();
@@ -256,7 +263,7 @@ public class KLALBCore {
for (int i = 0; i < l.size(); i++) { for (int i = 0; i < l.size(); i++) {
KLALBBlock block=l.get(i); KLALBBlock block=l.get(i);
long timex = (System.nanoTime() - block.sendtime) / 1000000; long timex = (System.nanoTime() - block.sendtime) / 1000000;
if (timex > 200+1000*i) { if (timex > 200*(1<<Math.min(8,block.resend))+1000*i) {
submitDataBlock(block); submitDataBlock(block);
block.sendtime = System.nanoTime(); block.sendtime = System.nanoTime();
block.resend++; block.resend++;
@@ -5,6 +5,7 @@ import java.net.Socket;
import java.util.*; import java.util.*;
import java.util.concurrent.BlockingDeque; import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.LinkedBlockingDeque;
import java.util.function.Consumer;
import org.kne.cloud.network.mport.ServiceElement; import org.kne.cloud.network.mport.ServiceElement;
@@ -90,10 +91,17 @@ private volatile long outputcount = 1;
return sendDeque; return sendDeque;
} }
private volatile int pcu=0; private volatile int pcu=0;
private Consumer<LocalTCPConnection> hook;
public void setPeerCacheUsed(int cacheused) { public void setPeerCacheUsed(int cacheused) {
pcu= cacheused; pcu= cacheused;
} }
public int getPeerCacheUsed() { public int getPeerCacheUsed() {
return pcu; return pcu;
} }
public Consumer<LocalTCPConnection> getRSTHook() {
return hook;
}
public void setRSTHook(Consumer<LocalTCPConnection> tc) {
this.hook=tc;
}
} }
@@ -46,8 +46,10 @@ public class RemoteTCPConnection extends TCPConnection {
public void sendBlock(KLALBBlock data) throws IOException { public void sendBlock(KLALBBlock data) throws IOException {
dout.writeLong(data.sn); dout.writeLong(data.sn);
if(data.sn!=0) {
dout.writeLong(data.cuid.getMostSignificantBits()); dout.writeLong(data.cuid.getMostSignificantBits());
dout.writeLong(data.cuid.getLeastSignificantBits()); dout.writeLong(data.cuid.getLeastSignificantBits());
}
dout.writeLong(data.number); dout.writeLong(data.number);
if (data.number > 0) { if (data.number > 0) {
if (data.data == null) { if (data.data == null) {
@@ -80,7 +82,11 @@ public class RemoteTCPConnection extends TCPConnection {
public KLALBBlock receiveBlock() throws IOException { public KLALBBlock receiveBlock() throws IOException {
KLALBBlock klb = new KLALBBlock(); KLALBBlock klb = new KLALBBlock();
klb.sn = din.readLong(); klb.sn = din.readLong();
if(klb.sn!=0) {
klb.cuid = new UUID(din.readLong(), din.readLong()); klb.cuid = new UUID(din.readLong(), din.readLong());
}else {
klb.cuid=new UUID(0,0);
}
klb.number = din.readLong(); klb.number = din.readLong();
if (klb.number > 0) { if (klb.number > 0) {
int size = din.readInt(); int size = din.readInt();
@@ -16,6 +16,8 @@ import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import javax.naming.spi.Resolver;
import org.kne.cloud.network.mport.IPPort; import org.kne.cloud.network.mport.IPPort;
public class Tunnel { public class Tunnel {