Beispiel #1
0
        /**
         * create the right type of control packet based on the packet data
         * @param packetData
         * @return
         */
        public static ControlPacket CreateControlPacket(byte[] encodedData, int length)
        {
            ControlPacket packet = null;

            int  pktType        = PacketUtil.DecodeType(encodedData, 0);
            long additionalInfo = PacketUtil.Decode(encodedData, 4);
            long timeStamp      = PacketUtil.Decode(encodedData, 8);
            long destID         = PacketUtil.Decode(encodedData, 12);

            byte[] controlInformation = new byte[length - 16];
            Array.Copy(encodedData, 16, controlInformation, 0, controlInformation.Length);

            //TYPE 0000:0
            if ((int)ControlPacketType.CONNECTION_HANDSHAKE == pktType)
            {
                packet = new ConnectionHandshake(controlInformation);
            }
            //TYPE 0001:1
            else if ((int)ControlPacketType.KEEP_ALIVE == pktType)
            {
                packet = new KeepAlive();
            }
            //TYPE 0010:2
            else if ((int)ControlPacketType.ACK == pktType)
            {
                packet = new Acknowledgement(additionalInfo, controlInformation);
            }
            //TYPE 0011:3
            else if ((int)ControlPacketType.NAK == pktType)
            {
                packet = new NegativeAcknowledgement(controlInformation);
            }
            //TYPE 0101:5
            else if ((int)ControlPacketType.SHUTDOWN == pktType)
            {
                packet = new Shutdown();
            }
            //TYPE 0110:6
            else if ((int)ControlPacketType.ACK2 == pktType)
            {
                packet = new Acknowledgment2(additionalInfo, controlInformation);
            }
            //TYPE 0111:7
            else if ((int)ControlPacketType.MESSAGE_DROP_REQUEST == pktType)
            {
                packet = new MessageDropRequest(controlInformation);
            }
            //TYPE 1111:8
            else if ((int)ControlPacketType.USER_DEFINED == pktType)
            {
                packet = new UserDefined(controlInformation);
            }

            if (packet != null)
            {
                packet.TimeStamp     = timeStamp;
                packet.DestinationID = destID;
            }
            return(packet);
        }
Beispiel #2
0
        void Decode(byte[] data)
        {
            udtVersion     = PacketUtil.Decode(data, 0);
            socketType     = PacketUtil.Decode(data, 4);
            initialSeqNo   = PacketUtil.Decode(data, 8);
            packetSize     = PacketUtil.Decode(data, 12);
            maxFlowWndSize = PacketUtil.Decode(data, 16);
            connectionType = PacketUtil.Decode(data, 20);
            socketID       = PacketUtil.Decode(data, 24);
            if (data.Length > 28)
            {
                cookie = PacketUtil.Decode(data, 28);
            }

            if (data.Length > 32)
            {
                //IP6
                peerIP[0] = PacketUtil.Decode(data, 32);

                peerIP[1] = PacketUtil.Decode(data, 36);

                peerIP[2] = PacketUtil.Decode(data, 40);

                peerIP[3] = PacketUtil.Decode(data, 44);
            }
        }
Beispiel #3
0
        //public long getMessageNumber() {
        //    return messageNumber;
        //}

        //public void setMessageNumber(long messageNumber) {
        //    this.messageNumber = messageNumber;
        //}



        //public long getTimeStamp() {
        //    return timeStamp;
        //}


        //public void setTimeStamp(long timeStamp) {
        //    this.timeStamp = timeStamp;
        //}



        //public long getDestinationID() {
        //    return destinationID;
        //}

        //public void setDestinationID(long destinationID) {
        //    this.destinationID = destinationID;
        //}


        /**
         * return the header according to specification p.5
         * @return
         */
        byte[] GetHeader()
        {
            byte[] res = new byte[16];
            Array.Copy(PacketUtil.EncodeControlPacketType(controlPacketType), 0, res, 0, 4);
            Array.Copy(PacketUtil.Encode(GetAdditionalInfo()), 0, res, 4, 4);
            Array.Copy(PacketUtil.Encode(timeStamp), 0, res, 8, 4);
            Array.Copy(PacketUtil.Encode(destinationID), 0, res, 12, 4);
            return(res);
        }
Beispiel #4
0
 void Decode(byte[] encodedData, int length)
 {
     packetSequenceNumber = PacketUtil.Decode(encodedData, 0);
     messageNumber        = PacketUtil.Decode(encodedData, 4);
     timeStamp            = PacketUtil.Decode(encodedData, 8);
     destinationID        = PacketUtil.Decode(encodedData, 12);
     data = new byte[length - 16];
     Array.Copy(encodedData, 16, data, 0, data.Length);
 }
