예제 #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);
        }