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
@@ -4,6 +4,7 @@ import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URI;
@@ -21,111 +22,68 @@ import org.kne.cloud.network.mport.ServiceElement;
import org.kne.cloud.network.mport.ThreadTool;
public class KLALBClient {
private static Object olock=new Object();
static {
new Thread(()->{
while(true) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(olock) {
olock.notifyAll();
}
}
}).start();
}
UUID suid=UUID.randomUUID();
IOThreadManager iom=new IOThreadManager();
private List<Tunnel> tls=new ArrayList<>();
private TCPListener tcpl;
private ServiceElement sel;
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.isOpen()) {
try {
TCPConnection tc=new TCPConnection(tll);
tc.getDout().writeShort(59649);
tc.getDout().write(1);
tc.getDout().writeUTF(sel.ipport.getIp().getHostAddress());
tc.getDout().writeInt(sel.ipport.getPort());
tc.getDout().writeUTF(sel.proc);
tc.getDout().writeUTF(tll.getName());
tc.getDout().writeUTF(tll.getIp());
tc.getDout().writeInt(tll.getPort());
tc.getDout().writeLong(uid.getMostSignificantBits());
tc.getDout().writeLong(uid.getLeastSignificantBits());
tc.getDout().flush();
aig.incrementAndGet();
System.out.println("隧道"+tll+"已连接,可用线路数量:"+ (kcp.getTcps().size()+1));
try {
kcp.handleSocket(tc);
}catch(IOException e){
e.printStackTrace();
}finally {
tc.close();
}
int n=kcp.getTcps().size();
System.out.println("隧道"+tll+"已断开,可用线路数量:"+ n);
if(n<=0) {
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;
}
}
synchronized(olock) {
try {
olock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
public void open(int port) throws IOException {
tcpl=new TCPListener(port);
tcpl.setCon((s)->{
IOThreadManager kcp=new IOThreadManager();
UUID uid=UUID.randomUUID();
LocalTCPConnection tcc = null;
try {
tcc = new LocalTCPConnection(s);
System.out.println("TCP"+tcc.getCuid()+"已连接");
iom.handleLocal(tcc,sel,true);
System.out.println("TCP"+tcc.getCuid()+"已关闭");
} catch (IOException e) {
e.printStackTrace();
}finally {
if(tcc!=null) {
tcc.close();
}
}
});
tcpl.open();
for (int i = 0; i < tls.size(); i++) {
Tunnel tll=tls.get(i);
ThreadTool.makeVThreadIfSupport("隧道监视线程", ()->{
RemoteTCPConnection tc=null;
while(iom.isOpen()) {
try {
kcp.setLocal(new TCPConnection(null, s));
kcp.startLocal();
runProtocol(kcp,uid);
tc=new RemoteTCPConnection(tll);
tc.getDout().writeShort(59649);
tc.getDout().write(1);
tc.getDout().writeUTF(tll.getName());
tc.getDout().writeUTF(tll.getIp());
tc.getDout().writeInt(tll.getPort());
tc.getDout().writeLong(suid.getMostSignificantBits());
tc.getDout().writeLong(suid.getLeastSignificantBits());
tc.getDout().flush();
System.out.println(tll+":连接成功");
iom.handleRemote(tc);
System.out.println(tll+":连接断开");
} catch (UnknownHostException e) {
e.printStackTrace();
}catch(ConnectException e) {
} catch (IOException e) {
e.printStackTrace();
}finally {
if(tc!=null)
tc.close();
}
});
tcpl.open();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
public List<Tunnel> getTls() {
return tls;
@@ -139,7 +97,7 @@ public class KLALBClient {
IPPort u=new IPPort(ipport);
TCPConnection tcpc=null;
try {
tcpc=new TCPConnection(null, new Socket(u.getIp(),u.getPort()));
tcpc=new RemoteTCPConnection(null, new Socket(u.getIp(),u.getPort()));
tcpc.getDout().writeShort(59649);
tcpc.getDout().write(0);
tcpc.getDout().flush();