Exemplo n.º 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)ControlPacket.ControlPacketType.CONNECTION_HANDSHAKE == pktType)
            {
                packet = new ConnectionHandshake(controlInformation);
            }
            //TYPE 0001:1
            else if ((int)ControlPacket.ControlPacketType.KEEP_ALIVE == pktType)
            {
                packet = new KeepAlive();
            }
            //TYPE 0010:2
            else if ((int)ControlPacket.ControlPacketType.ACK == pktType)
            {
                packet = new Acknowledgement(additionalInfo, controlInformation);
            }
            //TYPE 0011:3
            else if ((int)ControlPacket.ControlPacketType.NAK == pktType)
            {
                packet = new NegativeAcknowledgement(controlInformation);
            }
            //TYPE 0101:5
            else if ((int)ControlPacket.ControlPacketType.SHUTDOWN == pktType)
            {
                packet = new Shutdown();
            }
            //TYPE 0110:6
            else if ((int)ControlPacket.ControlPacketType.ACK2 == pktType)
            {
                packet = new Acknowledgment2(additionalInfo, controlInformation);
            }
            //TYPE 0111:7
            else if ((int)ControlPacket.ControlPacketType.MESSAGE_DROP_REQUEST == pktType)
            {
                packet = new MessageDropRequest(controlInformation);
            }
            //TYPE 1111:8
            else if ((int)ControlPacket.ControlPacketType.USER_DEFINED == pktType)
            {
                packet = new UserDefined(controlInformation);
            }

            if (packet != null)
            {
                packet.setTimeStamp(timeStamp);
                packet.setDestinationID(destID);
            }
            return(packet);
        }
        public override bool equals(Object obj)
        {
            if (this == obj)
            {
                return(true);
            }
            if (!base.equals(obj))
            {
                return(false);
            }
            if (this.GetType() != obj.GetType())
            {
                return(false);
            }
            NegativeAcknowledgement other = (NegativeAcknowledgement)obj;

            List <int> thisLost  = null;
            List <int> otherLost = null;

            //compare the loss info
            if (lostSequenceNumbers != null)
            {
                thisLost = lostSequenceNumbers;
            }
            else
            {
                if (lossInfo.Count > 0)
                {
                    byte[] loss = new byte[lossInfo.Count];
                    for (int i = 0; i < lossInfo.Count; i++)
                    {
                        loss[i] = lossInfo[i];
                    }
                    thisLost = decode(loss);
                }
            }
            if (other.lostSequenceNumbers != null)
            {
                otherLost = other.lostSequenceNumbers;
            }
            else
            {
                if (other.lossInfo.Count > 0)
                {
                    byte[] loss = new byte[other.lossInfo.Count];
                    for (int i = 0; i < other.lossInfo.Count; i++)
                    {
                        loss[i] = other.lossInfo[i];
                    }
                    otherLost = other.decode(loss);
                }
            }
            if (!thisLost.Equals(otherLost))
            {
                return(false);
            }

            return(true);
        }