97 lines
2.6 KiB
Java
97 lines
2.6 KiB
Java
package org.kne.cloud.network.klalb;
|
|
|
|
import java.io.DataInputStream;
|
|
import java.io.IOException;
|
|
import java.net.ConnectException;
|
|
import java.net.InetSocketAddress;
|
|
import java.net.Socket;
|
|
import java.util.Collection;
|
|
import java.util.Collections;
|
|
import java.util.HashMap;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
import java.util.WeakHashMap;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import java.util.function.Supplier;
|
|
|
|
import org.kne.cloud.network.mport.IPPort;
|
|
import org.kne.cloud.network.mport.ServiceElement;
|
|
import org.kne.cloud.network.mport.ThreadTool;
|
|
|
|
public class KLALBServer {
|
|
|
|
Map<UUID, IOThreadManager> whm=new WeakHashMap<UUID, IOThreadManager>();
|
|
public KLALBServer(int port,Supplier<String> gjso) throws IOException {
|
|
TCPListener tcpl=new TCPListener(port);
|
|
tcpl.setCon((s)->{
|
|
RemoteTCPConnection tcc=null;
|
|
try {
|
|
//s.setSoTimeout(10000);
|
|
tcc=new RemoteTCPConnection(null,s);
|
|
DataInputStream din=tcc.getDin();
|
|
|
|
int x=din.read();
|
|
if(x==0) {
|
|
String sj=gjso.get();
|
|
tcc.getDout().writeUTF(sj);
|
|
tcc.getDout().flush();
|
|
return;
|
|
}
|
|
|
|
String name = din.readUTF();
|
|
String ip = din.readUTF();
|
|
int portx=din.readInt();
|
|
Tunnel tll=new Tunnel(name,new IPPort( ip, portx));
|
|
tcc.setTunnel(tll);
|
|
|
|
UUID uid=new UUID(din.readLong(),din.readLong());
|
|
System.out.println(uid);
|
|
IOThreadManager nx = null;
|
|
synchronized (tcpl) {
|
|
if(whm.containsKey(uid)) {
|
|
nx=whm.get(uid);
|
|
}else {
|
|
nx=new IOThreadManager();
|
|
IOThreadManager n1=nx;
|
|
nx.getCore().setAcceptSYN((b)->{
|
|
try {
|
|
Socket soc=new Socket(b.lipport.getIp(),b.lipport.getPort());
|
|
LocalTCPConnection ltc=new LocalTCPConnection(soc, b.cuid);
|
|
ThreadTool.makeVThreadIfSupport("局域网连接监视线程", ()->{
|
|
try {
|
|
n1.handleLocal(ltc, false);
|
|
}finally {
|
|
ltc.close();
|
|
}
|
|
}).start();
|
|
return true;
|
|
} catch (IOException e) {
|
|
System.out.println("连接本地服务"+b.lipport+"失败,请检查你的本地服务");
|
|
e.printStackTrace();
|
|
}
|
|
return false;
|
|
});
|
|
whm.put(uid, nx);
|
|
}
|
|
}
|
|
tcc.getDout().write(0);
|
|
tcc.getDout().flush();
|
|
nx.handleRemote(tcc);
|
|
}catch(IOException e) {
|
|
e.printStackTrace();
|
|
}finally {
|
|
if(tcc!=null)
|
|
tcc.close();
|
|
}
|
|
});
|
|
tcpl.open();
|
|
|
|
}
|
|
|
|
/*public static void main(String[] args) throws IOException {
|
|
KLALBServer kc=new KLALBServer(4569);
|
|
}*/
|
|
}
|