MUTP实现功能
通过这个练手,来对可靠传输有更深的认识,当然肯定不会有tcp那么细节化,前辈们的设计当然是相当的完备
- 三次牵手
- 超时重传
- 处理丢包
- 滑动窗口
目录结构
数据模拟
这里只简单的模拟部分,也可以用byte[4]来实现
- DataPacketHeader
private boolean ACK;
private boolean SYN;
private int ackNum;
private int seqNum;
private int packetLength;
- DataPacket
private DataPacketHeader header;
private byte[] buf ;
private int windowSize = 5;
DataPacket类是MUTP传输的基本数据结构
- 创建包的工厂(DataPacketFactory)
package main.common;
/**
* Created by liqiushi on 2017/11/23.
*/
public class DataPacketFactory {
public static final String SYN_ONLY = "SYN_ONLY";
public static final String SYN_ACK = "SYN_ACK";
public static final String ACK_ONLY = "ACK_ONLY";
public static final String DATA_ONLY = "DATA_ONLY";
private DataPacketFactory(){
}
public static DataPacket getInstance(String pType){
/**
* 这里开始连接都ack、seq设为为1
*/
switch(pType){
//发出的SYN连接请求
case SYN_ONLY:
return new DataPacket(new DataPacketHeader(false,true,-1,0));
//发出的SYN和ACK,确认发送端的连接请求 ACK ack = 发送端seq+1
case SYN_ACK:
return new DataPacket(new DataPacketHeader(true,true,1,0));
//最终发送ACK ack = 接收端seq+1 确认连接
case ACK_ONLY:
return new DataPacket(new DataPacketHeader(true,false,1,1));
case DATA_ONLY:
return new DataPacket(new DataPacketHeader(false,false,-1,0));
default:
break;
}
return null;
}
}