Beispiel #5
0
 /**
  * add a single lost packet number
  * @param singleSequenceNumber
  */
 public void AddLossInfo(long singleSequenceNumber)
 {
     byte[] enc = PacketUtil.EncodeSetHighest(false, singleSequenceNumber);
     try
     {
         lossInfo.Write(enc, 0, enc.Length);
     }
     catch (IOException ignore) { }
 }
Beispiel #6
0
        /*
         * aplivation data
         * @param
         */

        //public void setData(byte[] data) {
        //    this.data = data;
        //}


        //public long getPacketSequenceNumber() {
        //    return this.packetSequenceNumber;
        //}

        //public void setPacketSequenceNumber(long sequenceNumber) {
        //    this.packetSequenceNumber = sequenceNumber;
        //}



        ////public long getMessageNumber() {
        ////    return this.messageNumber;
        ////}


        ////public void setMessageNumber(long messageNumber) {
        ////    this.messageNumber = messageNumber;
        ////}


        //public long getDestinationID() {
        //    return this.destinationID;
        //}


        //public long getTimeStamp() {
        //    return this.timeStamp;
        //}


        //public void setDestinationID(long destinationID) {
        //    this.destinationID = destinationID;
        //}


        //public void setTimeStamp(long timeStamp) {
        //    this.timeStamp = timeStamp;
        //}

        /**
         * complete header+data packet for transmission
         */

        public byte[] GetEncoded()
        {
            //header.length is 16
            byte[] result = new byte[16 + data.Length];
            Array.Copy(PacketUtil.Encode(packetSequenceNumber), 0, result, 0, 4);
            Array.Copy(PacketUtil.Encode(messageNumber), 0, result, 4, 4);
            Array.Copy(PacketUtil.Encode(timeStamp), 0, result, 8, 4);
            Array.Copy(PacketUtil.Encode(destinationID), 0, result, 12, 4);
            Array.Copy(data, 0, result, 16, data.Length);
            return(result);
        }
Beispiel #7
0
 void DecodeControlInformation(byte[] data)
 {
     ackNumber = PacketUtil.Decode(data, 0);
     if (data.Length > 4)
     {
         roundTripTime         = PacketUtil.Decode(data, 4);
         roundTripTimeVariance = PacketUtil.Decode(data, 8);
         bufferSize            = PacketUtil.Decode(data, 12);
     }
     if (data.Length > 16)
     {
         pktArrivalSpeed       = PacketUtil.Decode(data, 16);
         estimatedLinkCapacity = PacketUtil.Decode(data, 20);
     }
 }
Beispiel #8
0
        //public long getMsgFirstSeqNo()
        //{
        //    return msgFirstSeqNo;
        //}

        //public void setMsgFirstSeqNo(long msgFirstSeqNo)
        //{
        //    this.msgFirstSeqNo = msgFirstSeqNo;
        //}

        //public long getMsgLastSeqNo()
        //{
        //    return msgLastSeqNo;
        //}

        //public void setMsgLastSeqNo(long msgLastSeqNo)
        //{
        //    this.msgLastSeqNo = msgLastSeqNo;
        //}


        public override byte[] EncodeControlInformation()
        {
            try
            {
                MemoryStream bos = new MemoryStream();
                bos.Write(PacketUtil.Encode(msgFirstSeqNo), 0, 8);
                bos.Write(PacketUtil.Encode(msgLastSeqNo), 0, 8);
                return(bos.ToArray());
            }
            catch (Exception e)
            {
                // can't happen
                return(null);
            }
        }
