add test: TcpTransport

This commit is contained in:
free will committed 2021-05-10 16:24:20 +08:00
1 parent 6393c7985e
commit cfcd4cbe35
2 files changed
+124 -5

No files matched your search

+12 -5
View File
@@ -13,6 +13,7 @@ import java.io.IOException;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
@@ -98,7 +99,7 @@ public class StreamTransport extends Transport implements ITransport{
}
int pktTypeLen=pktType.size();
int pktLen=TLV.readVarNumber(buf,new VlInt(pktTypeLen)).getVlIntValue2Int();
int totalPktLen=pktTypeLen+new VlInt(pktLen).getVlIntValue2Int()+pktLen;
int totalPktLen=pktTypeLen+new VlInt(pktLen).size()+pktLen;
if(bufLen>=totalPktLen){
LpPacket lpPacket=this.parseByteArray2LpPacket(
ByteHelper.getLenBytes(buf,0,totalPktLen));
@@ -123,6 +124,7 @@ public class StreamTransport extends Transport implements ITransport{
* @return
*/
private LpPacket doReceive() throws LogicFaceException {
// System.out.println("doReceive param: "+ Arrays.toString(ByteHelper.getLenBytes(this.recvBuf,0, (int) this.recvLen)));
Pair<Long,LpPacket> packetPair=this.readPktAndDeal(
ByteHelper.getLenBytes(this.recvBuf,0, (int) this.recvLen),this.recvLen);
long pktLen=packetPair.getKey();
@@ -147,16 +149,21 @@ public class StreamTransport extends Transport implements ITransport{
public LpPacket receive() throws LogicFaceException {
try {
while (true) {
LpPacket lpPacket = this.receive();
LpPacket lpPacket = this.doReceive();
if (lpPacket != null) {
return lpPacket;
}
int recvRet = this.channel.read(ByteBuffer.wrap(ByteHelper.
getLenBytes(this.recvBuf, (int) this.recvLen, (int) (this.recvBuf.length - this.recvLen))));
// 从channel中接收数据
byte[] cacheBytes=new byte[(int) (this.recvBuf.length - this.recvLen)];
int recvRet = this.channel.read(ByteBuffer.wrap(cacheBytes));
// System.out.println("receive size: "+recvRet);
if(recvRet<0){
return null;
}
this.recvLen+=recvRet;
// System.out.println("receive res:"+Arrays.toString(
// ByteHelper.getLenBytes(cacheBytes,0,recvRet)));
System.arraycopy(cacheBytes,0,this.recvBuf,(int) this.recvLen,recvRet);
this.recvLen += recvRet;
}
} catch (IOException e) {
throw new LogicFaceException("StreamTransport.receive: "+e.getMessage());