自动获得节点地址功能

This commit is contained in:
Administrator
2022-11-28 15:47:42 +08:00
parent 913313b2a8
commit 05d4117ef3
8 changed files with 344 additions and 23 deletions
@@ -5,23 +5,51 @@ import java.io.IOException;
import java.net.ConnectException;
import java.net.Socket;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.WeakHashMap;
public class KLALBServer {
WeakHashMap<UUID, IOThreadManager> whm=new WeakHashMap<>();
public KLALBServer(int port) throws IOException {
public KLALBServer(int port, List<ServiceElement> services,List<Tunnel> tunnels) throws IOException {
TCPListener tcpl=new TCPListener(port);
tcpl.setCon((s)->{
try {
//s.setSoTimeout(10000);
DataInputStream din=new DataInputStream(s.getInputStream());
TCPConnection tcc=new TCPConnection(null,s);
DataInputStream din=tcc.getDin();
int val=din.readShort()&0xffff;
if(val!=59649) {
return;
}
int x=din.read();
if(x==0) {
synchronized (services) {
int counts=services.size();
tcc.getDout().writeInt(counts);
for (int i = 0; i < counts; i++) {
tcc.getDout().writeUTF(services.get(i).toString());
}
}
synchronized (tunnels) {
int counts=tunnels.size();
tcc.getDout().writeInt(counts);
for (int i = 0; i < counts; i++) {
tcc.getDout().writeUTF(tunnels.get(i).toString());
}
}
tcc.getDout().flush();
return;
}
String name = din.readUTF();
String ip = din.readUTF();
int portx=din.readInt();
Tunnel tll=new Tunnel(name, ip, portx);
tcc.setTunnel(tll);
UUID uid=new UUID(din.readLong(),din.readLong());
System.out.println(uid);
System.out.println("隧道连接:"+tll);
IOThreadManager nx = null;
if(whm.containsKey(uid)) {
nx=whm.get(uid);
@@ -33,7 +61,7 @@ public class KLALBServer {
whm.put(uid, nx);
}
nx.handleSocket(new TCPConnection(null,s));
nx.handleSocket(tcc);
int n=nx.getTcps().size();
if(n<=0) {
nx.closeLocal();
@@ -55,7 +83,7 @@ public class KLALBServer {
}
public static void main(String[] args) throws IOException {
/*public static void main(String[] args) throws IOException {
KLALBServer kc=new KLALBServer(4569);
}
}*/
}