TCP连接池功能

This commit is contained in:
Administrator
2022-12-11 17:27:03 +08:00
parent 35cccfde4b
commit 5d7324db0e
15 changed files with 948 additions and 619 deletions
@@ -18,14 +18,15 @@ import org.kne.cloud.network.mport.IPPort;
import org.kne.cloud.network.mport.ServiceElement;
public class KLALBServer {
WeakHashMap<UUID, IOThreadManager> whm=new WeakHashMap<>();
public KLALBServer(int port, Map<String,ServiceElement> services,List<Tunnel> tunnels) throws IOException {
TCPListener tcpl=new TCPListener(port);
tcpl.setCon((s)->{
TCPConnection tcc=null;
RemoteTCPConnection tcc=null;
try {
//s.setSoTimeout(10000);
tcc=new TCPConnection(null,s);
tcc=new RemoteTCPConnection(null,s);
DataInputStream din=tcc.getDin();
int val=din.readShort()&0xffff;
if(val!=59649) {
@@ -52,16 +53,6 @@ public class KLALBServer {
return;
}
String lip=din.readUTF();
int lport=din.readInt();
String lname=din.readUTF();
ServiceElement eas=services.get(lname);
System.out.println(eas);
if(eas==null||(!eas.ipport.equals(new IPPort(lip, lport)))){
return;
}
String name = din.readUTF();
String ip = din.readUTF();
int portx=din.readInt();
@@ -69,28 +60,16 @@ public class KLALBServer {
tcc.setTunnel(tll);
UUID uid=new UUID(din.readLong(),din.readLong());
System.out.println(new IPPort((InetSocketAddress)s.getRemoteSocketAddress())+"->"+tll.getIpport()+"->"+eas.ipport);
IOThreadManager nx = null;
synchronized (tcpl) {
if(whm.containsKey(uid)) {
nx=whm.get(uid);
}else {
nx=new IOThreadManager();
Socket soc=new Socket(eas.ipport.getIp(),eas.ipport.getPort());
nx.setLocal(new TCPConnection(null, soc));
nx.startLocal();
whm.put(uid, nx);
}
nx.handleSocket(tcc);
int n=nx.getTcps().size();
System.out.println(nx.getTcps());
if(n<=0) {
nx.closeLocal();
System.out.println("连接已关闭");
}
}catch(ConnectException e) {
System.out.println("连接本地服务失败,请检查你的服务程序");
nx.handleRemote(tcc);
}catch(IOException e) {
e.printStackTrace();
}finally {