forked from KNEMC/KLALB
修复长时间使用导致连接卡死的故障
优化性能,减少内存占用 提高断线切换的响应速度
This commit is contained in:
@@ -21,12 +21,14 @@ import org.kne.cloud.network.mport.ServiceElement;
|
||||
import org.kne.cloud.network.mport.ThreadTool;
|
||||
|
||||
public class KLALBServer {
|
||||
|
||||
Map<UUID, IOThreadManager> whm=Collections.synchronizedMap( new WeakHashMap<UUID, IOThreadManager>());
|
||||
Map<UUID, KLALBController> whm=new ConcurrentHashMap<>();
|
||||
//Map<UUID, IOThreadManager> whm=Collections.synchronizedMap( new WeakHashMap<UUID, IOThreadManager>());
|
||||
public KLALBServer(int port,Supplier<String> gjso) throws IOException {
|
||||
TCPListener tcpl=new TCPListener(port);
|
||||
tcpl.setCon((s)->{
|
||||
RemoteTCPConnection tcc=null;
|
||||
KLALBController nx=null;
|
||||
UUID uid=null;
|
||||
try {
|
||||
//s.setSoTimeout(10000);
|
||||
tcc=new RemoteTCPConnection(null,s);
|
||||
@@ -46,37 +48,50 @@ public class KLALBServer {
|
||||
Tunnel tll=new Tunnel(name,new IPPort( ip, portx));
|
||||
tcc.setTunnel(tll);
|
||||
|
||||
UUID uid=new UUID(din.readLong(),din.readLong());
|
||||
uid=new UUID(din.readLong(),din.readLong());
|
||||
|
||||
tcc.handshake(10);
|
||||
|
||||
IOThreadManager nx=whm.computeIfAbsent(uid, (kuid)->{
|
||||
IOThreadManager iom=new IOThreadManager();
|
||||
iom.getCore().setAcceptSYN((b)->{
|
||||
|
||||
synchronized (whm) {
|
||||
|
||||
nx=whm.computeIfAbsent(uid, (kuid)->{
|
||||
KLALBController iom=new KLALBController();
|
||||
iom.getCore().setAcceptSYN((b,bol)->{
|
||||
try {
|
||||
STJson stj=new STJson(gjso.get());
|
||||
if(!stj.checkSafety(b.lipport)) {
|
||||
System.out.println("未授权的请求:"+b.lipport);
|
||||
return false;
|
||||
bol.accept(false);
|
||||
return;
|
||||
}
|
||||
Socket soc=new Socket(b.lipport.getIp(),b.lipport.getPort());
|
||||
LocalTCPConnection ltc=new LocalTCPConnection(soc, b.cuid);
|
||||
ThreadTool.makeVThreadIfSupport("局域网连接监视线程", ()->{
|
||||
|
||||
|
||||
LocalTCPConnection ltc=null;
|
||||
try {
|
||||
Socket soc=new Socket(b.lipport.getIp(),b.lipport.getPort());
|
||||
ltc=new LocalTCPConnection(soc, b.cuid);
|
||||
bol.accept(true);
|
||||
iom.handleLocal(ltc, false);
|
||||
return;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
if(ltc!=null)
|
||||
ltc.close();
|
||||
}
|
||||
}).start();
|
||||
return true;
|
||||
bol.accept(false);
|
||||
return ;
|
||||
} catch (IOException e) {
|
||||
System.out.println("连接本地服务"+b.lipport+"失败,请检查你的本地服务");
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
bol.accept(false);
|
||||
return;
|
||||
});
|
||||
return iom;
|
||||
});
|
||||
}
|
||||
/*if(whm.containsKey(uid)) {
|
||||
nx=whm.get(uid);
|
||||
}else {
|
||||
@@ -113,6 +128,11 @@ public class KLALBServer {
|
||||
}finally {
|
||||
if(tcc!=null)
|
||||
tcc.close();
|
||||
if(nx.getCore().getRemotetcps().isEmpty()) {
|
||||
nx.close();
|
||||
if(uid!=null)
|
||||
whm.remove(uid);
|
||||
}
|
||||
}
|
||||
});
|
||||
tcpl.open();
|
||||
|
||||
Reference in New Issue
Block a user