合并IO线程管理类
This commit is contained in:
@@ -0,0 +1,132 @@
|
|||||||
|
package org.kne.cloud.network.klalb;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
|
public class IOThreadManager {
|
||||||
|
private KLALBCore klc=new KLALBCore(100);
|
||||||
|
private List<TCPConnection>tcps=new Vector<>();
|
||||||
|
|
||||||
|
|
||||||
|
private InputStream in;
|
||||||
|
private OutputStream out;
|
||||||
|
|
||||||
|
|
||||||
|
private volatile boolean closed=true;
|
||||||
|
public void startLocal() {
|
||||||
|
|
||||||
|
Thread upo=ThreadTool.makeVThreadIfSupport("本地接收线程",()->{
|
||||||
|
try{
|
||||||
|
|
||||||
|
while(true) {
|
||||||
|
byte[]b=new byte[Consts.BLOCKSIZE];
|
||||||
|
int size=in.read(b);
|
||||||
|
if(size==-1)
|
||||||
|
break;
|
||||||
|
klc.packDataBlock(b,size);
|
||||||
|
}
|
||||||
|
}catch(InterruptedException s) {
|
||||||
|
}catch(Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}finally {
|
||||||
|
closeALL();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Thread downo=ThreadTool.makeVThreadIfSupport("本地发送线程",()->{
|
||||||
|
try{
|
||||||
|
while(true) {
|
||||||
|
|
||||||
|
out.write(klc.unpackDataBlock());
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
|
}catch(InterruptedException s) {
|
||||||
|
}catch(Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}finally {
|
||||||
|
closeALL();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
upo.start();
|
||||||
|
downo.start();
|
||||||
|
}
|
||||||
|
public void handleSocket(TCPConnection s) throws IOException {
|
||||||
|
tcps.add(s);
|
||||||
|
try {
|
||||||
|
Thread up=ThreadTool.makeVThreadIfSupport("远程发送线程",()->{
|
||||||
|
try{
|
||||||
|
while(true) {
|
||||||
|
klc.sendDataBlock(s);
|
||||||
|
}
|
||||||
|
}catch(InterruptedException s1) {
|
||||||
|
|
||||||
|
}catch(Exception e) {
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Thread down=ThreadTool.makeVThreadIfSupport("远程接收线程",()->{
|
||||||
|
try{
|
||||||
|
while(true) {
|
||||||
|
klc.receiveDataBlock(s);
|
||||||
|
}
|
||||||
|
}catch(InterruptedException s1) {
|
||||||
|
|
||||||
|
}catch(Exception e) {
|
||||||
|
up.interrupt();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
up.start();
|
||||||
|
down.start();
|
||||||
|
try {
|
||||||
|
up.join();
|
||||||
|
down.join();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}finally {
|
||||||
|
tcps.remove(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public InputStream getIn() {
|
||||||
|
return in;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIn(InputStream in) {
|
||||||
|
this.in = in;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OutputStream getOut() {
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOut(OutputStream out) {
|
||||||
|
this.out = out;
|
||||||
|
}
|
||||||
|
public void closeALL() {
|
||||||
|
closed=false;
|
||||||
|
try {
|
||||||
|
in.close();
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
out.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
for (int i = 0; i < tcps.size(); i++) {
|
||||||
|
TCPConnection tll=tcps.get(i);
|
||||||
|
tll.close();
|
||||||
|
}
|
||||||
|
klc.close();
|
||||||
|
}
|
||||||
|
public List<TCPConnection> getTcps() {
|
||||||
|
return tcps;
|
||||||
|
}
|
||||||
|
public boolean isClosed() {
|
||||||
|
return closed;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,17 +13,56 @@ public class KLALBClient {
|
|||||||
public KLALBClient(int port) throws IOException {
|
public KLALBClient(int port) throws IOException {
|
||||||
tcpl=new TCPListener(port);
|
tcpl=new TCPListener(port);
|
||||||
tcpl.setCon((s)->{
|
tcpl.setCon((s)->{
|
||||||
KLALBClientProtocol kcp=new KLALBClientProtocol(tls);
|
IOThreadManager kcp=new IOThreadManager();
|
||||||
|
UUID uid=UUID.randomUUID();
|
||||||
try {
|
try {
|
||||||
kcp.setIn(new BufferedInputStream(s.getInputStream(),8192));
|
kcp.setIn(new BufferedInputStream(s.getInputStream(),8192));
|
||||||
kcp.setOut(new BufferedOutputStream(s.getOutputStream(),8192));
|
kcp.setOut(new BufferedOutputStream(s.getOutputStream(),8192));
|
||||||
kcp.startLocal();
|
kcp.startLocal();
|
||||||
kcp.runProtocol();
|
runProtocol(kcp,uid);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
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 {
|
public void open() throws IOException {
|
||||||
tcpl.open();
|
tcpl.open();
|
||||||
|
|||||||
@@ -7,21 +7,13 @@ import java.util.UUID;
|
|||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
|
|
||||||
public class KLALBClientProtocol {
|
public class KLALBClientProtocol {
|
||||||
|
|
||||||
private KLALBCore klc=new KLALBCore(100);
|
private KLALBCore klc=new KLALBCore(100);
|
||||||
|
|
||||||
|
private List<TCPConnection>tcps=new Vector<>();
|
||||||
private List<Tunnel> tls;
|
|
||||||
private volatile boolean flag=true;
|
|
||||||
private InputStream in;
|
private InputStream in;
|
||||||
private OutputStream out;
|
private OutputStream out;
|
||||||
|
private volatile boolean closed=true;
|
||||||
|
|
||||||
private List<TCPConnection>tcps=new Vector<>();
|
|
||||||
private UUID uid=UUID.randomUUID();
|
|
||||||
|
|
||||||
public KLALBClientProtocol(List<Tunnel> tls) {
|
|
||||||
this.tls=tls;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void startLocal() {
|
public void startLocal() {
|
||||||
Thread upo=ThreadTool.makeVThreadIfSupport("本地接收线程",()->{
|
Thread upo=ThreadTool.makeVThreadIfSupport("本地接收线程",()->{
|
||||||
@@ -95,45 +87,10 @@ public class KLALBClientProtocol {
|
|||||||
}
|
}
|
||||||
tcps.remove(tc);
|
tcps.remove(tc);
|
||||||
}
|
}
|
||||||
public void runProtocol() {
|
|
||||||
for (int i = 0; i < tls.size(); i++) {
|
|
||||||
Tunnel tll=tls.get(i);
|
|
||||||
ThreadTool.makeVThreadIfSupport("隧道监视线程",()->{
|
|
||||||
while (flag) {
|
|
||||||
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+"已连接,可用线路数量:"+ tcps.size()+1);
|
|
||||||
handleSocket(tc);
|
|
||||||
|
|
||||||
int n=tcps.size();
|
|
||||||
System.out.println("隧道"+tll+"已断开,可用线路数量:"+ n);
|
|
||||||
if(n<=0) {
|
|
||||||
closeALL();
|
|
||||||
System.out.println("连接已断开");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
Thread.sleep(5000);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}).start();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public InputStream getIn() {
|
public InputStream getIn() {
|
||||||
return in;
|
return in;
|
||||||
}
|
}
|
||||||
@@ -150,7 +107,7 @@ public class KLALBClientProtocol {
|
|||||||
this.out = out;
|
this.out = out;
|
||||||
}
|
}
|
||||||
public void closeALL() {
|
public void closeALL() {
|
||||||
flag=false;
|
closed=false;
|
||||||
try {
|
try {
|
||||||
in.close();
|
in.close();
|
||||||
|
|
||||||
@@ -171,4 +128,12 @@ public class KLALBClientProtocol {
|
|||||||
}
|
}
|
||||||
klc.close();
|
klc.close();
|
||||||
}
|
}
|
||||||
|
public List<TCPConnection> getTcps() {
|
||||||
|
return tcps;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isClosed() {
|
||||||
|
return closed;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,10 @@ import java.util.WeakHashMap;
|
|||||||
import java.util.concurrent.ArrayBlockingQueue;
|
import java.util.concurrent.ArrayBlockingQueue;
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
public class KLALBCore {
|
public class KLALBCore {
|
||||||
private volatile long inputcount = 1;
|
private volatile long inputcount = 1;
|
||||||
@@ -28,17 +31,17 @@ public class KLALBCore {
|
|||||||
private Set<KLALBBlock> inputcache = Collections.synchronizedSet(new HashSet<>());
|
private Set<KLALBBlock> inputcache = Collections.synchronizedSet(new HashSet<>());
|
||||||
private List<KLALBBlock> outputcache = Collections.synchronizedList(new ArrayList<>());
|
private List<KLALBBlock> outputcache = Collections.synchronizedList(new ArrayList<>());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private volatile boolean inlocal=true;
|
private volatile boolean inlocal=true;
|
||||||
|
|
||||||
private volatile boolean close = false;
|
private volatile boolean close = false;
|
||||||
|
|
||||||
|
|
||||||
private int cacheblocks;
|
private volatile int cacheblocks;
|
||||||
public KLALBCore(int cachesize) {
|
public KLALBCore(int cachesize) {
|
||||||
cacheblocks=cachesize;
|
cacheblocks=cachesize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ExecutorService exec=Executors.newCachedThreadPool();
|
||||||
public void packDataBlock(byte[] b, int size) throws InterruptedException {
|
public void packDataBlock(byte[] b, int size) throws InterruptedException {
|
||||||
|
|
||||||
if (close)
|
if (close)
|
||||||
@@ -139,13 +142,14 @@ public class KLALBCore {
|
|||||||
if (x.number > 0) {
|
if (x.number > 0) {
|
||||||
|
|
||||||
KLALBBlock klb=new KLALBBlock(null, 0, -x.number);
|
KLALBBlock klb=new KLALBBlock(null, 0, -x.number);
|
||||||
ThreadTool.makeVThreadIfSupport("ACK", ()->{
|
exec.execute( ()->{
|
||||||
try {
|
try {
|
||||||
send0(in, klb);
|
send0(in, klb);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
}finally {
|
||||||
}
|
}
|
||||||
}).start();
|
});
|
||||||
|
|
||||||
if (x.number >= inputcount) {
|
if (x.number >= inputcount) {
|
||||||
|
|
||||||
@@ -198,5 +202,15 @@ public class KLALBCore {
|
|||||||
|
|
||||||
public void close() {
|
public void close() {
|
||||||
close = true;
|
close = true;
|
||||||
|
exec.shutdown();
|
||||||
|
}
|
||||||
|
public void waitForEnding() {
|
||||||
|
while(outputcache.size()>0||inputcache.size()>0) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(1);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import java.util.UUID;
|
|||||||
import java.util.WeakHashMap;
|
import java.util.WeakHashMap;
|
||||||
|
|
||||||
public class KLALBServer {
|
public class KLALBServer {
|
||||||
WeakHashMap<UUID, KLALBServerProtocol> whm=new WeakHashMap<>();
|
WeakHashMap<UUID, IOThreadManager> whm=new WeakHashMap<>();
|
||||||
public KLALBServer(int port) throws IOException {
|
public KLALBServer(int port) throws IOException {
|
||||||
TCPListener tcpl=new TCPListener(port);
|
TCPListener tcpl=new TCPListener(port);
|
||||||
tcpl.setCon((s)->{
|
tcpl.setCon((s)->{
|
||||||
@@ -22,12 +22,12 @@ public class KLALBServer {
|
|||||||
}
|
}
|
||||||
UUID uid=new UUID(din.readLong(),din.readLong());
|
UUID uid=new UUID(din.readLong(),din.readLong());
|
||||||
System.out.println(uid);
|
System.out.println(uid);
|
||||||
KLALBServerProtocol nx = null;
|
IOThreadManager nx = null;
|
||||||
if(whm.containsKey(uid)) {
|
if(whm.containsKey(uid)) {
|
||||||
nx=whm.get(uid);
|
nx=whm.get(uid);
|
||||||
}else {
|
}else {
|
||||||
nx=new KLALBServerProtocol();
|
nx=new IOThreadManager();
|
||||||
Socket soc=new Socket("192.168.1.233",3389);
|
Socket soc=new Socket("192.168.1.233",8444);
|
||||||
nx.setOut(soc.getOutputStream());
|
nx.setOut(soc.getOutputStream());
|
||||||
nx.setIn(soc.getInputStream());
|
nx.setIn(soc.getInputStream());
|
||||||
nx.startLocal();
|
nx.startLocal();
|
||||||
@@ -35,6 +35,11 @@ public class KLALBServer {
|
|||||||
|
|
||||||
}
|
}
|
||||||
nx.handleSocket(new TCPConnection(null,s));
|
nx.handleSocket(new TCPConnection(null,s));
|
||||||
|
int n=nx.getTcps().size();
|
||||||
|
if(n<=0) {
|
||||||
|
nx.closeALL();
|
||||||
|
System.out.println("连接已关闭");
|
||||||
|
}
|
||||||
}catch(ConnectException e) {
|
}catch(ConnectException e) {
|
||||||
System.out.println("连接本地服务失败,请检查你的服务程序");
|
System.out.println("连接本地服务失败,请检查你的服务程序");
|
||||||
}catch(IOException e) {
|
}catch(IOException e) {
|
||||||
|
|||||||
@@ -10,12 +10,15 @@ import java.io.OutputStream;
|
|||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
public class KLALBServerProtocol {
|
public class KLALBServerProtocol {
|
||||||
private List<TCPConnection>tls=new Vector<>();
|
|
||||||
|
|
||||||
private KLALBCore klc=new KLALBCore(100);
|
private KLALBCore klc=new KLALBCore(100);
|
||||||
|
private List<TCPConnection>tcps=new Vector<>();
|
||||||
|
|
||||||
|
|
||||||
private InputStream in;
|
private InputStream in;
|
||||||
private OutputStream out;
|
private OutputStream out;
|
||||||
|
|
||||||
|
|
||||||
|
private volatile boolean closed=true;
|
||||||
public void startLocal() {
|
public void startLocal() {
|
||||||
|
|
||||||
Thread upo=ThreadTool.makeVThreadIfSupport("本地接收线程",()->{
|
Thread upo=ThreadTool.makeVThreadIfSupport("本地接收线程",()->{
|
||||||
@@ -55,7 +58,7 @@ public class KLALBServerProtocol {
|
|||||||
downo.start();
|
downo.start();
|
||||||
}
|
}
|
||||||
public void handleSocket(TCPConnection s) throws IOException {
|
public void handleSocket(TCPConnection s) throws IOException {
|
||||||
tls.add(s);
|
tcps.add(s);
|
||||||
Thread up=ThreadTool.makeVThreadIfSupport("远程发送线程",()->{
|
Thread up=ThreadTool.makeVThreadIfSupport("远程发送线程",()->{
|
||||||
try{
|
try{
|
||||||
while(true) {
|
while(true) {
|
||||||
@@ -86,8 +89,8 @@ public class KLALBServerProtocol {
|
|||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
tls.remove(s);
|
tcps.remove(s);
|
||||||
int n=tls.size();
|
int n=tcps.size();
|
||||||
if(n<=0) {
|
if(n<=0) {
|
||||||
closeALL();
|
closeALL();
|
||||||
System.out.println("连接已关闭");
|
System.out.println("连接已关闭");
|
||||||
@@ -109,6 +112,7 @@ public class KLALBServerProtocol {
|
|||||||
this.out = out;
|
this.out = out;
|
||||||
}
|
}
|
||||||
public void closeALL() {
|
public void closeALL() {
|
||||||
|
closed=false;
|
||||||
try {
|
try {
|
||||||
in.close();
|
in.close();
|
||||||
|
|
||||||
@@ -120,10 +124,17 @@ public class KLALBServerProtocol {
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
for (int i = 0; i < tls.size(); i++) {
|
for (int i = 0; i < tcps.size(); i++) {
|
||||||
TCPConnection tll=tls.get(i);
|
TCPConnection tll=tcps.get(i);
|
||||||
tll.close();
|
tll.close();
|
||||||
}
|
}
|
||||||
klc.close();
|
klc.close();
|
||||||
}
|
}
|
||||||
|
public List<TCPConnection> getTcps() {
|
||||||
|
return tcps;
|
||||||
|
}
|
||||||
|
public boolean isClosed() {
|
||||||
|
return closed;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ public class TCPConnection {
|
|||||||
ThreadTool.makeVThreadIfSupport("FLUSH", () -> {
|
ThreadTool.makeVThreadIfSupport("FLUSH", () -> {
|
||||||
try {
|
try {
|
||||||
while (true) {
|
while (true) {
|
||||||
Thread.sleep(100);
|
Thread.sleep(10);
|
||||||
if(flush) {
|
if(flush) {
|
||||||
synchronized (dout) {
|
synchronized (dout) {
|
||||||
dout.flush();
|
dout.flush();
|
||||||
|
|||||||
Reference in New Issue
Block a user