Beispiel #9
0
        //public long getAckSequenceNumber() {
        //    return ackSequenceNumber;
        //}
        //public void setAckSequenceNumber(long ackSequenceNumber) {
        //    this.ackSequenceNumber = ackSequenceNumber;
        //}


        /**
         * get the ack number (the number up to which all packets have been received (excluding))
         * @return
         */
        //public long getAckNumber() {
        //    return ackNumber;
        //}

        ///**
        // * set the ack number (the number up to which all packets have been received (excluding))
        // * @param ackNumber
        // */
        //public void setAckNumber(long ackNumber) {
        //    this.ackNumber = ackNumber;
        //}

        /**
         * get the round trip time (microseconds)
         * @return
         */
        //public long getRoundTripTime() {
        //    return roundTripTime;
        //}
        ///**
        // * set the round trip time (in microseconds)
        // * @param RoundTripTime
        // */
        //public void setRoundTripTime(long RoundTripTime) {
        //    roundTripTime = RoundTripTime;
        //}

        /**
         * set the variance of the round trip time (in microseconds)
         * @param RoundTripTime
         */
        //public void setRoundTripTimeVar(long roundTripTimeVar) {
        //    roundTripTimeVariance = roundTripTimeVar;
        //}

        //public long getRoundTripTimeVar() {
        //    return roundTripTimeVariance;
        //}

        //public long getBufferSize() {
        //    return bufferSize;
        //}

        //public void setBufferSize(long bufferSiZe) {
        //    this.bufferSize = bufferSiZe;
        //}

        //public long getPacketReceiveRate() {
        //    return pktArrivalSpeed;
        //}
        //public void setPacketReceiveRate(long packetReceiveRate) {
        //    this.pktArrivalSpeed = packetReceiveRate;
        //}


        //public long getEstimatedLinkCapacity() {
        //    return estimatedLinkCapacity;
        //}

        //public void setEstimatedLinkCapacity(long estimatedLinkCapacity) {
        //    this.estimatedLinkCapacity = estimatedLinkCapacity;
        //}


        public override byte[] EncodeControlInformation()
        {
            try {
                MemoryStream bos = new MemoryStream();
                bos.Write(PacketUtil.Encode(ackNumber), 0, 8);
                bos.Write(PacketUtil.Encode(roundTripTime), 0, 8);
                bos.Write(PacketUtil.Encode(roundTripTimeVariance), 0, 8);
                bos.Write(PacketUtil.Encode(bufferSize), 0, 8);
                bos.Write(PacketUtil.Encode(pktArrivalSpeed), 0, 8);
                bos.Write(PacketUtil.Encode(estimatedLinkCapacity), 0, 8);

                return(bos.ToArray());
            } catch (Exception e) {
                // can't happen
                return(null);
            }
        }
Beispiel #10
0
 /**
  * add an interval of lost packet numbers
  * @param firstSequenceNumber
  * @param lastSequenceNumber
  */
 public void AddLossInfo(long firstSequenceNumber, long lastSequenceNumber)
 {
     //check if we really need an interval
     if (lastSequenceNumber - firstSequenceNumber == 0)
     {
         AddLossInfo(firstSequenceNumber);
         return;
     }
     //else add an interval
     byte[] enc1 = PacketUtil.EncodeSetHighest(true, firstSequenceNumber);
     byte[] enc2 = PacketUtil.EncodeSetHighest(false, lastSequenceNumber);
     try
     {
         lossInfo.Write(enc1, 0, enc1.Length);
         lossInfo.Write(enc2, 0, enc2.Length);
     }
     catch (IOException ignore) { }
 }
Beispiel #11
0
 public override byte[] EncodeControlInformation()
 {
     try {
         MemoryStream bos    = new MemoryStream(24);
         byte[]       buffer = null;
         buffer = PacketUtil.Encode(udtVersion);
         bos.Write(buffer, 0, buffer.Length);
         buffer = PacketUtil.Encode(socketType);
         bos.Write(buffer, 0, buffer.Length);
         buffer = PacketUtil.Encode(initialSeqNo);
         bos.Write(buffer, 0, buffer.Length);
         buffer = PacketUtil.Encode(packetSize);
         bos.Write(buffer, 0, buffer.Length);
         buffer = PacketUtil.Encode(maxFlowWndSize);
         bos.Write(buffer, 0, buffer.Length);
         buffer = PacketUtil.Encode(connectionType);
         bos.Write(buffer, 0, buffer.Length);
         buffer = PacketUtil.Encode(socketID);
         bos.Write(buffer, 0, buffer.Length);
         buffer = PacketUtil.Encode(cookie);
         bos.Write(buffer, 0, buffer.Length);
         //bos.Write(PacketUtil.encode(socketType));
         //bos.Write(PacketUtil.encode(initialSeqNo));
         //bos.Write(PacketUtil.encode(packetSize));
         //bos.Write(PacketUtil.encode(maxFlowWndSize));
         //bos.Write(PacketUtil.encode(connectionType));
         //bos.Write(PacketUtil.encode(socketID));
         // bos.Write(PacketUtil.encode(cookie));//cd 2018-08-28
         for (int i = 0; i < 4; i++)
         {
             buffer = PacketUtil.Encode(peerIP[i]);
             bos.Write(buffer, 0, buffer.Length);
             //bos.write(PacketUtil.encode(peerIP[i]));
         }
         buffer = bos.ToArray();
         bos.Close();
         return(buffer);
     } catch (Exception e) {
         // can't happen
         return(null);
     }
 }
Beispiel #12
0
 void decode(byte[] data)
 {
     msgFirstSeqNo = PacketUtil.Decode(data, 0);
     msgLastSeqNo  = PacketUtil.Decode(data, 4);
 }