优化代码,性能暴涨

This commit is contained in:
Administrator
2025-01-29 14:01:11 +08:00
parent e243203535
commit 6de65562be
130 changed files with 9138 additions and 1020 deletions
@@ -24,7 +24,27 @@ import org.kne.cloud.network.MultipurposeSocketAddress;
import org.kne.cloud.network.TimeoutTimer;
public class StreamChannelKLALBPacketLink implements KLALBPacketLink {
private static TimeoutTimer tmoTimer=new TimeoutTimer();
private volatile long timeoutTimer;
private volatile boolean timerenabled=false;
private Thread timeouter=new Thread() {
public void run() {
timeoutTimer=System.nanoTime();
while(connectSocket.isOpen()) {
if(timerenabled&&(System.nanoTime()-timeoutTimer>sotimeout*1000000L)) {
if(connectSocket!=null)
try {
connectSocket.close();
} catch (IOException e) {
}
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
@Override
public String toString() {
try {
@@ -38,9 +58,10 @@ public class StreamChannelKLALBPacketLink implements KLALBPacketLink {
private int sotimeout;
public StreamChannelKLALBPacketLink(SocketChannel connectSocket) throws IOException {
this.connectSocket=connectSocket;
connectSocket.setOption(StandardSocketOptions.TCP_NODELAY,true);
timeouter.start();
new KLALBOutputStream(Channels.newOutputStream(connectSocket));
new KLALBInputStream(Channels.newInputStream(connectSocket));
connectSocket.setOption(StandardSocketOptions.TCP_NODELAY,true);
}
@@ -51,19 +72,21 @@ public class StreamChannelKLALBPacketLink implements KLALBPacketLink {
@Override
public KLALBPacket readPacket() throws IOException {
TimerTask tsk= tmoTimer.createTimeOutTask(connectSocket, sotimeout);
timeoutTimer=System.nanoTime();
timerenabled=true;
KLALBPacket res;
try {
res=KLALBPacket.readKLALBPacketFromChannel(connectSocket);
}finally {
if(tsk!=null)
tsk.cancel();
timerenabled=false;
}
return res;
}
@Override
public void close() throws IOException {
//System.out.println("连接被关闭");
//new Exception().printStackTrace();
connectSocket.close();
}