性能优化

This commit is contained in:
Administrator
2022-11-27 06:36:27 +08:00
parent 642c197143
commit 4f2b416ac3
10 changed files with 129 additions and 372 deletions
@@ -6,6 +6,8 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
public class KLALBClient {
private List<Tunnel> tls=new ArrayList<>();
@@ -16,8 +18,7 @@ public class KLALBClient {
IOThreadManager kcp=new IOThreadManager();
UUID uid=UUID.randomUUID();
try {
kcp.setIn(new BufferedInputStream(s.getInputStream(),8192));
kcp.setOut(new BufferedOutputStream(s.getOutputStream(),8192));
kcp.setLocal(new TCPConnection(null, s));
kcp.startLocal();
runProtocol(kcp,uid);
} catch (IOException e) {
@@ -27,28 +28,40 @@ public class KLALBClient {
});
}
public void runProtocol(IOThreadManager kcp, UUID uid) {
AtomicBoolean b=new AtomicBoolean(true);
AtomicInteger aig=new AtomicInteger(0);
for (int i = 0; i < tls.size(); i++) {
Tunnel tll=tls.get(i);
ThreadTool.makeVThreadIfSupport("隧道监视线程",()->{
while (kcp.isClosed()) {
while (kcp.isOpen()) {
try {
TCPConnection tc=new TCPConnection(tll);
tc.getDout().writeShort(59649);
tc.getDout().writeLong(uid.getMostSignificantBits());
tc.getDout().writeLong(uid.getLeastSignificantBits());
tc.getDout().flush();
aig.incrementAndGet();
System.out.println("隧道"+tll+"已连接,可用线路数量:"+ (kcp.getTcps().size()+1));
kcp.handleSocket(tc);
int n=kcp.getTcps().size();
System.out.println("隧道"+tll+"已断开,可用线路数量:"+ n);
if(n<=0) {
kcp.closeALL();
kcp.closeRemote();
kcp.closeLocal();
System.out.println("连接已断开");
return;
}
} catch (IOException e) {
if(aig.get()<=0) {
kcp.closeRemote();
kcp.closeLocal();
if(b.get()) {
System.out.println("连接失败");
b.set(false);
}
return;
}
}
try {