UDT协议-基于UDP的可靠数据传输协议的实现分析(3)-包结构说明

阅读更多

udt的包结构

1. 数据包,基本结构如下:
 

   0                   1                   2                   3
   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |0|                     Packet Sequence Number                  |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                                                               |
   ~                   Application Data Payload                    ~
   |                                                               |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+


第1位为0表示是数据包,后续31位为序号,表示范围是0-(2^31 - 1)

udt-java里数据包的类DataPacket,实际的属性如下:

    // 包序号, 4字节的低31位表示
    private long packetSequenceNumber;
    // 消息号, 4字节
    private long messageNumber;
    // 时间戳, 4字节
    private long timeStamp;
    // 对等端链接的ID, 4字节
    private long destinationID;
    // 实际数据长度(字节)
    private int dataLength;
    // 实际的数据
    private byte[] data ;

 

2. 控制包,基本结构如下:

   0                   1                   2                   3
   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |1|type |        Reserved       |          ACK Seq. No.         |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                                                               |
   ~                  Control Information Field                    ~
   |                                                               |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+



第1位为1表示是控制包,接下来的三位表示控制包类型。

UDT的ACK包使用子序号,每个ACK/ACK2包使用一个16位的序列号,使用16-31位表示,能够
表示的序号范围是0 - (2^16 - 1)。其他的控制包无子序号。

udt-java中所有控制包的基类ControlPacket,都存在的header(16字节)

        // 4字节
        System.arraycopy(PacketUtil.encodeControlPacketType(controlPacketType), 0, res, 0, 4);
        // 4字节,额外信息
        System.arraycopy(PacketUtil.encode(getAdditionalInfo()), 0, res, 4, 4);
        // 4字节,时间戳
        System.arraycopy(PacketUtil.encode(timeStamp), 0, res, 8, 4);
        // 4字节,对端的链接的id
        System.arraycopy(PacketUtil.encode(destinationID), 0, res, 12, 4);

 

udt-java中的控制包类型的定义:

   

public static enum ControlPacketType {
       
        CONNECTION_HANDSHAKE,
        KEEP_ALIVE,
        ACK,
        NAK,
        UNUNSED_1,
        SHUTDOWN,
        ACK2,
        MESSAGE_DROP_REQUEST,
        UNUNSED_2,
        UNUNSED_3,
        UNUNSED_4,
        UNUNSED_5,
        UNUNSED_6,
        UNUNSED_7,
        UNUNSED_8,
        USER_DEFINED,
       
    }

 


控制包的类型及具体的包结构如下:

TYPE 000: Protocol Connection Handshake 握手控制包
          Control Info:
          1) 32 bits: UDT version
          2) 32 bits: initial sequence number
          3) 32 bits: MSS (bytes)
          4) 32 bits: maximum flow window size (bytes)

udt-java中握手包ConnectionHandshake类的主要属性:

   

// udt版本 4字节
    private long udtVersion=4;
    // socket的类型,4字节
    private long socketType= SOCKET_TYPE_DGRAM; //stream or dgram
    // 初始序列号 4字节
    private long initialSeqNo = 0;
    // 包大小-MSS大小 4字节
    private long packetSize;
    // 流量窗口大小 4字节
    private long maxFlowWndSize;
    // 链接类型 4字节
    private long connectionType = CONNECTION_TYPE_REGULAR;//regular or rendezvous mode
    // socket id 4字节
    private long socketID;
    // cookie信息
    private long cookie=0;
    //address of the UDP socket
    private InetAddress address;

 

TYPE 001: Keep-alive
          Control Info: None
udt-java中的KeepAlive类
 
TYPE 010: Acknowledgement (ACK)
          bits 16-31: ACK sequence number
          Control Info:
          1) 32 bits: The packet sequence number to which (excluding)
                      all the previous packets have been received
          2) 32 bits: RTT (microseconds)
          3) 32 bits: RTT variance, or RTTVar (microseconds)
          4) 32 bits: Flow window size (number of packets)
          5) 32 bits: Estimated link capacity (number of packets per
                         second)
udt-java中Acknowledgement类:
   

 //the ack sequence number子序列
    private long ackSequenceNumber ;
    //the packet sequence number to which all the previous packets have been received (excluding)
    // 接收到的包的序号
    private long ackNumber ;
    //round-trip time in microseconds(RTT)
    private long roundTripTime;
    // RTT variance
    private long roundTripTimeVariance;
    //Available buffer size (in bytes)
    private long bufferSize;
    //packet receivind rate in number of packets per second
    private long pktArrivalSpeed;
    //estimated link capacity in number of packets per second
    private long estimatedLinkCapacity;

 

TYPE 011: Negative Acknowledgement (NAK)
          Control Info:
          32-bit integer array of compressed loss information
udt-java中的NegativeAcknowledgement类,丢包的控制包:

    //after decoding this contains the lost sequence numbers
    ListlostSequenceNumbers;

 

TYPE 100: Reserved.
        This type of control message is reserved for congestion
        warning that can be sent from the receiver to the sender. A
        congestion warning can be triggered by ECN, or a
        measurement of increasing trend in the packet delay.

TYPE 101: Shutdown
             Control Info: None
udt-java中的Shutdown类

TYPE 110: Acknowledgement of Acknowledgement (ACK2)
          bits 16-31: ACK sequence number
          Control Info: None
对确认包的确认,udt-java中的类Acknowledgment2,包含的属性

    //the ack sequence number
    private long ackSequenceNumber ;

 

TYPE 111: Explained by bits 4 - 15, reserved for future use.

3. udt-java中的packet的解析类PacketFactory

   

 public static UDTPacket createPacket(byte[]encodedData,int length)throws IOException{
        // 判断是否是控制包
        boolean isControl=(encodedData[0]&128) !=0 ;
        // 解析控制包
        if(isControl)return createControlPacket(encodedData,length);
        // 返回数据包
        return new DataPacket(encodedData,length);
    }

 

你可能感兴趣的:(tcp,udp,udt,通信,服务器编程)