forked from KNEMC/KLALB
合并IO线程管理类
This commit is contained in:
@@ -13,17 +13,56 @@ public class KLALBClient {
|
||||
public KLALBClient(int port) throws IOException {
|
||||
tcpl=new TCPListener(port);
|
||||
tcpl.setCon((s)->{
|
||||
KLALBClientProtocol kcp=new KLALBClientProtocol(tls);
|
||||
IOThreadManager kcp=new IOThreadManager();
|
||||
UUID uid=UUID.randomUUID();
|
||||
try {
|
||||
kcp.setIn(new BufferedInputStream(s.getInputStream(),8192));
|
||||
kcp.setOut(new BufferedOutputStream(s.getOutputStream(),8192));
|
||||
kcp.startLocal();
|
||||
kcp.runProtocol();
|
||||
runProtocol(kcp,uid);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
public void runProtocol(IOThreadManager kcp, UUID uid) {
|
||||
for (int i = 0; i < tls.size(); i++) {
|
||||
Tunnel tll=tls.get(i);
|
||||
ThreadTool.makeVThreadIfSupport("隧道监视线程",()->{
|
||||
while (kcp.isClosed()) {
|
||||
try {
|
||||
TCPConnection tc=new TCPConnection(tll);
|
||||
tc.getDout().writeShort(59649);
|
||||
tc.getDout().writeLong(uid.getMostSignificantBits());
|
||||
tc.getDout().writeLong(uid.getLeastSignificantBits());
|
||||
tc.getDout().flush();
|
||||
|
||||
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();
|
||||
System.out.println("连接已断开");
|
||||
return;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
public void open() throws IOException {
|
||||
tcpl.open();
|
||||
|
||||
Reference in New Issue
Block a user