解决了由ByteBuffer使用不当引发的bug。新结构的代码已可以正常运行

This commit is contained in:
free will committed 2021-10-21 18:41:50 +08:00
1 parent 5eed1d942b
commit e113cc92d3
6 files changed
+11 -4

No files matched your search

@@ -68,12 +68,12 @@ public class Ip2MinTask implements Runnable{
}
assert pendingProcessIpPacket != null;
pendingProcessIpPacket.flip();
System.out.println("debug ip2min len: "+pendingProcessIpPacket.limit()); // 测试
// 2.先将ip头放入buff
ipAndData.put(allocatedAddressBytes);
// 再将ip包放入buff(视情况决定是否加密)
int readLen=pendingProcessIpPacket.position();
int readLen=pendingProcessIpPacket.limit();
byte[] ipPackageData = new byte[readLen];
pendingProcessIpPacket.get(ipPackageData);
int encryptedLen=readLen;
@@ -10,6 +10,7 @@ package com.pkusz.min_vpn_client.services.Runnables;
import com.pkusz.min_vpn_client.services.ByteBufferPool;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.concurrent.ConcurrentLinkedQueue;
import packet.CPacket;
@@ -47,8 +48,10 @@ public class Min2IpTask implements Runnable{
break;
}
// 2.读取负载字段
assert pendingProcessCPacket != null;
System.out.println("debug min2ip len: "+pendingProcessCPacket.payload.getValue().length); // 测试
// 2.读取负载字段
byte[] rawIpDataBytes = pendingProcessCPacket.payload.getValue();
// 解密
ByteBuffer ipData= ByteBufferPool.acquire();
@@ -40,6 +40,7 @@ public class ReadTunTask implements Runnable{
// 读包入队
int readBytes = tunInput.read(bufferToNetwork);
if(readBytes>0){
System.out.println("debug read tun len: "+readBytes); // 测试
dataSent=true;
bufferToNetwork.flip();
readTunQueue.offer(bufferToNetwork);
@@ -40,6 +40,7 @@ public class ReceiveTask implements Runnable{
continue;
}
if(cPacket!=null){
System.out.println("debug face receive: "+cPacket.payload.getValue().length); // 测试
writeTunCPackets.offer(cPacket);
dataReceive=true;
}else{
@@ -36,6 +36,7 @@ public class SendTask implements Runnable{
CPacket cPacket = readTunCPackets.poll();
// 发包
if(cPacket!=null){
System.out.println("debug face send: "+cPacket.payload.getValue().length); //测试
try {
face.sendCPacket(cPacket);
}catch (LogicFaceException e){
@@ -12,6 +12,7 @@ import com.pkusz.min_vpn_client.services.ByteBufferPool;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Arrays;
import java.util.concurrent.ConcurrentLinkedQueue;
@@ -35,7 +36,7 @@ public class WriteTunTask implements Runnable{
ByteBuffer bufferFromNetwork = writeTunQueue.poll();
// 写包
if(bufferFromNetwork!=null){
bufferFromNetwork.flip();
System.out.println("debug tun write len-limit: "+bufferFromNetwork.limit());// 测试
while (bufferFromNetwork.hasRemaining()) {
tunOutput.write(bufferFromNetwork);
}