public class PacketHead {
        private byte[] rawData;
        public const int HeadLength = 20;
        public const int MaxPayloadLength = 0xa00000;

        public PacketHead() {
            rawData = new byte[HeadLength];
        }

        public PacketHead(PacketType type) {
            rawData = new byte[HeadLength];
            PacketType = type;
        }

        public PacketHead(byte[] source) {
            if(source.Length == HeadLength) {
                rawData = source;
            } else {
                throw new CMPacketException(Properties.Resources.DataLengthError);
            }
            if (PacketType != PacketType.Notify && PacketType != PacketType.Req && PacketType != PacketType.Resp && PacketType != PacketType.AckFail && PacketType != PacketType.AckSuccess) {
                throw new CMPacketException(Properties.Resources.PacketTypeError);
            }
            if (PayloadLength < 0 || PayloadLength > MaxPayloadLength) {
                throw new CMPacketException(Properties.Resources.DataLengthError);
            }
        }

        public byte[] RawData {
            get {
                return rawData;
            }
            set {
                rawData = value;
            }
        }

        public PacketType PacketType {
            get {
                byte[] pkttype = new byte[4];
                Array.Copy(rawData, 0, pkttype, 0, 4);
                int p = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(pkttype, 0));
                return (PacketType)p;
            }
            set {
                int p = (int)value;
                byte[] pkttype = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(p));
                Array.Copy(pkttype, 0, rawData, 0, 4);
            }
        }

        public IPAddress SourceIp {
            get {
                byte[] addr = new byte[4];
                Array.Copy(rawData, 4, addr, 0, 4);
                int p = BitConverter.ToInt32(addr, 0);
                //p = IPAddress.NetworkToHostOrder(p);
                addr = BitConverter.GetBytes(p);
                IPAddress ip = new IPAddress(addr);
                return ip;
            }
            set {
                byte[] addr = value.GetAddressBytes();
                int p = BitConverter.ToInt32(addr, 0);
                //p = IPAddress.HostToNetworkOrder(p);
                addr = BitConverter.GetBytes(p);
                Array.Copy(addr, 3, rawData, 7, 1);
            }
        }

        public IPAddress DestIp {
            get {
                byte[] addr = new byte[4];
                Array.Copy(rawData, 8, addr, 0, 4);
                int p = BitConverter.ToInt32(addr, 0);
                //p = IPAddress.NetworkToHostOrder(p);
                addr = BitConverter.GetBytes(p);
                IPAddress ip = new IPAddress(addr);
                return ip;
            }
            set {
                byte[] addr = value.GetAddressBytes();
                int p = BitConverter.ToInt32(addr, 0);
                //p = IPAddress.HostToNetworkOrder(p);
                addr = BitConverter.GetBytes(p);
                Array.Copy(addr, 3, rawData, 11, 1);
            }
        }

        public int PktId {
            get {
                byte[] pktid = new byte[4];
                Array.Copy(rawData, 12, pktid, 0, 4);
                int p = BitConverter.ToInt32(pktid, 0);
                p = IPAddress.NetworkToHostOrder(p);
                return p;
            }
            set {
                int p = IPAddress.HostToNetworkOrder(value);
                byte[] pktid = BitConverter.GetBytes(p);
                Array.Copy(pktid, 0, rawData, 12, 4);
            }
        }

        public int PayloadLength {
            get {
                byte[] pktid = new byte[4];
                Array.Copy(rawData, 16, pktid, 0, 4);
                int p = BitConverter.ToInt32(pktid, 0);
                p = IPAddress.NetworkToHostOrder(p);
                return p;
            }
            set {
                int p = IPAddress.HostToNetworkOrder(value);
                byte[] pktid = BitConverter.GetBytes(p);
                Array.Copy(pktid, 0, rawData, 16, 4);
            }
        }

        public static PacketHead MakeHead(Stream source) {
            byte[] buf = new byte[PacketHead.HeadLength];
            //DateTime begin = DateTime.Now;
            int nByte = 0;
            try {
                nByte = source.Read(buf, 0, PacketHead.HeadLength);
            } catch(IOException) {
                throw new CMIOException(Properties.Resources.ReadPacketHeadTimeout);
            }
            if(nByte != PacketHead.HeadLength) {
                throw new CMPacketException(Properties.Resources.PacketHeadPackingError);
            }
            return new PacketHead(buf);
        }
    }

posted on 2009-03-23 14:59  rudyfung  阅读(163)  评论(0编辑  收藏  举报