forked from KNEMC/KLALB
修复长时间使用导致连接卡死的故障
优化性能,减少内存占用 提高断线切换的响应速度
This commit is contained in:
@@ -0,0 +1,242 @@
|
||||
package org.kne.cloud.network.klalb;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.ConnectException;
|
||||
import java.net.SocketException;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Queue;
|
||||
import java.util.UUID;
|
||||
import java.util.Vector;
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.concurrent.BlockingDeque;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.kne.cloud.network.mport.ServiceElement;
|
||||
import org.kne.cloud.network.mport.ThreadTool;
|
||||
|
||||
public class KLALBController {
|
||||
private KLALBCore klc = new KLALBCore();
|
||||
private volatile boolean isopen = true;
|
||||
|
||||
public KLALBController() {
|
||||
|
||||
}
|
||||
|
||||
public void handleLocal(LocalTCPConnection tc, boolean syn) {
|
||||
if(!isopen) {
|
||||
return;
|
||||
}
|
||||
//klc.getLocaltcps().put(tc.getCuid(), tc);
|
||||
klc.openLocal(tc);
|
||||
// checkRemotes();
|
||||
|
||||
AtomicBoolean AB=new AtomicBoolean(true);
|
||||
try {
|
||||
|
||||
//AtomicReference<Thread>tn=new AtomicReference<>();
|
||||
//AtomicReference<Thread>ltn=new AtomicReference<>();
|
||||
Thread lt = ThreadTool.makeVThreadIfSupport("数据包计时重发线程", () -> {
|
||||
|
||||
try {
|
||||
while (AB.get()) {
|
||||
klc.outputTimer(tc);
|
||||
/*if(!checkRemotes()) {
|
||||
AB.set(false);
|
||||
}*/
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
AB.set(false);
|
||||
/*tn.get().interrupt();
|
||||
ltn.get().interrupt();
|
||||
tc.getOutputcache().clear();*/
|
||||
tc.close();
|
||||
klc.closeLocal(tc);
|
||||
}
|
||||
});
|
||||
|
||||
//Thread.sleep(500);
|
||||
Thread ls = ThreadTool.makeVThreadIfSupport("本地发送线程", () -> {
|
||||
|
||||
try {
|
||||
while (true) {
|
||||
KLALBBlock rd=klc.getDataBlock(tc);
|
||||
if(rd.data==null) {
|
||||
break;
|
||||
}
|
||||
tc.unpackBlock(rd);
|
||||
}
|
||||
|
||||
} catch (IOException|InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
AB.set(false);
|
||||
/*lt.interrupt();
|
||||
tc.getOutputcache().clear();*/
|
||||
tc.close();
|
||||
klc.closeLocal(tc);
|
||||
}finally {
|
||||
try {
|
||||
tc.getDout().close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
// tn.set(ls);
|
||||
Thread lr = ThreadTool.makeVThreadIfSupport("本地接收线程", () -> {
|
||||
try {
|
||||
while (true) {
|
||||
KLALBBlock ks = tc.packBlock();
|
||||
klc.putDataBlock(tc, ks);
|
||||
if (ks.data == null) {
|
||||
klc.waitOutput(tc);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (IOException|InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
AB.set(false);
|
||||
/*ls.interrupt();
|
||||
lt.interrupt();
|
||||
tc.getOutputcache().clear();*/
|
||||
tc.close();
|
||||
klc.closeLocal(tc);
|
||||
} finally {
|
||||
try {
|
||||
tc.getDin().close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
//ltn.set(lr);
|
||||
tc.setRSTHook((t)->{
|
||||
//System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxx");
|
||||
//System.exit(123);
|
||||
AB.set(false);
|
||||
/*ls.interrupt();
|
||||
lt.interrupt();
|
||||
tc.getOutputcache().clear();*/
|
||||
tc.close();
|
||||
klc.closeLocal(tc);
|
||||
});
|
||||
if (syn) {
|
||||
klc.connectLocal(tc);
|
||||
}
|
||||
ls.start();
|
||||
lr.start();
|
||||
lt.start();
|
||||
//System.out.println(klc.getLocaltcps());
|
||||
lr.join();
|
||||
ls.join();
|
||||
ls.interrupt();
|
||||
lt.interrupt();
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
// TODO 自动生成的 catch 块
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
klc.closeLocal(tc);
|
||||
//klc.getLocaltcps().remove(tc.getCuid());
|
||||
}
|
||||
}
|
||||
|
||||
public void handleRemote(RemoteTCPConnection tc) {
|
||||
if(!isopen) {
|
||||
return;
|
||||
}
|
||||
klc.openRemote(tc);
|
||||
try {
|
||||
Thread rs = ThreadTool.makeVThreadIfSupport("远程发送线程", () -> {
|
||||
|
||||
try {
|
||||
while (true) {
|
||||
klc.remoteSend(tc);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
Thread rr = ThreadTool.makeVThreadIfSupport("远程接收线程", () -> {
|
||||
try {
|
||||
while (true) {
|
||||
klc.remoteReceive(tc);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
rs.interrupt();
|
||||
}
|
||||
});
|
||||
rs.start();
|
||||
rr.start();
|
||||
rr.join();
|
||||
rs.interrupt();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
klc.closeRemote(tc);
|
||||
|
||||
|
||||
// checkRemotes();
|
||||
}
|
||||
}
|
||||
|
||||
/*private boolean checkRemotes() {
|
||||
boolean b=true;
|
||||
if(klc.getRemotetcps().isEmpty()) {
|
||||
System.out.println("所有线路已断开");
|
||||
b=false;
|
||||
Collection<LocalTCPConnection> c=klc.getLocaltcps().values();
|
||||
for (Iterator<LocalTCPConnection> iterator = c.iterator(); iterator.hasNext();) {
|
||||
LocalTCPConnection object =iterator.next();
|
||||
object.close();
|
||||
}
|
||||
}
|
||||
return b;
|
||||
}
|
||||
*/
|
||||
public void close() {
|
||||
isopen = false;
|
||||
Collection<LocalTCPConnection>ltcc= klc.getLocaltcps().values();
|
||||
synchronized ( klc.getLocaltcps()) {
|
||||
|
||||
for (Iterator iterator = ltcc.iterator(); iterator.hasNext();) {
|
||||
LocalTCPConnection localTCPConnection = (LocalTCPConnection) iterator.next();
|
||||
localTCPConnection.close();
|
||||
}
|
||||
}
|
||||
List<RemoteTCPConnection>rtcc= klc.getRemotetcps();
|
||||
synchronized (rtcc) {
|
||||
|
||||
for (Iterator iterator = rtcc.iterator(); iterator.hasNext();) {
|
||||
RemoteTCPConnection remoteTCPConnection = (RemoteTCPConnection) iterator.next();
|
||||
remoteTCPConnection.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isOpen() {
|
||||
return isopen;
|
||||
}
|
||||
|
||||
public KLALBCore getCore() {
|
||||
return klc;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user