forked from KNEMC/KLALB
KLALB完全大重写,代码规范了,可以当作网络库使用
This commit is contained in:
@@ -0,0 +1,204 @@
|
||||
package org.kne.cloud.network.klalb;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Inet6Address;
|
||||
import java.net.Socket;
|
||||
import java.util.concurrent.PriorityBlockingQueue;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.kne.cloud.network.mport.ThreadTool;
|
||||
|
||||
public class KLALBRemoteSocket extends MonitoredSocket {
|
||||
private Inet6Address remoteVaddr;
|
||||
|
||||
public Inet6Address getRemoteVaddr() {
|
||||
return remoteVaddr;
|
||||
}
|
||||
|
||||
public KLALBRemoteSocket(Socket socket) {
|
||||
super(socket);
|
||||
ThreadTool.makeVDaemonThreadIfSupport("远程接收线程", () -> {
|
||||
try {
|
||||
|
||||
while (!isClosed()) {
|
||||
KLALBPacket kpp = getInputStream().readPacket();
|
||||
//System.out.println("RX:" + kpp);
|
||||
if(kpp instanceof PINGPacket) {
|
||||
sendPacket(new PONGPacket(), 65540);
|
||||
}else if(kpp instanceof PONGPacket) {
|
||||
latency.set(System.nanoTime()-pingstarttime);
|
||||
pinging=false;
|
||||
}else {
|
||||
if(kpp instanceof VADDRPacket) {
|
||||
remoteVaddr=((VADDRPacket) kpp).getVaddr();
|
||||
}
|
||||
while(rec==null) {
|
||||
Thread.sleep(1);
|
||||
}
|
||||
rec.accept(kpp);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
ThreadTool.makeVDaemonThreadIfSupport("远程发送线程", () -> {
|
||||
try {
|
||||
|
||||
while (!isClosed()) {
|
||||
if(System.nanoTime()-pingstarttime>100000000000L) {
|
||||
pinging=false;
|
||||
}
|
||||
if(checkPingTime()&&!pinging) {
|
||||
getOutputStream().writePacket(new PINGPacket());
|
||||
pinging =true;
|
||||
pingstarttime=System.nanoTime();
|
||||
}else {
|
||||
PQItem pqi=sendQueue.poll();
|
||||
if(pqi!=null) {
|
||||
KLALBPacket kpp=pqi.getPacket();
|
||||
//System.out.println("TX:" + kpp);
|
||||
getOutputStream().writePacket(kpp);
|
||||
}else {
|
||||
Thread.sleep(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
} finally {
|
||||
try {
|
||||
close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
|
||||
}
|
||||
|
||||
private volatile long pingstarttime=System.nanoTime();
|
||||
|
||||
private volatile boolean pinging=false;
|
||||
private volatile long time = System.nanoTime();
|
||||
private volatile long pingInterval=1000000000L;
|
||||
|
||||
public long getPingInterval() {
|
||||
return pingInterval;
|
||||
}
|
||||
|
||||
public void setPingInterval(long ns) {
|
||||
this.pingInterval = ns;
|
||||
}
|
||||
|
||||
private boolean checkPingTime() {
|
||||
long cu = System.nanoTime();
|
||||
if (cu - time > pingInterval) {
|
||||
time = cu;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private volatile boolean waitingforPing = false;
|
||||
|
||||
public long getLatency() {
|
||||
return latency.get();
|
||||
}
|
||||
|
||||
private KLALBInputStream kis;
|
||||
private KLALBOutputStream kos;
|
||||
|
||||
@Override
|
||||
public KLALBInputStream getInputStream() throws IOException {
|
||||
if (kis == null)
|
||||
kis = new KLALBInputStream(super.getInputStream());
|
||||
return kis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KLALBOutputStream getOutputStream() throws IOException {
|
||||
if (kos == null)
|
||||
kos = new KLALBOutputStream(super.getOutputStream());
|
||||
return kos;
|
||||
}
|
||||
|
||||
private AtomicLong latency = new AtomicLong();
|
||||
private volatile Consumer<KLALBPacket> rec;
|
||||
private Consumer<KLALBRemoteSocket> clo;
|
||||
private PriorityBlockingQueue<PQItem> sendQueue=new PriorityBlockingQueue<PQItem>();
|
||||
|
||||
protected PriorityBlockingQueue<PQItem> getSendQueue() {
|
||||
return sendQueue;
|
||||
}
|
||||
|
||||
public void sendPacket(KLALBPacket kp) {
|
||||
sendPacket(kp, 5);
|
||||
}
|
||||
|
||||
public void sendPacket(KLALBPacket kp,int priority) {
|
||||
sendQueue.add(new PQItem(kp, priority));
|
||||
}
|
||||
|
||||
public void setPacketReceiver(Consumer<KLALBPacket> rec) {
|
||||
this.rec = rec;
|
||||
}
|
||||
public void setCloseListener(Consumer<KLALBRemoteSocket> clo) {
|
||||
this.clo = clo;
|
||||
}
|
||||
@Override
|
||||
public synchronized void close() throws IOException {
|
||||
super.close();
|
||||
if(!isClosed()&&clo!=null)
|
||||
clo.accept(this);
|
||||
}
|
||||
|
||||
private AtomicLong al=new AtomicLong(0);
|
||||
protected class PQItem implements Comparable<PQItem>{
|
||||
private KLALBPacket packet;
|
||||
private long index=al.getAndIncrement();
|
||||
private int priority=5;
|
||||
public PQItem(KLALBPacket value, int priority) {
|
||||
super();
|
||||
this.packet = value;
|
||||
this.priority = priority;
|
||||
}
|
||||
public KLALBPacket getPacket() {
|
||||
return packet;
|
||||
}
|
||||
public long getIndex() {
|
||||
return index;
|
||||
}
|
||||
public int getPriority() {
|
||||
return priority;
|
||||
}
|
||||
@Override
|
||||
public int compareTo(PQItem o) {
|
||||
if(priority<o.priority) {
|
||||
return 1;
|
||||
}else if(priority>o.priority) {
|
||||
return -1;
|
||||
}else {
|
||||
if(index>o.index) {
|
||||
return 1;
|
||||
}else if(index<o.index){
|
||||
return -1;
|
||||
}else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user