コード例 #1
0
        public INGetRequest Deserialize(byte[] Data, int DataSize)
        {
            INGetRequest rv = null;

            // Explicitly check for test mode
            bool testMode = false;

            if (Data != null && Data.Length >= 4 && DataSize == 4)
            {
                var text = (Encoding.ASCII.GetString(Data, 0, 4) ?? "").Trim().ToUpper();
                testMode = text == "TEST";
            }

            // Test mode always validates
            if (testMode)
            {
                return(this);
            }

            // Must be at least one byte for version
            if (Data == null || Data.Length <= 0 || Data.Length < DataSize || Data[0] != Version)
            {
                return(rv);
            }

            // Must be at exactly two bytes
            if (DataSize != 2)
            {
                return(rv);
            }

            // Checksum for all bytes except last must match last byte
            byte cs = ChecksumSeed;

            for (int i = 0; i < DataSize - 1; i++)
            {
                cs ^= Data[i];
            }
            if (Data[DataSize - 1] != cs)
            {
                return(rv);
            }

            return(this);
        }
コード例 #2
0
        public static INGetRequest Create(byte Version, byte[] Data, int DataSize)
        {
            var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes())
                        .Where(x => typeof(INGetRequest).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract);

            foreach (Type type in types)
            {
                INGetRequest creator = null;
                INGetRequest request = null;
                try
                {
                    creator = (INGetRequest)Activator.CreateInstance(type);
                }
                catch
                {
                    // Any error means this class can't handle the protocol,
                    // so try the next class
                }
                try
                {
                    bool testMode = false;
                    if (creator != null && Data != null && DataSize == 4)
                    {
                        var text = (Encoding.ASCII.GetString(Data, 0, 4) ?? "").Trim().ToUpper();
                        testMode = text == "TEST";
                    }
                    if (creator != null && (testMode || creator.Version == Version))
                    {
                        request = creator.Deserialize(Data, DataSize);
                    }
                }
                catch
                {
                    // Any error means this is the correct class for the protocol,
                    // but we had an error handling it, so return immediately
                    return(null);
                }
                return(request);
            }
            // No classes can handle the protocol
            return(null);
        }