修复Kperf测速工具仪表盘显示bug,优化Kperf缓存命中率
This commit is contained in:
@@ -1417,29 +1417,29 @@ public class KLALBStateGUI3 extends XFrame {
|
|||||||
private void updateDashboardData(KLALBController kc) {
|
private void updateDashboardData(KLALBController kc) {
|
||||||
// 更新上行速度
|
// 更新上行速度
|
||||||
double outload = kc.getLinkMonitor().getOutSpeed() * 100.0 / kc.getLinkMonitor().getOutSpeedMax();
|
double outload = kc.getLinkMonitor().getOutSpeed() * 100.0 / kc.getLinkMonitor().getOutSpeedMax();
|
||||||
|
upSpeedText.setLabel(KLALBUtils.convertIBUint(kc.getLinkMonitor().getOutSpeed()) + "/s");
|
||||||
if (Double.isFinite(outload)) {
|
if (Double.isFinite(outload)) {
|
||||||
upSpeedText.setLabel(KLALBUtils.convertIBUint(kc.getLinkMonitor().getOutSpeed()) + "/s");
|
|
||||||
upSpeed.setValue(Math.min(upSpeed.getValue().doubleValue() * 0.98 + outload * 0.02, 101.0));
|
upSpeed.setValue(Math.min(upSpeed.getValue().doubleValue() * 0.98 + outload * 0.02, 101.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新下行速度
|
// 更新下行速度
|
||||||
double inload = kc.getLinkMonitor().getInSpeed() * 100.0 / kc.getLinkMonitor().getInSpeedMax();
|
double inload = kc.getLinkMonitor().getInSpeed() * 100.0 / kc.getLinkMonitor().getInSpeedMax();
|
||||||
|
downSpeedText.setLabel(KLALBUtils.convertIBUint(kc.getLinkMonitor().getInSpeed()) + "/s");
|
||||||
if (Double.isFinite(inload)) {
|
if (Double.isFinite(inload)) {
|
||||||
downSpeedText.setLabel(KLALBUtils.convertIBUint(kc.getLinkMonitor().getInSpeed()) + "/s");
|
|
||||||
downSpeed.setValue(Math.min(downSpeed.getValue().doubleValue() * 0.98 + inload * 0.02, 101.0));
|
downSpeed.setValue(Math.min(downSpeed.getValue().doubleValue() * 0.98 + inload * 0.02, 101.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新上行包速率
|
// 更新上行包速率
|
||||||
double outPPSload = kc.getLinkMonitor().getOutPPS() * 100.0 / kc.getLinkMonitor().getOutPPSMax();
|
double outPPSload = kc.getLinkMonitor().getOutPPS() * 100.0 / kc.getLinkMonitor().getOutPPSMax();
|
||||||
|
upPPSText.setLabel(KLALBUtils.convertUintDefalut(kc.getLinkMonitor().getOutPPS()) + "PPS");
|
||||||
if (Double.isFinite(outPPSload)) {
|
if (Double.isFinite(outPPSload)) {
|
||||||
upPPSText.setLabel(KLALBUtils.convertUintDefalut(kc.getLinkMonitor().getOutPPS()) + "PPS");
|
|
||||||
upPPS.setValue(Math.min(upPPS.getValue().doubleValue() * 0.98 + outPPSload * 0.02, 101.0));
|
upPPS.setValue(Math.min(upPPS.getValue().doubleValue() * 0.98 + outPPSload * 0.02, 101.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新下行包速率
|
// 更新下行包速率
|
||||||
double inPPSload = kc.getLinkMonitor().getInPPS() * 100.0 / kc.getLinkMonitor().getInPPSMax();
|
double inPPSload = kc.getLinkMonitor().getInPPS() * 100.0 / kc.getLinkMonitor().getInPPSMax();
|
||||||
|
downPPSText.setLabel(KLALBUtils.convertUintDefalut(kc.getLinkMonitor().getInPPS()) + "PPS");
|
||||||
if (Double.isFinite(inPPSload)) {
|
if (Double.isFinite(inPPSload)) {
|
||||||
downPPSText.setLabel(KLALBUtils.convertUintDefalut(kc.getLinkMonitor().getInPPS()) + "PPS");
|
|
||||||
downPPS.setValue(Math.min(downPPS.getValue().doubleValue() * 0.98 + inPPSload * 0.02, 101.0));
|
downPPS.setValue(Math.min(downPPS.getValue().doubleValue() * 0.98 + inPPSload * 0.02, 101.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1471,29 +1471,29 @@ public class KLALBStateGUI3 extends XFrame {
|
|||||||
if(router!=null) {
|
if(router!=null) {
|
||||||
// 更新背板延迟
|
// 更新背板延迟
|
||||||
double backplaneTimev = router.getBackplaneTime()* 100.0/100000.0;
|
double backplaneTimev = router.getBackplaneTime()* 100.0/100000.0;
|
||||||
|
backplaneDelayText.setLabel(KLALBUtils.convertUintNanoDefalut(router.getBackplaneTime()) + "s");
|
||||||
if (Double.isFinite(backplaneTimev)) {
|
if (Double.isFinite(backplaneTimev)) {
|
||||||
backplaneDelayText.setLabel(KLALBUtils.convertUintNanoDefalut(router.getBackplaneTime()) + "s");
|
|
||||||
backplaneDelay.setValue(Math.min(backplaneDelay.getValue().doubleValue() * 0.98 + backplaneTimev * 0.02, 101.0));
|
backplaneDelay.setValue(Math.min(backplaneDelay.getValue().doubleValue() * 0.98 + backplaneTimev * 0.02, 101.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新背板转发率
|
// 更新背板转发率
|
||||||
double backplanePPSv = router.getBackplanePPS() * 100.0 / router.getBackplanePPSMax();
|
double backplanePPSv = router.getBackplanePPS() * 100.0 / router.getBackplanePPSMax();
|
||||||
|
backplanePPSText.setLabel(KLALBUtils.convertUintDefalut(router.getBackplanePPS()) + "PPS");
|
||||||
if (Double.isFinite(backplanePPSv)) {
|
if (Double.isFinite(backplanePPSv)) {
|
||||||
backplanePPSText.setLabel(KLALBUtils.convertUintDefalut(router.getBackplanePPS()) + "PPS");
|
|
||||||
backplanePPS.setValue(Math.min(backplanePPS.getValue().doubleValue() * 0.98 + backplanePPSv * 0.02, 101.0));
|
backplanePPS.setValue(Math.min(backplanePPS.getValue().doubleValue() * 0.98 + backplanePPSv * 0.02, 101.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新背板ECN率
|
// 更新背板ECN率
|
||||||
double backplaneECNv = router.getECNRate() * 100.0 ;
|
double backplaneECNv = router.getECNRate() * 100.0 ;
|
||||||
|
backplaneECNRateText.setLabel(String.format("%.1f",backplaneECNv) + "%");
|
||||||
if (Double.isFinite(backplaneECNv)) {
|
if (Double.isFinite(backplaneECNv)) {
|
||||||
backplaneECNRateText.setLabel(String.format("%.1f",backplaneECNv) + "%");
|
|
||||||
backplaneECNRate.setValue(Math.min(backplaneECNRate.getValue().doubleValue() * 0.98 + backplaneECNv * 0.02, 101.0));
|
backplaneECNRate.setValue(Math.min(backplaneECNRate.getValue().doubleValue() * 0.98 + backplaneECNv * 0.02, 101.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新背板ECN率
|
// 更新背板ECN率
|
||||||
double backplaneLossv = router.getLossRate() * 100.0 ;
|
double backplaneLossv = router.getLossRate() * 100.0 ;
|
||||||
|
backplaneLossRateText.setLabel(String.format("%.1f",backplaneLossv) + "%");
|
||||||
if (Double.isFinite(backplaneLossv)) {
|
if (Double.isFinite(backplaneLossv)) {
|
||||||
backplaneLossRateText.setLabel(String.format("%.1f",backplaneLossv) + "%");
|
|
||||||
backplaneLossRate.setValue(Math.min(backplaneLossRate.getValue().doubleValue() * 0.98 + backplaneLossv * 0.02, 101.0));
|
backplaneLossRate.setValue(Math.min(backplaneLossRate.getValue().doubleValue() * 0.98 + backplaneLossv * 0.02, 101.0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,7 +45,6 @@ public class KperfGUI extends XFrame{
|
|||||||
private Kperf kperf;
|
private Kperf kperf;
|
||||||
private JTextField targetAddressField;
|
private JTextField targetAddressField;
|
||||||
private JButton startButton;
|
private JButton startButton;
|
||||||
private JProgressBar progressBar;
|
|
||||||
private DefaultValueDataset upSpeed;
|
private DefaultValueDataset upSpeed;
|
||||||
private DefaultValueDataset upDataSpeed;
|
private DefaultValueDataset upDataSpeed;
|
||||||
private DialTextAnnotation upSpeedText;
|
private DialTextAnnotation upSpeedText;
|
||||||
@@ -54,7 +53,7 @@ public class KperfGUI extends XFrame{
|
|||||||
private DialTextAnnotation downSpeedText;
|
private DialTextAnnotation downSpeedText;
|
||||||
private JTextField sourceAddressField;
|
private JTextField sourceAddressField;
|
||||||
|
|
||||||
private SpeedAndTrafficAndDelayMonitorDataImpl monitor;
|
private volatile SpeedAndTrafficAndDelayMonitorDataImpl monitor;
|
||||||
|
|
||||||
private DefaultValueDataset upPPS;
|
private DefaultValueDataset upPPS;
|
||||||
|
|
||||||
@@ -70,10 +69,6 @@ public class KperfGUI extends XFrame{
|
|||||||
|
|
||||||
private TimerTask tsk;
|
private TimerTask tsk;
|
||||||
private class KperfReportsImpl implements KperfReports{
|
private class KperfReportsImpl implements KperfReports{
|
||||||
@Override
|
|
||||||
public void testProcess(double process) {
|
|
||||||
progressBar.setValue((int) (process*100.0f));
|
|
||||||
}
|
|
||||||
@Override
|
@Override
|
||||||
public void testFinish() {
|
public void testFinish() {
|
||||||
if(kperf!=null) {
|
if(kperf!=null) {
|
||||||
@@ -520,10 +515,7 @@ public class KperfGUI extends XFrame{
|
|||||||
panel_top.add(panel);
|
panel_top.add(panel);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
progressBar = new JProgressBar();
|
|
||||||
progressBar.setPreferredSize(new Dimension(100, 5));
|
|
||||||
getContentPane().add(progressBar, BorderLayout.NORTH);
|
|
||||||
getTitlelabel().setFont(UIEnv.getFont().deriveFont(17.0f));
|
getTitlelabel().setFont(UIEnv.getFont().deriveFont(17.0f));
|
||||||
getTitlepanel().setBorder(new LineBorder(Color.LIGHT_GRAY));
|
getTitlepanel().setBorder(new LineBorder(Color.LIGHT_GRAY));
|
||||||
getTitlelabel().setForeground(Color.WHITE);
|
getTitlelabel().setForeground(Color.WHITE);
|
||||||
@@ -538,26 +530,26 @@ public class KperfGUI extends XFrame{
|
|||||||
public void run() {
|
public void run() {
|
||||||
if(monitor!=null) {
|
if(monitor!=null) {
|
||||||
double outload = monitor.getOutSpeed() * 100.0 / monitor.getOutSpeedMax();
|
double outload = monitor.getOutSpeed() * 100.0 / monitor.getOutSpeedMax();
|
||||||
if (Double.isFinite(outload)) {
|
|
||||||
upSpeedText.setLabel(KLALBUtils.convertIBUint(monitor.getOutSpeed()) + "/s");
|
upSpeedText.setLabel(KLALBUtils.convertIBUint(monitor.getOutSpeed()) + "/s");
|
||||||
|
if (Double.isFinite(outload)) {
|
||||||
upSpeed.setValue(Math.min(upSpeed.getValue().doubleValue() * 0.98 + outload * 0.02, 101.0));
|
upSpeed.setValue(Math.min(upSpeed.getValue().doubleValue() * 0.98 + outload * 0.02, 101.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
double inload = monitor.getInSpeed() * 100.0 / monitor.getInSpeedMax();
|
double inload = monitor.getInSpeed() * 100.0 / monitor.getInSpeedMax();
|
||||||
if (Double.isFinite(inload)) {
|
|
||||||
downSpeedText.setLabel(KLALBUtils.convertIBUint(monitor.getInSpeed()) + "/s");
|
downSpeedText.setLabel(KLALBUtils.convertIBUint(monitor.getInSpeed()) + "/s");
|
||||||
|
if (Double.isFinite(inload)) {
|
||||||
downSpeed.setValue(Math.min(downSpeed.getValue().doubleValue() * 0.98 + inload * 0.02, 101.0));
|
downSpeed.setValue(Math.min(downSpeed.getValue().doubleValue() * 0.98 + inload * 0.02, 101.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
double outPPSload = monitor.getOutPPS() * 100.0 / monitor.getOutPPSMax();
|
double outPPSload = monitor.getOutPPS() * 100.0 / monitor.getOutPPSMax();
|
||||||
if (Double.isFinite(outPPSload)) {
|
|
||||||
upPPSText.setLabel(KLALBUtils.convertUintDefalut(monitor.getOutPPS()) + "PPS");
|
upPPSText.setLabel(KLALBUtils.convertUintDefalut(monitor.getOutPPS()) + "PPS");
|
||||||
|
if (Double.isFinite(outPPSload)) {
|
||||||
upPPS.setValue(Math.min(upPPS.getValue().doubleValue() * 0.98 + outPPSload * 0.02, 101.0));
|
upPPS.setValue(Math.min(upPPS.getValue().doubleValue() * 0.98 + outPPSload * 0.02, 101.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
double inPPSload = monitor.getInPPS() * 100.0 / monitor.getInPPSMax();
|
double inPPSload = monitor.getInPPS() * 100.0 / monitor.getInPPSMax();
|
||||||
if (Double.isFinite(inPPSload)) {
|
|
||||||
downPPSText.setLabel(KLALBUtils.convertUintDefalut(monitor.getInPPS()) + "PPS");
|
downPPSText.setLabel(KLALBUtils.convertUintDefalut(monitor.getInPPS()) + "PPS");
|
||||||
|
if (Double.isFinite(inPPSload)) {
|
||||||
downPPS.setValue(Math.min(downPPS.getValue().doubleValue() * 0.98 + inPPSload * 0.02, 101.0));
|
downPPS.setValue(Math.min(downPPS.getValue().doubleValue() * 0.98 + inPPSload * 0.02, 101.0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -177,7 +177,7 @@ public class SpeedAndTrafficMonitorDataImpl extends MonitorDataImpl implements S
|
|||||||
inPPSEvaluator.markDirty();
|
inPPSEvaluator.markDirty();
|
||||||
}
|
}
|
||||||
long time=System.nanoTime();
|
long time=System.nanoTime();
|
||||||
if(time-stime>timewindowmax) {
|
if(time-stime>timewindow) {
|
||||||
stime=time;
|
stime=time;
|
||||||
long os=uploadBandwidth.calculateBandwidth(timewindowmax);
|
long os=uploadBandwidth.calculateBandwidth(timewindowmax);
|
||||||
long is=downloadBandwidth.calculateBandwidth(timewindowmax);
|
long is=downloadBandwidth.calculateBandwidth(timewindowmax);
|
||||||
|
|||||||
@@ -77,11 +77,9 @@ public class Kperf implements Runnable{
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
KLALBPacket testPacket=null;
|
private KLALBPacket testPacket=null;
|
||||||
|
|
||||||
private long starttime=System.nanoTime();
|
private long starttime=System.nanoTime();
|
||||||
long totaltime=1000;
|
|
||||||
long usedtime=0;
|
|
||||||
|
|
||||||
|
|
||||||
private ScheduledFuture<?> monitorUpdater;
|
private ScheduledFuture<?> monitorUpdater;
|
||||||
@@ -121,9 +119,9 @@ public class Kperf implements Runnable{
|
|||||||
});
|
});
|
||||||
Thread tc = ThreadTool.makeVDaemonThreadIfSupport("测速控制线程", () -> {
|
Thread tc = ThreadTool.makeVDaemonThreadIfSupport("测速控制线程", () -> {
|
||||||
final int wtimes=5;
|
final int wtimes=5;
|
||||||
totaltime=(20*1000*1+20*1000*4+wtimes*1000*5)*1000000L;
|
|
||||||
starttime=System.nanoTime();
|
|
||||||
System.out.println("测速开始!");
|
System.out.println("测速开始!");
|
||||||
|
if(reports!=null)
|
||||||
|
reports.updateDial(monitor);
|
||||||
try {
|
try {
|
||||||
long startTime;
|
long startTime;
|
||||||
|
|
||||||
@@ -132,7 +130,6 @@ public class Kperf implements Runnable{
|
|||||||
startTime=System.nanoTime();
|
startTime=System.nanoTime();
|
||||||
for (int i = 0; i < 20; i++) {
|
for (int i = 0; i < 20; i++) {
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
updateUsedTime();
|
|
||||||
}
|
}
|
||||||
System.out.printf("上传延迟 最小:%.3fms 平均:%.3fms 抖动:%.3fms\n",monitor.getOutDelay()/1000000.0,monitor.getOutDelay()/1000000.0,monitor.getOutJitter()/1000000.0);
|
System.out.printf("上传延迟 最小:%.3fms 平均:%.3fms 抖动:%.3fms\n",monitor.getOutDelay()/1000000.0,monitor.getOutDelay()/1000000.0,monitor.getOutJitter()/1000000.0);
|
||||||
System.out.printf("下载延迟 最小:%.3fms 平均:%.3fms 抖动:%.3fms\n",monitor.getInDelay()/1000000.0,monitor.getInDelay()/1000000.0,monitor.getInJitter()/1000000.0);
|
System.out.printf("下载延迟 最小:%.3fms 平均:%.3fms 抖动:%.3fms\n",monitor.getInDelay()/1000000.0,monitor.getInDelay()/1000000.0,monitor.getInJitter()/1000000.0);
|
||||||
@@ -298,9 +295,6 @@ public class Kperf implements Runnable{
|
|||||||
|
|
||||||
|
|
||||||
tlock.unpark();
|
tlock.unpark();
|
||||||
updateUsedTime() ;
|
|
||||||
if(reports!=null)
|
|
||||||
reports.updateDial(monitor);
|
|
||||||
break;
|
break;
|
||||||
case KLALBPacket.TESTN:
|
case KLALBPacket.TESTN:
|
||||||
break;
|
break;
|
||||||
@@ -325,12 +319,7 @@ public class Kperf implements Runnable{
|
|||||||
tcc.interrupt();
|
tcc.interrupt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void updateUsedTime() {
|
|
||||||
usedtime=System.nanoTime()-starttime;
|
|
||||||
//System.out.println(usedtime+" "+totaltime);
|
|
||||||
if(reports!=null)
|
|
||||||
reports.testProcess(usedtime/(double)totaltime);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package org.kne.cloud.network.perf;
|
|||||||
import org.kne.cloud.network.monitor.SpeedAndTrafficAndDelayMonitorDataImpl;
|
import org.kne.cloud.network.monitor.SpeedAndTrafficAndDelayMonitorDataImpl;
|
||||||
|
|
||||||
public interface KperfReports {
|
public interface KperfReports {
|
||||||
public void testProcess(double d);
|
|
||||||
public void testFinish();
|
public void testFinish();
|
||||||
public void updateDial(SpeedAndTrafficAndDelayMonitorDataImpl mdt);
|
public void updateDial(SpeedAndTrafficAndDelayMonitorDataImpl mdt);
|
||||||
public void uploadSpeedFinish(long uploadSpeed);
|
public void uploadSpeedFinish(long uploadSpeed);
|
||||||
|
|||||||
@@ -1,73 +1,87 @@
|
|||||||
package org.kne.cloud.network.perf;
|
package org.kne.cloud.network.perf;
|
||||||
|
|
||||||
import java.io.DataInput;
|
|
||||||
import java.io.DataInputStream;
|
|
||||||
import java.io.DataOutput;
|
|
||||||
import java.io.DataOutputStream;
|
|
||||||
import java.io.EOFException;
|
import java.io.EOFException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.channels.ReadableByteChannel;
|
import java.nio.channels.ReadableByteChannel;
|
||||||
import java.nio.channels.WritableByteChannel;
|
import java.nio.channels.WritableByteChannel;
|
||||||
|
|
||||||
import org.kne.cloud.network.ByteBufferAllocator;
|
|
||||||
import org.kne.cloud.network.NetworkPacket;
|
import org.kne.cloud.network.NetworkPacket;
|
||||||
import org.kne.cloud.network.klalb.KLALBPacket;
|
import org.kne.cloud.network.klalb.KLALBPacket;
|
||||||
|
|
||||||
public class TESTNPacket extends KLALBPacket {
|
public class TESTNPacket extends KLALBPacket {
|
||||||
|
|
||||||
private static final int HEADER_LENGTH=3;
|
private static final int HEADER_LENGTH = 3;
|
||||||
|
|
||||||
private ByteBuffer dataBuffer;//=NetworkPacket.databufferpool_65535.borrow();
|
// 核心:一个线程私有的 65535 字节大缓存,生命周期与线程同寿
|
||||||
|
private static final ThreadLocal<ByteBuffer> BENCH_BUFFER =
|
||||||
|
ThreadLocal.withInitial(() -> ByteBuffer.allocate(65535));
|
||||||
|
|
||||||
|
// 不再持有 ByteBuffer,只记录长度
|
||||||
|
private int payloadLength;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getTotalLength() {
|
public long getTotalLength() {
|
||||||
return HEADER_LENGTH+dataBuffer.limit();
|
return HEADER_LENGTH + payloadLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 发送端构造
|
||||||
public TESTNPacket(int limit) {
|
public TESTNPacket(int limit) {
|
||||||
super(TESTN,HEADER_LENGTH);
|
super(TESTN, HEADER_LENGTH);
|
||||||
//dataBuffer.limit(limit);
|
this.payloadLength = limit;
|
||||||
dataBuffer=NetworkPacket.bufferAllocator.allocate(limit);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 接收端构造(从已有的 ByteBuffer 解析)
|
||||||
|
public TESTNPacket(ByteBuffer bb) {
|
||||||
public ByteBuffer getDataBuffer() {
|
super(bb, HEADER_LENGTH);
|
||||||
return dataBuffer;
|
this.payloadLength = klalbHeader.getChar(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getPayloadLength() {
|
||||||
|
return payloadLength;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeToChannel(WritableByteChannel dto) throws IOException {
|
public void writeToChannel(WritableByteChannel dto) throws IOException {
|
||||||
klalbHeader.putChar(1, (char) dataBuffer.limit());
|
// 1. 写入包头(包含 payloadLength)
|
||||||
|
klalbHeader.putChar(1, (char) payloadLength);
|
||||||
super.writeToChannel(dto);
|
super.writeToChannel(dto);
|
||||||
//System.out.println(dataBuffer);
|
|
||||||
dto.write(dataBuffer.slice(0, dataBuffer.limit()));
|
// 2. 如果有 payload,借用 ThreadLocal 缓冲区并写出去
|
||||||
|
if (payloadLength > 0) {
|
||||||
|
ByteBuffer buf = BENCH_BUFFER.get();
|
||||||
|
buf.clear();
|
||||||
|
buf.limit(payloadLength); // 限制到指定长度
|
||||||
|
|
||||||
|
// 直接写出,里面的数据是上一次残留的或者全 0,对于烤机无所谓
|
||||||
|
dto.write(buf);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void readFromChannel(ReadableByteChannel din) throws IOException {
|
public void readFromChannel(ReadableByteChannel din) throws IOException {
|
||||||
|
// 1. 读包头
|
||||||
super.readFromChannel(din);
|
super.readFromChannel(din);
|
||||||
int limit=klalbHeader.getChar(1);
|
this.payloadLength = klalbHeader.getChar(1);
|
||||||
dataBuffer=NetworkPacket.bufferAllocator.allocate(limit);
|
|
||||||
while(dataBuffer.hasRemaining()){
|
// 2. 如果有 payload,借用缓冲区接收数据
|
||||||
if(din.read(dataBuffer)==-1) {
|
if (payloadLength > 0) {
|
||||||
throw new EOFException();
|
ByteBuffer buf = BENCH_BUFFER.get();
|
||||||
|
buf.clear();
|
||||||
|
buf.limit(payloadLength);
|
||||||
|
|
||||||
|
// 循环读满(使用你现有的 KNEChannels 工具或手动循环)
|
||||||
|
while (buf.hasRemaining()) {
|
||||||
|
if (din.read(buf) == -1) {
|
||||||
|
throw new EOFException("连接提前断开,未能读满 TESTN 载荷");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
// 数据读完直接丢弃,缓冲区不保存,下一次 clear() 时会自动覆盖
|
||||||
}
|
}
|
||||||
dataBuffer.flip();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public TESTNPacket(ByteBuffer bb) {
|
|
||||||
super(bb,HEADER_LENGTH);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "TESTN["+dataBuffer.limit()+"]";
|
return "TESTN[" + payloadLength + "]";
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user